search
HomeOperation and MaintenanceNginxHow to build Nginx+PHP+MySQL environment with Docker and deploy WordPress

准备工作

 使用阿里云kelude

到  创建一个项目,如dockerfile。之后我们把wordpress环境的所有相关dockerfile及配置文件放置到centosbz目录。

使用阿里云镜像仓库

阿里云docker镜像仓库地址为  ,用来存放docker镜像,可以在本地push镜像上去,也可以从kelude拉取dockerfile自动构建镜像。我们先登录,然后新建一个namespace,如centos-bz,之后所有的nginx,php,mysql镜像将存放在这个namespace下。

安装docker-compose

需要在运行docker容器的主机上安装docker-compose,可以参照官方文档手动安装,也可以使用ezhttp的一键安装工具(推荐)安装。如:

wget centos.bz/ezhttp.zip
 unzip ezhttp.zip
 cd ezhttp-master
 ./start.sh

之后会弹出一个菜单,输入2选择some useful tools,然后输入18选择安装docker和compose。

编写dockerfile

clone以上在阿里云kelude创建的dockerfile镜像到本地,在此项目中创建centos.bz,然后在centos.bz目录分别创建mysql,nginx,php目录,用于存放它们各自dockerfile及配置文件。

这里我们还约定以下目录:

/home/docker/nginx/logs/centos.bz:存放www.centos.bz网站的日志

/home/docker/nginx/www/centos.bz: 存放www.centos.bz网站的文件

/home/docker/php: 存放php-fpm的日志

/home/docker/mysql:mysql data目录

nginx dockerfile

在nginx目录创建dockerfile文件,写入如下内容:

# 从debian:jessie镜像基础上安装nginx
from debian:jessie
 
# 声明此dockerfile维护者的邮箱,有什么问题可以发到此邮件寻问
label maintainer "admin@centos.bz"
 
# 定义软件版本及编译工具变量
env nginx_version 1.10.3
env openssl_version 1.0.2h
env zlib_version 1.2.11
env pcre_version 8.40
env concat_version 1.2.2
env build_tools wget gcc make g++
env src_dir /opt/nginx
 
# 切换到工作目录
workdir ${src_dir}
 
# 开始编译nginx,我们这里使用编译安装nginx而不是使用官方提供的nginx镜像是因为这里使用到了第三方的concat模块,只能编译了。
# 把所有的安装命令都写在一个run指令中是因为这样可以减小镜像层数,缩减镜像大小。推荐使用反斜杠和&&把所有的安装命令放置到一行中。
run apt-get update \
 && apt-get -y --no-install-recommends install ca-certificates ${build_tools} \
 && wget http://nginx.org/download/nginx-${nginx_version}.tar.gz \
 && wget https://www.openssl.org/source/openssl-${openssl_version}.tar.gz \
 && wget http://www.zlib.net/zlib-${zlib_version}.tar.gz \
 && wget https://ftp.pcre.org/pub/pcre/pcre-${pcre_version}.tar.gz \
 && wget https://github.com/alibaba/nginx-http-concat/archive/${concat_version}.tar.gz -o nginx-http-concat-${concat_version}.tar.gz \
 && tar xf nginx-${nginx_version}.tar.gz \
 && tar xf openssl-${openssl_version}.tar.gz \
 && tar xf zlib-${zlib_version}.tar.gz \
 && tar xf pcre-${pcre_version}.tar.gz \
 && tar xf nginx-http-concat-${concat_version}.tar.gz \
 && cd nginx-${nginx_version} \
 && ./configure --prefix=/usr/local/nginx --with-pcre=../pcre-${pcre_version} \
    --with-zlib=../zlib-${zlib_version} \
    --with-http_ssl_module \
    --with-openssl=../openssl-${openssl_version} \
    --add-module=../nginx-http-concat-${concat_version} \
 && make -j$(nproc) \
 && make install \
 && rm -rf ${src_dir} \
 && apt-get purge -y --auto-remove ${build_tools} \
 && rm -rf /var/lib/apt/lists/*
 
# 把构建上下文目录conf,即dockerfile/centos.bz/nginx/conf目录下的文件复制到容器的/usr/local/nginx/conf目录。
copy conf/ /usr/local/nginx/conf/
 
# 定义启动容器时运行的命令
entrypoint ["/usr/local/nginx/sbin/nginx"]
 
expose 80 443

对于conf目录下的nginx配置文件,需要把日志,网站目录更改为以下约定的目录位置。

php-fpm dockerfile

创建dockerfile/centos.bz/php-fpm目录,在此目录下创建dockerfile文件,内容如下:

from debian:jessie
label maintainer "admin@centos.bz"
 
# 定义软件版本,编译工具,依赖等变量
env php_version 5.6.30
env build_tools m4 \
    autoconf \
    autoconf2.13 \
    openssl \
    wget \
    gcc \
    make
 
env build_deps libcurl4-gnutls-dev \
    libxml2-dev \
    zlib1g-dev \
    libpcre3-dev \
    libjpeg-dev \
    libpng12-dev \
    libfreetype6-dev \
    libmhash-dev \
    libmcrypt-dev \
    libssl-dev \
    libtool
 
env php_location /usr/local/php
env build_arg --prefix=${php_location} \
    --with-config-file-path=${php_location}/etc \
    --enable-fpm \
    --enable-bcmath \
    --with-pdo_sqlite \
    --with-gettext \
    --with-iconv \
    --enable-ftp \
    --with-sqlite3 \
    --enable-mbstring \
    --enable-sockets \
    --enable-zip \
    --enable-soap \
    --with-openssl \
    --with-zlib \
    --with-curl \
    --with-gd \
    --with-jpeg-dir \
    --with-png-dir \
    --with-freetype-dir \
    --with-mcrypt \
    --with-mhash \
    --with-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --without-pear \
    --with-libdir=lib64 \
    --enable-opcache \
    --disable-cgi
 
env src_dir /opt/php
 
workdir ${src_dir}
 
# 开始编译安装php
run apt-get update \
 && apt-get -y --no-install-recommends install ${build_deps} ${build_tools} \
 && wget http://php.net/distributions/php-${php_version}.tar.gz \
 && tar xf php-${php_version}.tar.gz \
 && cd php-${php_version} \
 && ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/libssl.so \
 && ln -s /usr/lib /usr/lib64 \
 && ./configure ${build_arg} \
 && make -j$(nproc) \
 && make install \
 && cp php.ini-production ${php_location}/etc/php.ini \
 && cp ${php_location}/etc/php-fpm.conf.default ${php_location}/etc/php-fpm.conf \
 && rm -rf ${src_dir} \
 && apt-get purge -y --auto-remove ${build_tools} \
 && rm -rf /var/lib/apt/lists/*
 
 
workdir ${php_location}/etc/
 
# 配置php-fpm,即使用sed工具编辑php-fpm.conf和php.ini文件,这里的php-fpm相关配置命令不与上面的编译命令合在一起来减小层数是因为
# 配置文件可能会改动比较多,这样分开当配置文件更改时可以直接使用缓存跳过编译步骤,加快构建速度。
run set_php_variable(){ \
  local key=$1; \
  local value=$2; \
  if grep -q -e "^$key\s*=" php.ini;then \
   sed -i -r "s#^$key\s*=.*#$key=$value#" php.ini; \
  else \
   sed -i -r "s#;\s*$key\s*=.*#$key=$value#" php.ini; \
  fi; \
  if ! grep -q -e "^$key\s*=" php.ini;then \
   echo "$key=$value" >> php.ini; \
  fi; \
 } \
 && base_dir=/home/docker/php \
 && set_php_variable disable_functions "dl,eval,assert,exec,popen,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open" \
 && set_php_variable expose_php off \
 && set_php_variable error_log ${base_dir}/php_errors.log \
 && set_php_variable request_order "cgp" \
 && set_php_variable cgi.fix_pathinfo 0 \
 && set_php_variable short_open_tag on \
 && set_php_variable date.timezone asia/chongqing \
 && sed -i 's/^user =.*/user = www-data/' php-fpm.conf \
 && sed -i 's/^group =.*/group = www-data/' php-fpm.conf \
 && sed -i "s#;slowlog = log/\$pool.log.slow#slowlog = ${base_dir}/\$pool.log.slow#" php-fpm.conf \
 && sed -i 's/;request_slowlog_timeout = 0/request_slowlog_timeout = 5/' php-fpm.conf \
 && sed -i 's/^pm.max_children.*/pm.max_children =20/' php-fpm.conf \
 && sed -i 's/^pm.start_servers.*/pm.start_servers =5/' php-fpm.conf \
 && sed -i 's/^pm.min_spare_servers.*/pm.min_spare_servers =3/' php-fpm.conf \
 && sed -i 's/^pm.max_spare_servers.*/pm.max_spare_servers =8/' php-fpm.conf \
 && sed -i '/\[global\]/a\daemonize =no' php-fpm.conf \
 && sed -i 's/^listen.*/listen =0.0.0.0:9000/' php-fpm.conf \
 && echo "[opcache]\n \
   zend_extension=opcache.so\n \
   opcache.memory_consumption=128\n \
   opcache.interned_strings_buffer=8\n \
   opcache.max_accelerated_files=4000\n \
   opcache.revalidate_freq=60\n \
   opcache.fast_shutdown=1 \n" >> php.ini
 
entrypoint ["/usr/local/php/sbin/php-fpm"]
 
expose 9000

mysql dockerfile

创建dockerfile/centos.bz/mysql/dockerfile文件,内容如下:

from mysql:5.6
label maintainer "admin@centos.bz"
copy my.cnf /etc/mysql/my.cnf

这个dockerfile非常简单,直接使用了官方的mysql镜像,唯一区别是我们使用自己定义的my.cnf配置文件。

对于my.cnf配置文件,需要把日志,data目录指向/home/docker/mysql,一个my.cnf示例文件如下:

# generated by ezhttp at 2016-02-03 01:05:29
 
[mysql]
 
# client #
port       = 3306
socket       = /home/docker/mysql/mysql.sock
 
[mysqld]
 
# general #
port       = 3306
user       = mysql
default-storage-engine   = innodb
socket       = /home/docker/mysql/mysql.sock
pid-file      = /home/docker/mysql/mysql.pid
skip-name-resolve
 
# myisam #
key-buffer-size    = 32m
 
# innodb #
#innodb-flush-method   = o_direct
innodb-log-files-in-group  = 2
innodb-log-file-size   = 64m
innodb-flush-log-at-trx-commit = 2
innodb-file-per-table   = 1
innodb-buffer-pool-size  = 1g
 
# caches and limits #
tmp-table-size     = 32m
max-heap-table-size   = 32m
query-cache-type    = 0
query-cache-size    = 0
max-connections    = 300
thread-cache-size    = 50
open-files-limit    = 1024
table-definition-cache   = 100
table-open-cache    = 400
 
 
# safety #
max-allowed-packet    = 16m
max-connect-errors    = 1000000
 
# data storage #
datadir      = /home/docker/mysql
 
# logging #
log-error      = /home/docker/mysql/mysql-error.log
log-queries-not-using-indexes = 1
slow-query-log     = 1
slow-query-log-file   = /home/docker/mysql/mysql-slow.log
 
# binary logging #
log-bin = /home/docker/mysql/mysql-bin
server-id = 1
expire-logs-days = 14
sync-binlog = 1

构建镜像

把上一步创建的文件推送到阿里云的kelude。然后我们登录阿里云的docker镜像仓库cr.console.aliyun.com。这里以设置自动构建nginx镜像为例,php和mysql镜像构建设置类似。

1.点击左侧“镜像列表”,在右侧点击仓库镜像,如图:

How to build Nginx+PHP+MySQL environment with Docker and deploy WordPress

2.在仓库镜像创建对话框中,说明如下:

地域:选择离部署docker主机最近的位置,国内的话选择华东1或华东2。

namespace和仓库名称:这里选择centos-bz,nginx。

设置代码源:我们这里选择阿里云code。

构建设置:勾选代码变更时自动构建镜像,海外机器构建(因为国内主机apt-get安装软件时较慢),dockerfile路径填/centos.bz/nginx

完成后点击创建仓库按钮。

如图:

How to build Nginx+PHP+MySQL environment with Docker and deploy WordPress

3.回到镜像列表,找到nginx镜像,点击管理。

4.左侧点击“构建”,右侧点击“立即构建”开始首次构建,之后我们更改dockerfile及配置文件到kelude之后就会自动构建了。

5.查看日志,查看构建进程。

然后继续完成php,mysql的镜像构建设置。

启动环境

为了方便统一管理nginx,php,mysql的启动,我们使用docker-compose工具。我们只需要编写一个docker-compose.yml文件,然后使用docker-compose工具就可以快速启动docker容器了。之后把docker-compose.yml传输到任意一台支持docker环境的主机中就可以快速配置wordpress的运行环境。

docker-compose.yml

把docker-compose.yml文件放置在/home/docker目录下。

version: '3'
# 定义三个服务nginx,php,mysql
services:
 nginx:
  # 依赖php服务,意味着在启动nginx之前先启动php
  depends_on:
   - php
  # nginx镜像的路径
  image: registry.cn-hangzhou.aliyuncs.com/centos-bz/nginx
  # 容器的/home/docker/nginx目录挂载主机中的/home/docker/nginx目录,
  # 这样使nginx容器把网站文件和目录存放到主机目录中,持久化和方便管理
  volumes:
   - /home/docker/nginx:/home/docker/nginx
  # nginx意外退出时自动重启
  restart: always
 
  # 映射80和443端口
  ports:
   - "80:80"
   - "443:443"
 
  # 容器名称
  container_name: nginx 
 php:
  depends_on:
   - mysql
  image: registry.cn-hangzhou.aliyuncs.com/centos-bz/php-fpm
  restart: always
  volumes:
   - /home/docker/nginx/www:/home/docker/nginx/www
   - /home/docker/php:/home/docker/php
  container_name: php 
 
 mysql:
  image: registry.cn-hangzhou.aliyuncs.com/centos-bz/mysql
  volumes:
   - /home/docker/mysql:/home/docker/mysql
  restart: always
  # 设置mysql_root_password环境变量,这里是设置mysql的root密码。这里为root。
  environment:
   mysql_root_password: root
  container_name: mysql

启动环境

在/home/docker目录执行:

docker-compose up

查看nginx,php,mysql是否正常启动,如果正常,ctrl-c停止,再执行:

docker-compose up -d

这里compose命令就在后台启动了。

执行docker ps查看容器运行状态。

连接问题

容器之间可以通过容器名称来连接,如nginx配置文件中连接php的代码fastcgi_pass php:9000,网站数据库配置文件使用mysql:3306。

日常运维

 迁移

比如a主机迁移到b主机。只需要三步。

1.打包a主机的/home/docker目录,传输到b主机相同位置

2.配置b主机docker环境

3.在b主机的/home/docker目录下执行docker-compose up -d

导出导入数据库

把centos.sql.gz数据库文件导入到centos数据库:

gunzip < centos.sql.gz | docker exec -i mysql mysql -uroot -proot centos

把centos数据库导出到centos.sql.gz

docker exec -i mysql mysqldump -uroot -proot centos | gzip > centos.sql.gz

备份

推荐使用ezhttp一键备份设置:

wget centos.bz/ezhttp.zip
unzip ezhttp.zip
cd ezhttp-master
./start.sh

之后会弹出一个菜单,输入2选择some useful tools,然后输入14选择备份设置。需要注意的是在设置mysql使用mysqldump备份时,在提示输入mysql bin directory时,输入docker exec /usr/bin/。

The above is the detailed content of How to build Nginx+PHP+MySQL environment with Docker and deploy WordPress. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX's Key Features: Performance, Scalability, and SecurityNGINX's Key Features: Performance, Scalability, and SecurityApr 13, 2025 am 12:09 AM

NGINX improves performance through its event-driven architecture and asynchronous processing capabilities, enhances scalability through modular design and flexible configuration, and improves security through SSL/TLS encryption and request rate limiting.

NGINX vs. Apache: Web Hosting and Traffic ManagementNGINX vs. Apache: Web Hosting and Traffic ManagementApr 12, 2025 am 12:04 AM

NGINX is suitable for high concurrency and low resource consumption scenarios, while Apache is suitable for scenarios that require complex configurations and functional extensions. 1.NGINX is known for handling large numbers of concurrent connections with high performance. 2. Apache is known for its stability and rich module support. When choosing, it must be decided based on specific needs.

NGINX: The Versatile Tool for Modern Web ApplicationsNGINX: The Versatile Tool for Modern Web ApplicationsApr 11, 2025 am 12:03 AM

NGINXisessentialformodernwebapplicationsduetoitsrolesasareverseproxy,loadbalancer,andwebserver,offeringhighperformanceandscalability.1)Itactsasareverseproxy,enhancingsecurityandperformancebycachingandloadbalancing.2)NGINXsupportsvariousloadbalancingm

Nginx SSL/TLS Configuration: Securing Your Website with HTTPSNginx SSL/TLS Configuration: Securing Your Website with HTTPSApr 10, 2025 am 09:38 AM

To ensure website security through Nginx, the following steps are required: 1. Create a basic configuration, specify the SSL certificate and private key; 2. Optimize the configuration, enable HTTP/2 and OCSPStapling; 3. Debug common errors, such as certificate path and encryption suite issues; 4. Application performance optimization suggestions, such as using Let'sEncrypt and session multiplexing.

Nginx Interview Questions: Ace Your DevOps/System Admin InterviewNginx Interview Questions: Ace Your DevOps/System Admin InterviewApr 09, 2025 am 12:14 AM

Nginx is a high-performance HTTP and reverse proxy server that is good at handling high concurrent connections. 1) Basic configuration: listen to the port and provide static file services. 2) Advanced configuration: implement reverse proxy and load balancing. 3) Debugging skills: Check the error log and test the configuration file. 4) Performance optimization: Enable Gzip compression and adjust cache policies.

Nginx Caching Techniques: Improving Website PerformanceNginx Caching Techniques: Improving Website PerformanceApr 08, 2025 am 12:18 AM

Nginx cache can significantly improve website performance through the following steps: 1) Define the cache area and set the cache path; 2) Configure the cache validity period; 3) Set different cache policies according to different content; 4) Optimize cache storage and load balancing; 5) Monitor and debug cache effects. Through these methods, Nginx cache can reduce back-end server pressure, improve response speed and user experience.

Nginx with Docker: Deploying and Scaling Containerized ApplicationsNginx with Docker: Deploying and Scaling Containerized ApplicationsApr 07, 2025 am 12:08 AM

Using DockerCompose can simplify the deployment and management of Nginx, and scaling through DockerSwarm or Kubernetes is a common practice. 1) Use DockerCompose to define and run Nginx containers, 2) implement cluster management and automatic scaling through DockerSwarm or Kubernetes.

Advanced Nginx Configuration: Mastering Server Blocks & Reverse ProxyAdvanced Nginx Configuration: Mastering Server Blocks & Reverse ProxyApr 06, 2025 am 12:05 AM

The advanced configuration of Nginx can be implemented through server blocks and reverse proxy: 1. Server blocks allow multiple websites to be run in one instance, each block is configured independently. 2. The reverse proxy forwards the request to the backend server to realize load balancing and cache acceleration.

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)