当前位置:首页 > 行业动态 > 正文

如何在centos7中使用docker建立wordpress博客以及踩坑记录

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器或 Windows 机器上,也可以实现虚拟化,本文将介绍如何在 CentOS 7 中使用 Docker 建立 WordPress 博客,并分享一些踩坑记录。

环境准备

1、系统要求:CentOS 7.x 及以上版本

2、Docker:17.03 及以上版本

3、Nginx:1.14.2 及以上版本

4、MySQL:5.7 及以上版本

5、Wordpress:5.6.2 及以上版本

安装 Docker

1、更新系统软件包:

sudo yum update -y

2、安装依赖软件包:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

3、添加 Docker 仓库:

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

4、安装 Docker CE:

sudo yum install -y docker-ce docker-ce-cli containerd.io

5、启动 Docker 服务:

sudo systemctl start docker
sudo systemctl enable docker

安装 Nginx、MySQL 和 WordPress

1、创建 Nginx、MySQL 和 WordPress 用户:

sudo useradd -r nginxuser mysqluser wordpressuser
sudo passwd nginxuser mysqluser wordpressuser

2、下载 Nginx、MySQL 和 WordPress 镜像:

sudo yum download-cache --downloadonly --downloaddir=/tmp/docker-images --repoid=*/centos/7/* --resolve --destdir=/tmp/docker-images http://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/nginx-1.18.0-3.el7.x86_64.rpm http://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/mysql-5.7.35-1.el7.x86_64.rpm http://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/wordpress-5.6.2-1.el7.x86_64.rpm

3、将下载的镜像导入到 Docker:

sudo yum localinstall --nogpgcheck -y 
    /tmp/docker-images/*.rpm 
    /tmp/docker-images/*.gz 
    /tmp/docker-images/*.bz2 
    /tmp/docker-images/*sha256sums 
    /tmp/docker-images/*manifests 
    /tmp/docker-images/*metalink 
    /tmp/docker-images/*signatures 
    ; 
    rm -rf /tmp/docker-images/* 

4、创建 WordPress 数据库和用户:

mysqladmin -u root password 'your_password' --authentication-string='mysql_native_password' --skip-grant-tables &> /dev/null && mysql -uroot <<EOF && create database wordpress; grant all privileges on wordpress.* to wordpressuser@localhost identified by 'your_password'; flush privileges; exit;EOF &> /dev/null && mysqladmin -u root shutdown && killall mysqld && sleep 3 && systemctl restart mysqld

5、在 WordPress 目录下创建一个名为 wp-config.php 的文件,内容如下:

<?php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpressuser');
define('DB_PASSWORD', 'your_password');
define('DB_HOST', 'localhost');
$dbh = new PDO('mysql:host=localhost;dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

运行 WordPress Web 服务器和 NGINX Web 服务器容器

1、以 root 用户身份运行以下命令,创建 WordPress Web 服务器容器:

sudo docker run --name wordpress --restart=always --volume=~/html:/var/www/html --volume=~/nginx:/usr/share/nginx --volume=~/mysql:/var/lib/mysql --publish=80:80 --detach php:7.4-apache wordpress bash -c "cd html && composer install && php bin/console cache:clear && php bin/console setup" > ~/logs/wordpress.log & tailf ~/logs/wordpress.log & disown %1 & sleep infinity & sudo killall tail || true & sudo killall tail || true & sudo killall tail || true & sudo killall tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || true & sudo pkill tail || false && echo 'Wordpress web server is running!' && exit;" | tee logs/wordpress_container_status.txt; echo $! > logs/wordpress_container_pid; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo 'Nginx web server is running!' && exit;" | tee logs/nginx_container_status.txt; echo $! > logs/nginx_container_pid; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '';echo '/etc//nginx//conf//nginx//conf//sites//default//index' > logs/nginx_sitemap_redirects && curl http://localhost > logs/nginx_accesslog && curl http://localhost > logs/nginx_errorlog && sleep Infinity && exit < logs/wordpress_container_pid logs/nginx_container_pid > logs/container_monitoring.txt& (tailf logs/* | grep "GET|POST|PUT|DELETE|HEAD|OPTIONS" >> logs/accesslogs)& (tailf logs/* | grep "ERROR|WARNING" >> logs/errorlogs)& (tailf logs/* | grep "HTTP|Server|Connection|Content" >> logs/httplogs)& (tailf logs/* | grep "time|date|upstream|server|request|bytes|sent|received" >> logs/performancelogs)& (tailf logs/* | grep "slow|fast|bytes|bytes_transferred|bytes_received|bytes_sent|connect" >> logs/bandwidthlogs)& (tailf logs/* | grep "keepalive|keepalive_timeout|proxy_read|proxy_write|proxy_connect" >> logs/loadlogs)& (tailf logs/* | grep "upstream\['server']" >> logs/upstreamlogs)& (tailf logs/* | grep "location\['path']" >> logs/locationlogs)& (tailf logs/* | grep "try_files\['$uri\','$uri\@fastcgi\['$fastcgi_param\']['$scheme
0