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


Posted in PHP onJuly 29, 2015

1. 安装 Docker

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

# yum install docker

  

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

# 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 安装的脚本。

# nano Dockerfile

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

   

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。

# nano start.sh

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

 

#!/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。

# nano nginx-site.conf

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

   

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 文件并添加下面的行。

# nano supervisord.conf

然后,添加以下行。

[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,内容管理系统)所需要的容器,并根据配置文件进行配置。做到这点,我们需要在对应的目录中运行以下命令。

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

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

    6. 运行 WordPress 容器

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

# CID=$(docker run -d -p 80:80 wordpress:centos7)

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

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

  

# echo "$(docker logs $CID )"

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

# 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 :-)

PHP 相关文章推荐
PHP中ADODB类详解
Mar 25 PHP
php 生成饼图 三维饼图
Sep 28 PHP
PHP 时间转换Unix时间戳代码
Jan 22 PHP
php str_pad() 将字符串填充成指定长度的字符串
Feb 23 PHP
PHP实现Soap通讯的方法
Nov 03 PHP
学习php开源项目的源码指南
Dec 21 PHP
yii2中添加验证码的实现方法
Jan 09 PHP
php实现爬取和分析知乎用户数据
Jan 26 PHP
CodeIgniter基于Email类发邮件的方法
Mar 29 PHP
thinkPHP中钩子的使用方法实例分析
Nov 16 PHP
PHP的PDO连接讲解
Jan 24 PHP
PHP中md5()函数的用法讲解
Mar 30 PHP
php去掉文件前几行的方法
Jul 29 #PHP
PHP实现的简单网络硬盘
Jul 29 #PHP
PHP简单生成缩略图相册的方法
Jul 29 #PHP
PHP之预定义接口详解
Jul 29 #PHP
PHP实现的迷你漂流瓶
Jul 29 #PHP
PHP之浮点数计算比较以及取整数不准确的解决办法
Jul 29 #PHP
PHP实现简单搜歌的方法
Jul 28 #PHP
You might like
虫族 Zerg 魔法科技
2020/03/14 星际争霸
《神奇女侠:血脉》神力女超人大战犯罪公司
2020/04/09 欧美动漫
PHP连接局域网MYSQL数据库的简单实例
2013/08/26 PHP
php中namespace use用法实例分析
2016/01/22 PHP
yii2简单使用less代替css示例
2017/03/10 PHP
php 多继承的几种常见实现方法示例
2019/11/18 PHP
改善用户体验的五款jQuery插件分享
2011/05/22 Javascript
基于jquery ajax 用户无刷新登录方法详解
2012/04/28 Javascript
jQuery使用empty()方法删除元素及其所有子元素的方法
2015/03/26 Javascript
jquery.gridrotator实现响应式图片展示画廊效果
2015/06/23 Javascript
关于JS中match() 和 exec() 返回值和属性的测试
2016/03/21 Javascript
谈谈对vue响应式数据更新的误解
2017/08/01 Javascript
基于vue-ssr的静态网站生成器VuePress 初体验
2018/04/17 Javascript
AngularJS实现动态切换样式的方法分析
2018/06/26 Javascript
[49:27]2018DOTA2亚洲邀请赛 4.4 淘汰赛 TNC vs VG 第一场
2018/04/05 DOTA
Python中SOAP项目的介绍及其在web开发中的应用
2015/04/14 Python
Django管理员账号和密码忘记的完美解决方法
2018/12/06 Python
Python实现繁?转为简体的方法示例
2018/12/18 Python
python利用跳板机ssh远程连接redis的方法
2019/02/19 Python
Python 3.8新特征之asyncio REPL
2019/05/28 Python
django之静态文件 django 2.0 在网页中显示图片的例子
2019/07/28 Python
Django文件存储 默认存储系统解析
2019/08/02 Python
在Python中字典按值排序的实现方法
2020/11/12 Python
python 爬取百度文库并下载(免费文章限定)
2020/12/04 Python
德国机车企业:FC-Moto
2017/10/27 全球购物
ghd法国官方网站:英国最受欢迎的美发工具品牌
2019/04/18 全球购物
师范应届生语文教师求职信
2013/10/29 职场文书
老师自我鉴定范文
2013/12/25 职场文书
客服服务心得体会
2013/12/30 职场文书
幼儿园小班教学反思
2014/02/02 职场文书
企业内部培训方案
2014/02/04 职场文书
给学校建议书范文
2014/05/13 职场文书
大学生个人总结范文
2015/02/15 职场文书
2015年“我们的节日·重阳节”活动总结
2015/07/29 职场文书
Python机器学习之底层实现KNN
2021/06/20 Python
CSS 鼠标选中文字后改变背景色的实现代码
2023/05/21 HTML / CSS