今天,我们会在 docker 容器上部署最新的 wordpress 软件包,包括需要的前提条件,例如 nginx web 服务器、php5、mariadb 服务器等。下面是在运行在 docker 容器上成功安装 wordpress 的简单步骤。
<a target="_blank"></a>
在我们真正开始之前,我们需要确保在我们的 linux 机器上已经安装了 docker。我们使用的主机是 centos 7,因此我们用下面的命令使用 yum 管理器安装 docker。
<code># yum install docker</code>

安装 docker
<code># systemctl restart docker.service</code>
我们需要创建用于自动安装 wordpress 以及其前置需求的 dockerfile。这个 dockerfile 将用于构建 wordpress 的安装镜像。这个 wordpress dockerfile 会从 docker registry hub 获取 centos 7 镜像并用最新的可用更新升级系统。然后它会安装必要的软件,例如 nginx web 服务器、php、mariadb、open ssh 服务器,以及其它保证 docker 容器正常运行不可缺少的组件。最后它会执行一个初始化 wordpress 安装的脚本。
<code># nano dockerfile</code>
然后,我们需要将下面的配置行添加到 dockerfile中。
<code>from centos:centos7</code>
<code>maintainer the centos project <[email protected]></code>
<code></code>
<code>run yum -y update; yum clean all</code>
<code>run yum -y install epel-release; yum clean all</code>
<code>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</code>
<code>add ./start.sh /start.sh</code>
<code>add ./nginx-site.conf /nginx.conf</code>
<code>run mv /nginx.conf /etc/nginx/nginx.conf</code>
<code>run rm -rf /usr/share/nginx/html/*</code>
<code>run /usr/bin/easy_install supervisor</code>
<code>run /usr/bin/easy_install supervisor-stdout</code>
<code>add ./supervisord.conf /etc/supervisord.conf</code>
<code>run echo %sudo all=nopasswd: all >> /etc/sudoers</code>
<code>add http://wordpress.org/latest.tar.gz /wordpress.tar.gz</code>
<code>run tar xvzf /wordpress.tar.gz</code>
<code>run mv /wordpress/* /usr/share/nginx/html/.</code>
<code>run chown -r apache:apache /usr/share/nginx/</code>
<code>run chmod 755 /start.sh</code>
<code>run mkdir /var/run/sshd</code>
<code>expose 80</code>
<code>expose 22</code>
<code>cmd ["/bin/bash", "/start.sh"]</code>
wordpress docker 文件
我们创建了 dockerfile 之后,我们需要创建用于运行和配置 wordpress 安装的脚本,名称为 start.sh。它会为 wordpress 创建并配置数据库和密码。用我们喜欢的文本编辑器打开 start.sh。
<code># nano start.sh</code>
打开 start.sh 之后,我们要添加下面的配置行到文件中。
<code>#!/bin/bash</code>
<code>__check() {</code>
<code>if [ -f /usr/share/nginx/html/wp-config.php ]; then</code>
<code>exit</code>
<code>fi</code>
<code>}</code>
<code>__create_user() {</code>
<code># 创建用于 ssh 登录的用户</code>
<code>ssh_userpass=`pwgen -c -n -1 8`</code>
<code>useradd -g wheel user</code>
<code>echo user:$ssh_userpass | chpasswd</code>
<code>echo ssh user password: $ssh_userpass</code>
<code>__mysql_config() {</code>
<code># 启用并运行 mysql</code>
<code>yum -y erase mariadb mariadb-server</code>
<code>rm -rf /var/lib/mysql/ /etc/my.cnf</code>
<code>yum -y install mariadb mariadb-server</code>
<code>mysql_install_db</code>
<code>chown -r mysql:mysql /var/lib/mysql</code>
<code>/usr/bin/mysqld_safe &</code>
<code>sleep 10</code>
<code>__handle_passwords() {</code>
<code># 在这里我们生成随机密码(多亏了 pwgen)。前面两个用于 mysql 用户,最后一个用于 wp-config.php 的随机密钥。</code>
<code>wordpress_db="wordpress"</code>
<code>mysql_password=`pwgen -c -n -1 12`</code>
<code>wordpress_password=`pwgen -c -n -1 12`</code>
<code># 这是在日志中显示的密码。</code>
<code>echo mysql root password: $mysql_password</code>
<code>echo wordpress password: $wordpress_password</code>
<code>echo $mysql_password > /mysql-root-pw.txt</code>
<code>echo $wordpress_password > /wordpress-db-pw.txt</code>
<code># 这里原来是一个包括 sed、cat、pipe 和 stuff 的很长的行,但多亏了</code>
<code># @djfiander 的 https://gist.github.com/djfiander/6141138</code>
<code># 现在没有了</code>
<code>sed -e "s/database_name_here/$wordpress_db/</code>
<code>s/username_here/$wordpress_db/</code>
<code>s/password_here/$wordpress_password/</code>
<code>/'auth_key'/s/put your unique phrase here/`pwgen -c -n -1 65`/</code>
<code>/'secure_auth_key'/s/put your unique phrase here/`pwgen -c -n -1 65`/</code>
<code>/'logged_in_key'/s/put your unique phrase here/`pwgen -c -n -1 65`/</code>
<code>/'nonce_key'/s/put your unique phrase here/`pwgen -c -n -1 65`/</code>
<code>/'auth_salt'/s/put your unique phrase here/`pwgen -c -n -1 65`/</code>
<code>/'secure_auth_salt'/s/put your unique phrase here/`pwgen -c -n -1 65`/</code>
<code>/'logged_in_salt'/s/put your unique phrase here/`pwgen -c -n -1 65`/</code>
<code>/'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</code>
<code>__httpd_perms() {</code>
<code>chown apache:apache /usr/share/nginx/html/wp-config.php</code>
<code>__start_mysql() {</code>
<code># systemctl 启动 mysqld 服务</code>
<code>mysqladmin -u root password $mysql_password</code>
<code>mysql -uroot -p$mysql_password -e "create database wordpress; grant all privileges on wordpress.* to 'wordpress'@'localhost' identified by '$wordpress_password'; flush privileges;"</code>
<code>killall mysqld</code>
<code>__run_supervisor() {</code>
<code>supervisord -n</code>
<code># 调用所有函数</code>
<code>__check</code>
<code>__create_user</code>
<code>__mysql_config</code>
<code>__handle_passwords</code>
<code>__httpd_perms</code>
<code>__start_mysql</code>
<code>__run_supervisor</code>
启动脚本
增加完上面的配置之后,保存并关闭文件。
现在,我们需要创建 nginx web 服务器的配置文件,命名为 nginx-site.conf。
<code># nano nginx-site.conf</code>
然后,增加下面的配置信息到配置文件。
<code>user nginx;</code>
<code>worker_processes 1;</code>
<code>error_log /var/log/nginx/error.log;</code>
<code>#error_log /var/log/nginx/error.log notice;</code>
<code>#error_log /var/log/nginx/error.log info;</code>
<code>pid /run/nginx.pid;</code>
<code>events {</code>
<code>worker_connections 1024;</code>
<code>http {</code>
<code>include /etc/nginx/mime.types;</code>
<code>default_type application/octet-stream;</code>
<code>log_format main '$remote_addr - $remote_user [$time_local] "$request" '</code>
<code>'$status $body_bytes_sent "$http_referer" '</code>
<code>'"$http_user_agent" "$http_x_forwarded_for"';</code>
<code>access_log /var/log/nginx/access.log main;</code>
<code>sendfile on;</code>
<code>#tcp_nopush on;</code>
<code>#keepalive_timeout 0;</code>
<code>keepalive_timeout 65;</code>
<code>#gzip on;</code>
<code>index index.html index.htm index.php;</code>
<code># load modular configuration files from the /etc/nginx/conf.d directory.</code>
<code># see http://nginx.org/en/docs/ngx_core_module.html#include</code>
<code># for more information.</code>
<code>include /etc/nginx/conf.d/*.conf;</code>
<code>server {</code>
<code>listen 80;</code>
<code>server_name localhost;</code>
<code>#charset koi8-r;</code>
<code>#access_log logs/host.access.log main;</code>
<code>root /usr/share/nginx/html;</code>
<code>#error_page 404 /404.html;</code>
<code># redirect server error pages to the static page /50x.html</code>
<code>#</code>
<code>error_page 500 502 503 504 /50x.html;</code>
<code>location = /50x.html {</code>
<code>root html;</code>
<code># proxy the php scripts to apache listening on 127.0.0.1:80</code>
<code>#location ~ \.php$ {</code>
<code># proxy_pass http://127.0.0.1;</code>
<code>#}</code>
<code># pass the php scripts to fastcgi server listening on 127.0.0.1:9000</code>
<code>location ~ \.php$ {</code>
<code>try_files $uri =404;</code>
<code>fastcgi_pass 127.0.0.1:9000;</code>
<code>fastcgi_index index.php;</code>
<code>fastcgi_param script_filename $document_root$fastcgi_script_name;</code>
<code>include fastcgi_params;</code>
<code># deny access to .htaccess files, if apache's document root</code>
<code># concurs with nginx's one</code>
<code>#location ~ /\.ht {</code>
<code># deny all;</code>
nginx 配置
现在,创建 supervisor.conf 文件并添加下面的行。
<code># nano supervisord.conf</code>
然后,添加以下行。
<code>[unix_http_server]</code>
<code>file=/tmp/supervisor.sock ; (the path to the socket file)</code>
<code>[supervisord]</code>
<code>logfile=/tmp/supervisord.log ; (main log file;default $cwd/supervisord.log)</code>
<code>logfile_maxbytes=50mb ; (max main logfile bytes b4 rotation;default 50mb)</code>
<code>logfile_backups=10 ; (num of main logfile rotation backups;default 10)</code>
<code>loglevel=info ; (log level;default info; others: debug,warn,trace)</code>
<code>pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)</code>
<code>nodaemon=false ; (start in foreground if true;default false)</code>
<code>minfds=1024 ; (min. avail startup file descriptors;default 1024)</code>
<code>minprocs=200 ; (min. avail process descriptors;default 200)</code>
<code>; the below section must remain in the config file for rpc</code>
<code>; (supervisorctl/web interface) to work, additional interfaces may be</code>
<code>; added by defining them in separate rpcinterface: sections</code>
<code>[rpcinterface:supervisor]</code>
<code>supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface</code>
<code>[supervisorctl]</code>
<code>serverurl=unix:///tmp/supervisor.sock ; use a unix:// url for a unix socket</code>
<code>[program:php-fpm]</code>
<code>command=/usr/sbin/php-fpm -c /etc/php/fpm</code>
<code>stdout_events_enabled=true</code>
<code>stderr_events_enabled=true</code>
<code>[program:php-fpm-log]</code>
<code>command=tail -f /var/log/php-fpm/php-fpm.log</code>
<code>[program:mysql]</code>
<code>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</code>
<code>[program:nginx]</code>
<code>command=/usr/sbin/nginx</code>
<code>[eventlistener:stdout]</code>
<code>command = supervisor_stdout</code>
<code>buffer_size = 100</code>
<code>events = process_log</code>
<code>result_handler = supervisor_stdout:event_handler</code>
supervisord 配置
添加完后,保存并关闭文件。
现在,完成了创建配置文件和脚本之后,我们终于要使用 dockerfile 来创建安装最新的 wordpress cms(译者注:content management system,内容管理系统)所需要的容器,并根据配置文件进行配置。做到这点,我们需要在对应的目录中运行以下命令。
<code># docker build --rm -t wordpress:centos7 .</code>
构建 wordpress 容器
现在,执行以下命令运行新构建的容器,并为 nginx web 服务器和 ssh 访问打开88 和 22号相应端口 。
<code># cid=$(docker run -d -p 80:80 wordpress:centos7)</code>
运行 wordpress docker
运行以下命令检查进程以及容器内部执行的命令。
<code># echo "$(docker logs $cid )"</code>
运行以下命令检查端口映射是否正确。
<code># docker ps</code>
docker 状态
启动wordpress
现在,我们将通过 web 界面为 wordpress 面板设置 wordpress 的配置、用户名和密码。
wordpress 欢迎界面
然后,用上面用户名和密码输入到 wordpress 登录界面。
wordpress 登录
我们已经成功地在以 centos 7 作为 docker os 的 lemp 栈上构建并运行了 wordpress cms。从安全层面来说,在容器中运行 wordpress 对于宿主系统更加安全可靠。这篇文章介绍了在 docker 容器中运行的 nginx web 服务器上使用 wordpress 的完整配置。如果你有任何问题、建议、反馈,请在下面的评论框中写下来,让我们可以改进和更新我们的内容。非常感谢!enjoy :-)
<b></b>
<b>原文发布时间为:2015-06-03</b>
<b>本文来自云栖社区合作伙伴“linux中国”</b>