前言
随着云计算和容器化技术的快速发展,使用 Docker Compose 编排 LNMP 环境已经成为快速部署 Web 应用程序的一种流行方式。LNMP 环境由 Linux、Nginx、MySQL 和 PHP 组成,为运行 Web 应用提供了稳定的基础。本文将介绍如何通过 Docker Compose 编排 LNMP 环境,并利用该环境顺利完成 WordPress 的部署和配置。
编写 Dockerfile 部署 LNMP 请参考:Docker构建LNMP部署WordPress-CSDN博客
目录
docker-compose%20%E7%BC%96%E6%8E%92%20lnmp-toc" style="margin-left:0px;">一、docker-compose 编排 lnmp
1. 环境准备
2. 准备 mysql 容器目录及相关文件
2.1 进入工作目录
2.2 编写 Dockerfile 文件
2.3 准备 my.cnf 文件
3. 准备 php 容器目录及相关文件
3.1 进入工作目录
3.2 编写 Dockerfile 文件
3.3 准备 php.ini、php-fpm.conf、www.conf 配置文件
3.3.1 php.ini
3.3.2 php-fpm.conf
3.3.3 www.conf
docker-compose.yml%20%E6%96%87%E4%BB%B6-toc" style="margin-left:40px;">4. 编辑 docker-compose.yml 文件
5. 下载 Docker Compose 并安装
6. 查看工作目录清理历史数据
7. 启动服务
8. 进入容器登录 mysql 授权
9. 创建一个新的配置文件 wp-config.php
10. 浏览器访问 wordpress
二、错误总结
1. php 无法获取指定镜像
2. 建议保证 nginx、mysql 服务优先启动于 php
3. 访问 wordpress/index.php 提示 File not found.
4. yum 安装依赖包失败
5. 其他建议
docker-compose%20%E7%BC%96%E6%8E%92%20lnmp" style="background-color:transparent;">一、docker-compose 编排 lnmp
1. 环境准备
容器名 | 宿主机系统/配置 | IP地址 | Docker版本 | 基于镜像 | 安装软件 |
---|---|---|---|---|---|
nginx | Centos 7.4;8C/8G | 172.19.8.10 | 20.10.17 | centos:7 | Docker-NginxDocker-Wordpress |
mysql | Centos 7.4;8C/8G | 172.19.8.20 | 20.10.17 | centos:7 | Docker-Mysql |
php | Centos 7.4;8C/8G | 172.19.8.30 | 20.10.17 | centos:7 | Docker-php |
nginx 容器构建请参考:Docker部署nginx并启用https加密连接-CSDN博客
2. 准备 mysql 容器目录及相关文件
2.1 进入工作目录
[root@localhost ~]# cd /lnmp/
[root@localhost lnmp]# mkdir mysql
[root@localhost lnmp]# cd mysql/
[root@localhost mysql]# ls
mysql-boost-5.7.20.tar.gz # 准备安装包
2.2 编写 Dockerfile 文件
[root@localhost mysql]# vim Dockerfile
FROM centos:7
MAINTAINER mysql image <fql>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/ # 将MySQL源代码包解压缩到镜像/usr/local/src/目录下
WORKDIR /usr/local/src/mysql-5.7.20/ # 设置工作目录为MySQL源代码包的目录
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j 8 && make install
ADD my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH # 将MySQL的可执行文件路径添加到环境变量中
RUN useradd -M -s /sbin/nologin mysql
RUN chown mysql:mysql /etc/my.cnf
RUN chown -R mysql:mysql /usr/local/mysql/
RUN /usr/local/mysql/bin/mysqld \ # 运行MySQL的初始化命令,初始化MySQL数据库
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"] # 启动MySQL数据库服务
2.3 准备 my.cnf 文件
[root@localhost mysql]# cat my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
3. 准备 php 容器目录及相关文件
3.1 进入工作目录
[root@localhost mysql]# cd ..
[root@localhost lnmp]# mkdir php
[root@localhost lnmp]# cd php/
[root@localhost php]# ls
php-7.1.10.tar.bz2 # 准备安装包
3.2 编写 Dockerfile 文件
[root@localhost php]# vim Dockerfile
FROM centos:7
MAINTAINER php image <fql>
RUN yum -y install gd \ # 安装一系列依赖包
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel
RUN useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/ # 将php文件解压并复制到/usr/local/src/目录下
WORKDIR /usr/local/src/php-7.1.10 # 设置工作目录
RUN ./configure \ # 配置PHP编译选项
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j 8 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH # 将PHP可执行文件路径添加到系统PATH中
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000 # 暴露容器的9000端口,用于PHP-FPM服务
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ] # 启动PHP-FPM服务并以守护进程方式运行
3.3 准备 php.ini、php-fpm.conf、www.conf 配置文件
3.3.1 php.ini
[root@localhost php]# vim php.ini939 date.timezone = Asia/Shanghai # 取消注释,修改
1170 mysqli.default_socket = /usr/local/mysql/mysql.sock
# MySQL数据库的默认socket文件路径,用于PHP连接MySQL数据库时寻找MySQL服务器的通信端口
[root@localhost php]# egrep -v "^;" php.ini | egrep -v "^$"
# 显示php.ini文件中的非注释和非空行内容,即显示出配置文件中的有效配置项
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket = /usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]
3.3.2 php-fpm.conf
[root@localhost php]# vim php-fpm.conf17 pid = run/php-fpm.pid # 删除注释符号“;”
[root@localhost php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
3.3.3 www.conf
[root@localhost php]# vim www.conf23 user = nginx24 group = nginx36 listen = 172.19.0.30:9000 # 修改监听IP和端口为容器IP:9000端口
[root@localhost php]# egrep -v "^;" www.conf | egrep -v "^$"
[www]
user = nginx
group = nginx
listen = 172.19.0.30:9000
listen.allowed_clients = 127.0.0.1,172.19.0.10
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
docker-compose.yml%20%E6%96%87%E4%BB%B6">4. 编辑 docker-compose.yml 文件
[root@localhost php]# cd ..
[root@localhost lnmp]# ls
mysql nginx php
[root@localhost lnmp]# vim docker-compose.yml
version: '2'
services:nginx:container_name: nginx01 # 指定容器的名称为nginx01hostname: nginx-host # 容器的主机名为nginx-hostimage: nginx:lnmp # 使用的镜像为nginx:lnmpbuild: # 构建上下文和Dockerfile的位置context: ./nginxdockerfile: Dockerfileports: # 将宿主机的80端口映射到容器的80端口,将宿主机的443端口映射到容器的443端口- 80:80- 443:443volumes: # 将/usr/local/nginx/html目录设为共享挂载点- /usr/local/nginx/htmlnetworks: # 将该服务连接到名为lnmp的网络,并指定了容器的IPv4地址lnmp:ipv4_address: 172.19.0.10mysql: container_name: mysql01 # 容器的名称为mysql01hostname: mysql-host # 容器的主机名为mysql-hostimage: mysql:lnmp # 使用的镜像为mysql:lnmpprivileged: true # 使容器内的root拥有真正的root权限build: # 构建上下文和Dockerfile的位置context: ./mysql dockerfile: Dockerfileports: # 将宿主机的3306端口映射到容器的3306端口- 3306:3306volumes: # 创建容器共享挂载目录/usr/local/mysql - /usr/local/mysqlnetworks:lnmp:ipv4_address: 172.19.0.20php:container_name: php # 容器的名称为phphostname: php-host # 容器的主机名为php-hostimage: php:lnmp # 使用的镜像为php:lnmpbuild: # 指定了构建上下文和Dockerfile的位置context: ./phpdockerfile: Dockerfileports: # 将宿主机的9000端口映射到容器的9000端口- 9000:9000depends_on: # 声明了该服务依赖于nginx和mysql服务- nginx- mysqlvolumes_from: # 从nginx和mysql服务中挂载卷- nginx- mysqlnetworks:lnmp:ipv4_address: 172.19.0.30
networks: # 定义了一个叫做lnmp的网络,使用了bridge驱动lnmp:driver: bridgeipam:config: # 指定了IP地址池的子网范围- subnet: 172.19.0.0/16
5. 下载 Docker Compose 并安装
[root@localhost ~]# curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# `uname -s`-`uname -m`也可以写成$(uname -s) 和 $(uname -m):分别会自动替换为你的系统类型(如:Linux)和机器架构(如:x86_64),确保你下载的是适合你系统的版本。
# curl -L:这部分表示使用curl命令进行下载,并且使用-L参数来跟随重定向
# -o /usr/local/bin/docker-compose: 表示将下载的文件保存为/usr/local/bin目录下的docker-compose文件
[root@localhost bin]# pwd
/usr/local/bin
[root@localhost bin]# ls
docker-compose
[root@localhost bin]# chmod +x /usr/local/bin/docker-compose
[root@localhost bin]# ll
总用量 10616
-rwxr-xr-x. 1 root root 10867152 5月 3 16:34 docker-compose
[root@localhost bin]# docker-compose --version # 查看版本
docker-compose version 1.21.1, build 5a3f1a3
6. 查看工作目录清理历史数据
[root@localhost lnmp]# tree -L 3
.
├── docker-compose.yml
├── mysql
│ ├── Dockerfile
│ ├── my.cnf
│ └── mysql-boost-5.7.20.tar.gz
├── nginx
│ ├── cert
│ │ ├── ca-key.pem
│ │ ├── ca.pem
│ │ ├── ca.srl
│ │ ├── server-cert.pem
│ │ ├── server.csr
│ │ └── server-key.pem
│ ├── Dockerfile
│ ├── html
│ │ ├── index.html
│ │ └── wordpress
│ ├── nginx-1.24.0.tar.gz
│ ├── nginx.conf
│ └── wordpress-4.9.4-zh_CN.tar.gz
└── php├── Dockerfile├── php-7.1.10.tar.bz2├── php-fpm.conf├── php.ini└── www.conf
# 删除原镜像、容器、网络、数据卷:docker-compose -f docker-compose-yml down
建议单独删,别把有用的文件一块删了:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx centos a5f6eea7f002 6 hours ago 623MB
nginx latest 7383c266ef25 9 days ago 188MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dca9b540b422 nginx:centos "/usr/local/nginx/sb…" 6 hours ago Up 6 hours 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp n1
[root@localhost ~]# docker rm -f dca9b540b422
dca9b540b422
[root@localhost ~]# docker rmi -f a5f6eea7f002
[root@localhost ~]# iptables -F
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl restart network
7. 启动服务
[root@localhost ~]# cd /lnmp/
[root@localhost lnmp]# ls
docker-compose.yml mysql nginx php
[root@localhost lnmp]# docker-compose -f docker-compose.yml up -d
Creating mysql01 ... done
Creating nginx01 ... done
Creating php ... done
[root@localhost lnmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php lnmp 2c776b3060db About a minute ago 1.41GB
mysql lnmp 291e964e74d3 8 minutes ago 10.1GB
nginx lnmp dc1695c1c244 26 minutes ago 651MB
nginx latest 7383c266ef25 9 days ago 188MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
[root@localhost lnmp]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e2c69e85e86 php:lnmp "/usr/local/php/sbin…" 15 seconds ago Up 14 seconds 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
bd45ba2ccaeb mysql:lnmp "/usr/local/mysql/bi…" 23 seconds ago Up 15 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql01
6dc9248104d1 nginx:lnmp "/usr/local/nginx/sb…" 23 seconds ago Up 15 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp nginx01
8. 进入容器登录 mysql 授权
[root@localhost lnmp]# docker exec -it mysql01 /bin/bash
[root@mysql-host mysql-5.7.20]# mysql
mysql> create database wordpress;
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
mysql> flush privileges;
mysql> exit
Bye
[root@mysql-host mysql-5.7.20]# exit
9. 创建一个新的配置文件 wp-config.php
[root@localhost lnmp]# cd /lnmp/nginx/html/wordpress
[root@localhost wordpress]# ls
index.php wp-admin wp-content wp-load.php wp-signup.php
license.txt wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php
readme.html wp-comments-post.php wp-includes wp-mail.php xmlrpc.php
wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php
[root@localhost wordpress]# cp wp-config-sample.php wp-config.php
[root@localhost wordpress]# vim wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'wordpress');
/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');
# 修改WordPress数据库信息
10. 浏览器访问 wordpress
访问http://192.168.190.108/wordpress/index.php
二、错误总结
1. php 无法获取指定镜像
WARNING: Image for service mysql was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling php (php:lnmp)...
ERROR: manifest for php:lnmp not found: manifest unknown: manifest unknown
[root@localhost lnmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
排错:检查docker-compose.yml文件
build: # 指定了构建上下文和Dockerfile的位置context: ./phpdockerfile: Dockerfile
原因是缺少build
2. 建议保证 nginx、mysql 服务优先启动于 php
depends_on: # 声明了该服务依赖于nginx和mysql服务- nginx- mysql
3. 访问 wordpress/index.php 提示 File not found.
① 查看 Dockerfile 指定映射路径是否正确,一开始路径写成yum安装的share了
② 查看 Dockerfile 指定映射路径权限问题,777没毛病
③ 另外:最坑的
ADD html/ /usr/local/nginx/html/
居然不能把本地的 html/里面内容 拷贝到容器/usr/local/nginx/html/COPY html/ /usr/local/nginx/html/
尝试了修改为COPY;或者尝试直接ADD解压压缩包到容器;或者尝试在compose里面加valuesvolumes: # 将本地目录./lnmp/nginx/html/挂载到容器内的/usr/local/nginx/html目录- ./lnmp/nginx/html/:/usr/local/nginx/html
最后发现Dockerfile与composed定义的有冲突,修改为- /usr/local/nginx/html
4. yum 安装依赖包失败
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache
# 如果遇到类似dns问题,可修改dns、替换阿里源尝试解决
vim /etc/sysctl.conf
net.ipv4.ip_forward=1sysctl -p
systemctl restart network
systemctl restart docker
5. 其他建议
- 建议多核编译,提高效率
- 出现故障可分开排查,比如nginx有问题,可以删掉compose其他容器服务配置,单独检查,避免多次编译