如何在 Docker 容器中架设一个完整的 WordPress 站点

大家好,今天我们来学习一下如何在 Docker 容器里运行的 Nginx Web 服务器中安装 WordPress。WordPress 是一个很好的免费开源的内容管理系统,全球成千上万的网站都在使用它。Docker 是一个开源项目,提供了一个可以打包、装载和运行任何应用的轻量级容器的开放平台。它没有语言支持、框架和打包系统的限制,从小型的家用电脑到高端服务器,在何时何地都可以运行。这使它们可以不依赖于特定软件栈和供应商,像一块块积木一样部署和扩展网络应用、数据库和后端服务。

今天,我们会在 docker 容器上部署最新的 WordPress 软件包,包括需要的前提条件,例如 Nginx Web 服务器、PHP5、MariaDB 服务器等。下面是在运行在 Docker 容器上成功安装 WordPress 的简单步骤。

 

1. 安装 Docker

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

  1. <span class="com"># yum install docker</span>

如何在 Docker 容器中架设一个完整的 WordPress 站点

  1. <span class="com"># systemctl restart docker.service</span>

 

2. 创建 WordPress 的 Dockerfile

我们需要创建用于自动安装 wordpress 以及其前置需求的 Dockerfile。这个 Dockerfile 将用于构建 WordPress 的安装镜像。这个 WordPress Dockerfile 会从 Docker Registry Hub 获取 CentOS 7 镜像并用最新的可用更新升级系统。然后它会安装必要的软件,例如 Nginx Web 服务器、PHP、MariaDB、Open SSH 服务器,以及其它保证 Docker 容器正常运行不可缺少的组件。最后它会执行一个初始化 WordPress 安装的脚本。

  1. <span class="com"># nano Dockerfile</span>

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

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

如何在 Docker 容器中架设一个完整的 WordPress 站点

 

3. 创建启动脚本

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

  1. <span class="com"># nano start.sh</span>

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

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

如何在 Docker 容器中架设一个完整的 WordPress 站点

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

 

4. 创建配置文件

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

  1. <span class="com"># nano nginx-site.conf</span>

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

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

如何在 Docker 容器中架设一个完整的 WordPress 站点

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

  1. <span class="com"># nano supervisord.conf</span>

然后,添加以下行。

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

如何在 Docker 容器中架设一个完整的 WordPress 站点

添加完后,保存并关闭文件。

 

5. 构建 WordPress 容器

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

  1. <span class="com"># docker build --rm -t wordpress:centos7 .</span>

如何在 Docker 容器中架设一个完整的 WordPress 站点

 

6. 运行 WordPress 容器

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

  1. <span class="com"># CID=$(docker run -d -p 80:80 wordpress:centos7)</span>

如何在 Docker 容器中架设一个完整的 WordPress 站点

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

  1. <span class="com"># echo "$(docker logs $CID )"</span>

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

  1. <span class="com"># docker ps</span>

如何在 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 :-)

Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里


via: http://linoxide.com/linux-how-to/install-wordpress-nginx-docker-container/

作者:Arun Pyasi 译者:ictlyh 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

相关推荐