如何在 Docker 容器中架设一个完整的 WordPress 站点
大家好,今天我们来学习一下如何在 Docker 容器里运行的 Nginx Web 服务器中安装 WordPress。WordPress 是一个很好的免费开源的内容管理系统,全球成千上万的网站都在使用它。Docker 是一个开源项目,提供了一个可以打包、装载和运行任何应用的轻量级容器的开放平台。它没有语言支持、框架和打包系统的限制,从小型的家用电脑到高端服务器,在何时何地都可以运行。这使它们可以不依赖于特定软件栈和供应商,像一块块积木一样部署和扩展网络应用、数据库和后端服务。
今天,我们会在 docker 容器上部署最新的 WordPress 软件包,包括需要的前提条件,例如 Nginx Web 服务器、PHP5、MariaDB 服务器等。下面是在运行在 Docker 容器上成功安装 WordPress 的简单步骤。
1. 安装 Docker
在我们真正开始之前,我们需要确保在我们的 Linux 机器上已经安装了 Docker。我们使用的主机是 CentOS 7,因此我们用下面的命令使用 yum 管理器安装 docker。
<span class="com"># yum install docker</span>
<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 安装的脚本。
<span class="com"># nano Dockerfile</span>
然后,我们需要将下面的配置行添加到 Dockerfile中。
<span class="pln">FROM centos</span><span class="pun">:</span><span class="pln">centos7</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span class="com">RUN /usr/bin/easy_install supervisor</span>
<span class="com">RUN /usr/bin/easy_install supervisor-stdout</span>
<span class="com">ADD ./supervisord.conf /etc/supervisord.conf</span>
<span class="com">RUN echo %sudo ALL=NOPASSWD: ALL >> /etc/sudoers</span>
<span class="com">ADD http://wordpress.org/latest.tar.gz /wordpress.tar.gz</span>
<span class="com">RUN tar xvzf /wordpress.tar.gz</span>
<span class="com">RUN mv /wordpress/* /usr/share/nginx/html/.</span>
<span class="com">RUN chown -R apache:apache /usr/share/nginx/</span>
<span class="com">RUN chmod 755 /start.sh</span>
<span class="com">RUN mkdir /var/run/sshd</span>
<span class="com">EXPOSE 80</span>
<span class="com">EXPOSE 22</span>
<span class="com">CMD ["/bin/bash", "/start.sh"]</span>
3. 创建启动脚本
我们创建了 Dockerfile 之后,我们需要创建用于运行和配置 WordPress 安装的脚本,名称为 start.sh。它会为 WordPress 创建并配置数据库和密码。用我们喜欢的文本编辑器打开 start.sh。
<span class="com"># nano start.sh</span>
打开 start.sh 之后,我们要添加下面的配置行到文件中。
<span class="com">#!/bin/bash</span>
<span class="pln">__check</span><span class="pun">()</span><span class="pun">{</span>
<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>
<span class="kwd">exit</span>
<span class="kwd">fi</span>
<span class="pun">}</span>
<span class="pln">__create_user</span><span class="pun">()</span><span class="pun">{</span>
<span class="com"># 创建用于 SSH 登录的用户</span>
<span class="pln">SSH_USERPASS</span><span class="pun">=</span><span class="str">`pwgen -c -n -1 8`</span>
<span class="pln">useradd </span><span class="pun">-</span><span class="pln">G wheel user</span>
<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>
<span class="pln">echo ssh user password</span><span class="pun">:</span><span class="pln"> $SSH_USERPASS</span>
<span class="pun">}</span>
<span class="pln">__mysql_config</span><span class="pun">()</span><span class="pun">{</span>
<span class="com"># 启用并运行 MySQL</span>
<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>
<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>
<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>
<span class="pln">mysql_install_db</span>
<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>
<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>
<span class="pln">sleep </span><span class="lit">10</span>
<span class="pun">}</span>
<span class="pln">__handle_passwords</span><span class="pun">()</span><span class="pun">{</span>
<span class="com"># 在这里我们生成随机密码(多亏了 pwgen)。前面两个用于 mysql 用户,最后一个用于 wp-config.php 的随机密钥。</span>
<span class="pln">WORDPRESS_DB</span><span class="pun">=</span><span class="str">"wordpress"</span>
<span class="pln">MYSQL_PASSWORD</span><span class="pun">=</span><span class="str">`pwgen -c -n -1 12`</span>
<span class="pln">WORDPRESS_PASSWORD</span><span class="pun">=</span><span class="str">`pwgen -c -n -1 12`</span>
<span class="com"># 这是在日志中显示的密码。</span>
<span class="pln">echo mysql root password</span><span class="pun">:</span><span class="pln"> $MYSQL_PASSWORD</span>
<span class="pln">echo wordpress password</span><span class="pun">:</span><span class="pln"> $WORDPRESS_PASSWORD</span>
<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>
<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>
<span class="com"># 这里原来是一个包括 sed、cat、pipe 和 stuff 的很长的行,但多亏了</span>
<span class="com"># @djfiander 的 https://gist.github.com/djfiander/6141138</span>
<span class="com"># 现在没有了</span>
<span class="pln">sed </span><span class="pun">-</span><span class="pln">e </span><span class="str">"s/database_name_here/$WORDPRESS_DB/</span>
<span class="str">s/username_here/$WORDPRESS_DB/</span>
<span class="str">s/password_here/$WORDPRESS_PASSWORD/</span>
<span class="str">/'AUTH_KEY'/s/put your unique phrase here/`pwgen -c -n -1 65`/</span>
<span class="str">/'SECURE_AUTH_KEY'/s/put your unique phrase here/`pwgen -c -n -1 65`/</span>
<span class="str">/'LOGGED_IN_KEY'/s/put your unique phrase here/`pwgen -c -n -1 65`/</span>
<span class="str">/'NONCE_KEY'/s/put your unique phrase here/`pwgen -c -n -1 65`/</span>
<span class="str">/'AUTH_SALT'/s/put your unique phrase here/`pwgen -c -n -1 65`/</span>
<span class="str">/'SECURE_AUTH_SALT'/s/put your unique phrase here/`pwgen -c -n -1 65`/</span>
<span class="str">/'LOGGED_IN_SALT'/s/put your unique phrase here/`pwgen -c -n -1 65`/</span>
<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>
<span class="pun">}</span>
<span class="pln">__httpd_perms</span><span class="pun">()</span><span class="pun">{</span>
<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>
<span class="pun">}</span>
<span class="pln">__start_mysql</span><span class="pun">()</span><span class="pun">{</span>
<span class="com"># systemctl 启动 mysqld 服务</span>
<span class="pln">mysqladmin </span><span class="pun">-</span><span class="pln">u root password $MYSQL_PASSWORD</span>
<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>
<span class="pln">killall mysqld</span>
<span class="pln">sleep </span><span class="lit">10</span>
<span class="pun">}</span>
<span class="pln">__run_supervisor</span><span class="pun">()</span><span class="pun">{</span>
<span class="pln">supervisord </span><span class="pun">-</span><span class="pln">n</span>
<span class="pun">}</span>
<span class="com"># 调用所有函数</span>
<span class="pln">__check</span>
<span class="pln">__create_user</span>
<span class="pln">__mysql_config</span>
<span class="pln">__handle_passwords</span>
<span class="pln">__httpd_perms</span>
<span class="pln">__start_mysql</span>
<span class="pln">__run_supervisor</span>
增加完上面的配置之后,保存并关闭文件。
4. 创建配置文件
现在,我们需要创建 Nginx Web 服务器的配置文件,命名为 nginx-site.conf。
<span class="com"># nano nginx-site.conf</span>
然后,增加下面的配置信息到配置文件。
<span class="pln">user nginx</span><span class="pun">;</span>
<span class="pln">worker_processes </span><span class="lit">1</span><span class="pun">;</span>
<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>
<span class="com">#error_log /var/log/nginx/error.log notice;</span>
<span class="com">#error_log /var/log/nginx/error.log info;</span>
<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>
<span class="pln">events </span><span class="pun">{</span>
<span class="pln">worker_connections </span><span class="lit">1024</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="pln">http </span><span class="pun">{</span>
<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>
<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>
<span class="pln">log_format main </span><span class="str">'$remote_addr - $remote_user [$time_local] "$request" '</span>
<span class="str">'$status $body_bytes_sent "$http_referer" '</span>
<span class="str">'"$http_user_agent" "$http_x_forwarded_for"'</span><span class="pun">;</span>
<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>
<span class="pln">sendfile on</span><span class="pun">;</span>
<span class="com">#tcp_nopush on;</span>
<span class="com">#keepalive_timeout 0;</span>
<span class="pln">keepalive_timeout </span><span class="lit">65</span><span class="pun">;</span>
<span class="com">#gzip on;</span>
<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>
<span class="com"># Load modular configuration files from the /etc/nginx/conf.d directory.</span>
<span class="com"># See http://nginx.org/en/docs/ngx_core_module.html#include</span>
<span class="com"># for more information.</span>
<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>
<span class="com">server {</span>
<span class="com">listen 80;</span>
<span class="com">server_name localhost;</span>
<span class="com">#charset koi8-r;</span>
<span class="com">#access_log logs/host.access.log main;</span>
<span class="com">root /usr/share/nginx/html;</span>
<span class="com">#error_page 404 /404.html;</span>
<span class="com"># redirect server error pages to the static page /50x.html</span>
<span class="com">#</span>
<span class="com">error_page 500 502 503 504 /50x.html;</span>
<span class="com">location = /50x.html {</span>
<span class="com">root html;</span>
<span class="com">}</span>
<span class="com"># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span>
<span class="com">#</span>
<span class="com">#location ~ \.php$ {</span>
<span class="com"># proxy_pass http://127.0.0.1;</span>
<span class="com">#}</span>
<span class="com"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span>
<span class="com">#</span>
<span class="com">location ~ \.php$ {</span>
<span class="com">root /usr/share/nginx/html;</span>
<span class="com">try_files $uri =404;</span>
<span class="com">fastcgi_pass 127.0.0.1:9000;</span>
<span class="com">fastcgi_index index.php;</span>
<span class="com">fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;</span>
<span class="com">include fastcgi_params;</span>
<span class="com">}</span>
<span class="com"># deny access to .htaccess files, if Apache's document root</span>
<span class="com"># concurs with nginx's one</span>
<span class="com">#</span>
<span class="com">#location ~ /\.ht {</span>
<span class="com"># deny all;</span>
<span class="com">#}</span>
<span class="com">}</span>
<span class="com">}</span>
现在,创建 supervisor.conf 文件并添加下面的行。
<span class="com"># nano supervisord.conf</span>
然后,添加以下行。
<span class="pun">[</span><span class="pln">unix_http_server</span><span class="pun">]</span>
<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>
<span class="pun">[</span><span class="pln">supervisord</span><span class="pun">]</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span class="pun">[</span><span class="pln">rpcinterface</span><span class="pun">:</span><span class="pln">supervisor</span><span class="pun">]</span>
<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>
<span class="pun">[</span><span class="pln">supervisorctl</span><span class="pun">]</span>
<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>
<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">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>
<span class="pln">stdout_events_enabled</span><span class="pun">=</span><span class="kwd">true</span>
<span class="pln">stderr_events_enabled</span><span class="pun">=</span><span class="kwd">true</span>
<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>
<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>
<span class="pln">stdout_events_enabled</span><span class="pun">=</span><span class="kwd">true</span>
<span class="pln">stderr_events_enabled</span><span class="pun">=</span><span class="kwd">true</span>
<span class="pun">[</span><span class="pln">program</span><span class="pun">:</span><span class="pln">mysql</span><span class="pun">]</span>
<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>
<span class="pln">stdout_events_enabled</span><span class="pun">=</span><span class="kwd">true</span>
<span class="pln">stderr_events_enabled</span><span class="pun">=</span><span class="kwd">true</span>
<span class="pun">[</span><span class="pln">program</span><span class="pun">:</span><span class="pln">nginx</span><span class="pun">]</span>
<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>
<span class="pln">stdout_events_enabled</span><span class="pun">=</span><span class="kwd">true</span>
<span class="pln">stderr_events_enabled</span><span class="pun">=</span><span class="kwd">true</span>
<span class="pun">[</span><span class="pln">eventlistener</span><span class="pun">:</span><span class="pln">stdout</span><span class="pun">]</span>
<span class="pln">command </span><span class="pun">=</span><span class="pln"> supervisor_stdout</span>
<span class="pln">buffer_size </span><span class="pun">=</span><span class="lit">100</span>
<span class="pln">events </span><span class="pun">=</span><span class="pln"> PROCESS_LOG</span>
<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>
添加完后,保存并关闭文件。
5. 构建 WordPress 容器
现在,完成了创建配置文件和脚本之后,我们终于要使用 Dockerfile 来创建安装最新的 WordPress CMS(译者注:Content Management System,内容管理系统)所需要的容器,并根据配置文件进行配置。做到这点,我们需要在对应的目录中运行以下命令。
<span class="com"># docker build --rm -t wordpress:centos7 .</span>
6. 运行 WordPress 容器
现在,执行以下命令运行新构建的容器,并为 Nginx Web 服务器和 SSH 访问打开88 和 22号相应端口 。
<span class="com"># CID=$(docker run -d -p 80:80 wordpress:centos7)</span>
运行以下命令检查进程以及容器内部执行的命令。
<span class="com"># echo "$(docker logs $CID )"</span>
运行以下命令检查端口映射是否正确。
<span class="com"># docker ps</span>
7. Web 界面
最后如果一切正常的话,当我们用浏览器打开 http://ip-address/ 或者 http://mywebsite.com/ 的时候会看到 WordPress 的欢迎界面。
现在,我们将通过 Web 界面为 WordPress 面板设置 WordPress 的配置、用户名和密码。
然后,用上面用户名和密码输入到 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