服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - 再Docker中架设完整的WordPress站点全攻略

再Docker中架设完整的WordPress站点全攻略

2020-11-10 17:00Arun Pyasi PHP教程

这篇文章主要介绍了再Docker中架设完整的WordPress站点全攻略,Docker是当下最火爆的虚拟机类技术,需要的朋友可以参考下

1. 安装 docker

在我们真正开始之前,我们需要确保在我们的 linux 机器上已经安装了 docker。我们使用的主机是 centos 7,因此我们用下面的命令使用 yum 管理器安装 docker。

?
1
# yum install docker

  

再Docker中架设完整的WordPress站点全攻略

?
1
# systemctl restart docker.service

2. 创建 wordpress 的 dockerfile

我们需要创建用于自动安装 wordpress 以及其前置需求的 dockerfile。这个 dockerfile 将用于构建 wordpress 的安装镜像。这个 wordpress dockerfile 会从 docker registry hub 获取 centos 7 镜像并用最新的可用更新升级系统。然后它会安装必要的软件,例如 nginx web 服务器、php、mariadb、open ssh 服务器,以及其它保证 docker 容器正常运行不可缺少的组件。最后它会执行一个初始化 wordpress 安装的脚本。

?
1
# nano dockerfile

然后,我们需要将下面的配置行添加到 dockerfile中。

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from centos:centos7
 maintainer the centos project <cloud-ops@centos.org>
 run yum -y update; yum clean all
 run yum -y install epel-release; yum clean all
 run yum -y install mariadb mariadb-server mariadb-client nginx php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy php-apc pwgen python-setuptools curl git tar; yum clean all
 add ./start.sh /start.sh
 add ./nginx-site.conf /nginx.conf
 run mv /nginx.conf /etc/nginx/nginx.conf
 run rm -rf /usr/share/nginx/html/*
 run /usr/bin/easy_install supervisor
 run /usr/bin/easy_install supervisor-stdout
 add ./supervisord.conf /etc/supervisord.conf
 run echo %sudo all=nopasswd: all >> /etc/sudoers
 add http://wordpress.org/latest.tar.gz /wordpress.tar.gz
 run tar xvzf /wordpress.tar.gz
 run mv /wordpress/* /usr/share/nginx/html/.
 run chown -r apache:apache /usr/share/nginx/
 run chmod 755 /start.sh
 run mkdir /var/run/sshd
 expose 80
 expose 22
 cmd ["/bin/bash", "/start.sh"]

再Docker中架设完整的WordPress站点全攻略

    3. 创建启动脚本

我们创建了 dockerfile 之后,我们需要创建用于运行和配置 wordpress 安装的脚本,名称为 start.sh。它会为 wordpress 创建并配置数据库和密码。用我们喜欢的文本编辑器打开 start.sh。

?
1
# nano start.sh

打开 start.sh 之后,我们要添加下面的配置行到文件中。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
__check() {
if [ -f /usr/share/nginx/html/wp-config.php ]; then
exit
fi
}
__create_user() {
# 创建用于 ssh 登录的用户
ssh_userpass=`pwgen -c -n -1 8`
useradd -g wheel user
echo user:$ssh_userpass | chpasswd
echo ssh user password: $ssh_userpass
}
__mysql_config() {
# 启用并运行 mysql
yum -y erase mariadb mariadb-server
rm -rf /var/lib/mysql/ /etc/my.cnf
yum -y install mariadb mariadb-server
mysql_install_db
chown -r mysql:mysql /var/lib/mysql
/usr/bin/mysqld_safe &
sleep 10
}
__handle_passwords() {
# 在这里我们生成随机密码(多亏了 pwgen)。前面两个用于 mysql 用户,最后一个用于 wp-config.php 的随机密钥。
wordpress_db="wordpress"
mysql_password=`pwgen -c -n -1 12`
wordpress_password=`pwgen -c -n -1 12`
# 这是在日志中显示的密码。
echo mysql root password: $mysql_password
echo wordpress password: $wordpress_password
echo $mysql_password > /mysql-root-pw.txt
echo $wordpress_password > /wordpress-db-pw.txt
# 这里原来是一个包括 sed、cat、pipe 和 stuff 的很长的行,但多亏了
# @djfiander 的 https://gist.github.com/djfiander/6141138
# 现在没有了
sed -e "s/database_name_here/$wordpress_db/
s/username_here/$wordpress_db/
s/password_here/$wordpress_password/
/'auth_key'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'secure_auth_key'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'logged_in_key'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'nonce_key'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'auth_salt'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'secure_auth_salt'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'logged_in_salt'/s/put your unique phrase here/`pwgen -c -n -1 65`/
/'nonce_salt'/s/put your unique phrase here/`pwgen -c -n -1 65`/" /usr/share/nginx/html/wp-config-sample.php > /usr/share/nginx/html/wp-config.php
}
__httpd_perms() {
chown apache:apache /usr/share/nginx/html/wp-config.php
}
__start_mysql() {
# systemctl 启动 mysqld 服务
mysqladmin -u root password $mysql_password
mysql -uroot -p$mysql_password -e "create database wordpress; grant all privileges on wordpress.* to 'wordpress'@'localhost' identified by '$wordpress_password'; flush privileges;"
killall mysqld
sleep 10
}
__run_supervisor() {
supervisord -n
}
# 调用所有函数
__check
__create_user
__mysql_config
__handle_passwords
__httpd_perms
__start_mysql
__run_supervisor

再Docker中架设完整的WordPress站点全攻略

    增加完上面的配置之后,保存并关闭文件。
4. 创建配置文件

现在,我们需要创建 nginx web 服务器的配置文件,命名为 nginx-site.conf。

?
1
# nano nginx-site.conf

然后,增加下面的配置信息到配置文件。

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
user nginx;
  worker_processes 1;
  error_log /var/log/nginx/error.log;
  #error_log /var/log/nginx/error.log notice;
  #error_log /var/log/nginx/error.log info;
  pid /run/nginx.pid;
  events {
  worker_connections 1024;
  }
  http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  access_log /var/log/nginx/access.log main;
  sendfile on;
  #tcp_nopush on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #gzip on;
  index index.html index.htm index.php;
  # load modular configuration files from the /etc/nginx/conf.d directory.
  # see http://nginx.org/en/docs/ngx_core_module.html#include
  # for more information.
  include /etc/nginx/conf.d/*.conf;
  server {
  listen 80;
  server_name localhost;
  #charset koi8-r;
  #access_log logs/host.access.log main;
  root /usr/share/nginx/html;
  #error_page 404 /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
  root html;
  }
  # proxy the php scripts to apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  # proxy_pass http://127.0.0.1;
  #}
  # pass the php scripts to fastcgi server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {
  root /usr/share/nginx/html;
  try_files $uri =404;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param script_filename $document_root$fastcgi_script_name;
  include fastcgi_params;
  }
  # deny access to .htaccess files, if apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  # deny all;
  #}
  }
  }

再Docker中架设完整的WordPress站点全攻略

    现在,创建 supervisor.conf 文件并添加下面的行。

?
1
# nano supervisord.conf

然后,添加以下行。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $cwd/supervisord.log)
logfile_maxbytes=50mb ; (max main logfile bytes b4 rotation;default 50mb)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
; the below section must remain in the config file for rpc
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// url for a unix socket
[program:php-fpm]
command=/usr/sbin/php-fpm -c /etc/php/fpm
stdout_events_enabled=true
stderr_events_enabled=true
[program:php-fpm-log]
command=tail -f /var/log/php-fpm/php-fpm.log
stdout_events_enabled=true
stderr_events_enabled=true
[program:mysql]
command=/usr/bin/mysql --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
stdout_events_enabled=true
stderr_events_enabled=true
[program:nginx]
command=/usr/sbin/nginx
stdout_events_enabled=true
stderr_events_enabled=true
[eventlistener:stdout]
command = supervisor_stdout
buffer_size = 100
events = process_log
result_handler = supervisor_stdout:event_handler

   

再Docker中架设完整的WordPress站点全攻略

    添加完后,保存并关闭文件。
5. 构建 wordpress 容器

现在,完成了创建配置文件和脚本之后,我们终于要使用 dockerfile 来创建安装最新的 wordpress cms(译者注:content management system,内容管理系统)所需要的容器,并根据配置文件进行配置。做到这点,我们需要在对应的目录中运行以下命令。

?
1
# docker build --rm -t wordpress:centos7 .

 

再Docker中架设完整的WordPress站点全攻略

    6. 运行 wordpress 容器

现在,执行以下命令运行新构建的容器,并为 nginx web 服务器和 ssh 访问打开88 和 22号相应端口 。

?
1
# cid=$(docker run -d -p 80:80 wordpress:centos7)

 

再Docker中架设完整的WordPress站点全攻略

    运行以下命令检查进程以及容器内部执行的命令。

  

?
1
# echo "$(docker logs $cid )"

运行以下命令检查端口映射是否正确。

?
1
# docker ps

再Docker中架设完整的WordPress站点全攻略

    7. web 界面

最后如果一切正常的话,当我们用浏览器打开 http://ip-address/ 或者 http://mywebsite.com/ 的时候会看到 wordpress 的欢迎界面。

再Docker中架设完整的WordPress站点全攻略

现在,我们将通过 web 界面为 wordpress 面板设置 wordpress 的配置、用户名和密码。

再Docker中架设完整的WordPress站点全攻略

然后,用上面用户名和密码输入到 wordpress 登录界面。

再Docker中架设完整的WordPress站点全攻略

总结

我们已经成功地在以 centos 7 作为 docker os 的 lemp 栈上构建并运行了 wordpress cms。从安全层面来说,在容器中运行 wordpress 对于宿主系统更加安全可靠。这篇文章介绍了在 docker 容器中运行的 nginx web 服务器上使用 wordpress 的完整配置。如果你有任何问题、建议、反馈,请在下面的评论框中写下来,让我们可以改进和更新我们的内容。非常感谢!enjoy :-)

延伸 · 阅读

精彩推荐