Home Backend Development PHP Tutorial 再Docker中架设完整的WordPress站点全攻略_PHP

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

May 30, 2016 am 08:46 AM
docker wordpress

Docker

1. 安装 Docker

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

  # yum install docker
Copy after login

201572995512549.png (684×490)

    # systemctl restart docker.service

Copy after login

2. 创建 WordPress 的 Dockerfile

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

  # nano Dockerfile

Copy after login

然后,我们需要将下面的配置行添加到 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"]

Copy after login

201572995556444.png (684×490)

3. 创建启动脚本

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

  # nano start.sh

Copy after login

打开 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

Copy after login

201572995630747.png (684×490)

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

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

  # nano nginx-site.conf

Copy after login

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

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;
  #}
  }
  }

Copy after login

201572995707457.png (684×490)

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

  # nano supervisord.conf

Copy after login

然后,添加以下行。

  [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
Copy after login

201572995736091.png (684×490)

添加完后,保存并关闭文件。
5. 构建 WordPress 容器

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

  # docker build --rm -t wordpress:centos7 .
Copy after login

201572995803992.png (684×490)

6. 运行 WordPress 容器

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

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

201572995845827.png (593×38)

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

 # echo "$(docker logs $CID )"
Copy after login

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

  # docker ps


Copy after login

201572995909669.png (631×104)

    7. Web 界面

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

201572995934305.png (1380×722)

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

201572995957637.png (1380×722)

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

2015729100028673.png (1380×722)

总结

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP vs. Flutter: The best choice for mobile development PHP vs. Flutter: The best choice for mobile development May 06, 2024 pm 10:45 PM

PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

How to package project with pycharm How to package project with pycharm Apr 25, 2024 am 03:54 AM

There are four ways to package a project in PyCharm: Package as a separate executable file: Export to EXE single file format. Packaged as an installer: Generate Setuptools Makefile and build. Package as a Docker image: specify an image name, adjust build options, and build. Package as a container: Specify the image to build, adjust runtime options, and start the container.

Docker completes local deployment of LLama3 open source large model in three minutes Docker completes local deployment of LLama3 open source large model in three minutes Apr 26, 2024 am 10:19 AM

Overview LLaMA-3 (LargeLanguageModelMetaAI3) is a large-scale open source generative artificial intelligence model developed by Meta Company. It has no major changes in model structure compared with the previous generation LLaMA-2. The LLaMA-3 model is divided into different scale versions, including small, medium and large, to suit different application needs and computing resources. The parameter size of small models is 8B, the parameter size of medium models is 70B, and the parameter size of large models reaches 400B. However, during training, the goal is to achieve multi-modal and multi-language functionality, and the results are expected to be comparable to GPT4/GPT4V. Install OllamaOllama is an open source large language model (LL

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

Agile development and operation of PHP microservice containerization Agile development and operation of PHP microservice containerization May 08, 2024 pm 02:21 PM

Answer: PHP microservices are deployed with HelmCharts for agile development and containerized with DockerContainer for isolation and scalability. Detailed description: Use HelmCharts to automatically deploy PHP microservices to achieve agile development. Docker images allow for rapid iteration and version control of microservices. The DockerContainer standard isolates microservices, and Kubernetes manages the availability and scalability of the containers. Use Prometheus and Grafana to monitor microservice performance and health, and create alarms and automatic repair mechanisms.

PHP distributed system architecture and practice PHP distributed system architecture and practice May 04, 2024 am 10:33 AM

PHP distributed system architecture achieves scalability, performance, and fault tolerance by distributing different components across network-connected machines. The architecture includes application servers, message queues, databases, caches, and load balancers. The steps for migrating PHP applications to a distributed architecture include: Identifying service boundaries Selecting a message queue system Adopting a microservices framework Deployment to container management Service discovery

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

How to optimize the performance of Java functions through containerization? How to optimize the performance of Java functions through containerization? Apr 29, 2024 pm 03:09 PM

Containerization improves Java function performance in the following ways: Resource isolation - ensuring an isolated computing environment and avoiding resource contention. Lightweight - takes up less system resources and improves runtime performance. Fast startup - reduces function execution delays. Consistency - Decouple applications and infrastructure to ensure consistent behavior across environments.

See all articles