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

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:

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

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

1. 安装 Docker

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

 
  1. # yum install docker

安装 Docker

安装 Docker

 
  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. FROM centos:centos7
  2. MAINTAINER The CentOS Project <cloud-ops@centos.org>
  3. RUN yum -y update; yum clean all
  4. RUN yum -y install epel-release; yum clean all
  5. 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
  6. ADD ./start.sh /start.sh
  7. ADD ./nginx-site.conf /nginx.conf
  8. RUN mv /nginx.conf /etc/nginx/nginx.conf
  9. RUN rm -rf /usr/share/nginx/html/*
  10. RUN /usr/bin/easy_install supervisor
  11. RUN /usr/bin/easy_install supervisor-stdout
  12. ADD ./supervisord.conf /etc/supervisord.conf
  13. RUN echo %sudo ALL=NOPASSWD: ALL >> /etc/sudoers
  14. ADD http://wordpress.org/latest.tar.gz /wordpress.tar.gz
  15. RUN tar xvzf /wordpress.tar.gz
  16. RUN mv /wordpress/* /usr/share/nginx/html/.
  17. RUN chown -R apache:apache /usr/share/nginx/
  18. RUN chmod 755 /start.sh
  19. RUN mkdir /var/run/sshd
  20. EXPOSE 80
  21. EXPOSE 22
  22. CMD ["/bin/bash", "/start.sh"]

Wordpress Docker 文件

Wordpress Docker 文件

3. 创建启动脚本

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

 
  1. # nano start.sh

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

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

启动脚本

启动脚本

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

4. 创建配置文件

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

 
  1. # nano nginx-site.conf

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

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

Nginx 配置

Nginx 配置

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

 
  1. # nano supervisord.conf

然后,添加以下行。

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

Supervisord 配置

Supervisord 配置

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

5. 构建 WordPress 容器

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

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

构建 WordPress 容器

构建 WordPress 容器

6. 运行 WordPress 容器

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

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

运行 WordPress Docker

运行 WordPress Docker

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

 
  1. # echo "$(docker logs $CID )"

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

 
  1. # docker ps

docker 状态

docker 状态

7. Web 界面

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

启动Wordpress

启动Wordpress

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

Wordpress 欢迎界面

Wordpress 欢迎界面

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

wordpress 登录

wordpress 登录

总结

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


原文发布时间为:2015-06-03

本文来自云栖社区合作伙伴“Linux中国”

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
Java Go 开发者
Docker容器技术简介及其与Go语言的结合点
【2月更文挑战第23天】本文首先概述了Docker容器技术的核心概念和优势,接着探讨了Go语言与Docker容器技术的结合点。通过阐述Docker的轻量级、可移植性和版本控制等特性,以及Go语言在容器化应用中的优势,本文旨在说明两者结合能够实现更高效、灵活的应用开发和部署。
|
1月前
|
Oracle 关系型数据库 数据库
|
2天前
|
存储 Kubernetes Docker
Kubernetes(K8S)集群管理Docker容器(概念篇)
Kubernetes(K8S)集群管理Docker容器(概念篇)
|
2天前
|
存储 Ubuntu 安全
Docker容器常用命令
Docker容器常用命令
10 1
|
8天前
|
存储 运维 监控
构建高效稳定的Docker容器监控体系
【4月更文挑战第18天】 在现代微服务架构中,Docker容器已成为部署和运行应用的标准环境。随之而来的挑战是如何有效监控这些容器的性能与健康状况,确保系统的稳定性和可靠性。本文将探讨构建一个高效稳定的Docker容器监控体系的关键技术和方法,包括日志管理、性能指标收集以及异常检测机制,旨在为运维人员提供实用的指导和建议。
13 0
|
17天前
|
Linux Docker 容器
docker 容器常用命令
docker 容器常用命令
13 0
|
17天前
|
Linux Shell 虚拟化
linux 部署docker容器虚拟化平台(二)--------docker 镜像制作方法
linux 部署docker容器虚拟化平台(二)--------docker 镜像制作方法
28 0
|
17天前
|
存储 Linux Shell
centos 部署docker容器 安装 、基本使用方法(一)
centos 部署docker容器 安装 、基本使用方法(一)
32 0
|
20天前
|
关系型数据库 MySQL 数据安全/隐私保护
使用docker快速搭建wordpress服务,并指定域名访问
通过以上步骤,你可以使用Docker快速搭建WordPress服务,并通过指定的域名进行访问。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
22 1
|
25天前
|
Kubernetes 网络协议 Docker
Docker 容器的DNS
Docker 容器的DNS
28 1