当前位置: 首页 > article >正文

08. 基于docker-compose部署LNMP架构

目录

前言

1、docker

1.1 任务要求

1.2 关闭防火墙

1.3 安装docker

1.4 配置镜像加速下载

2、Nginx

2.1 建立工作目录并进行相关操作

2.2 准备 nginx.conf 配置文件

3、Mysql

3.1 建立工作目录并进行相关操作

3.2 编写 my.cnf 配置文件

4、PHP

4.1 建立工作目录并进行相关操作

4.2 准备 php.ini、php-fpm.conf、www.conf 配置文件

4.2.1 修改php.ini 配置文件

4.2.2 修改 php-fpm.conf 配置文件

4.2.3 修改www.conf 配置文件

5、Docker-Compose环境安装

5.1 安装docker-compose

5.2 docker-compose.yml 文件配置

5.3 lnmp Docker-Compose 文件结构

5.4 启动 Docker 容器

6、启动 wordpress 服务

6.1 mysql授权

6.2 创建wp-config.php文件

6.3 浏览器访问安装wordpress


前言

        LNMP架构是一种流行的Web服务架构,由Linux、Nginx、MySQL和PHP组成

        它提供了一个稳定、高性能的平台,适用于各种Web应用程序的部署

        本文将介绍如何使用docker-compose来部署LNMP架构,以简化部署流程,并提高整个架构的可管理性和可移植性

  • Linux:作为操作系统,提供了稳定的运行环境
  • Nginx:作为Web服务器,具有高性能和高并发处理能力,可以作为反向代理服务器,负责接收用户的请求并将请求转发给后端的应用服务器
  • MySQL:作为关系型数据库,提供了数据存储和管理的功能
  • PHP(或Python、Perl):作为服务器端脚本语言,用于处理用户的请求,生成动态的网页内容

1、docker

1.1 任务要求

任务说明:需要使用 docker-compose 编排 lnmp(dockerfile) ,完成并运行Wordpress 网站平台

宿主机

操作系统

IP地址

主要软件

配置要求

Docker

CentOS 7

10.4.7.11

Docker 26.1.4

4核8G以上

容器

 操作系统

IP地址

主要软件

nginx

CentOS 7

172.20.0.10

Docker-Nginx

mysql

CentOS 7

172.20.0.20

Docker-Mysql

php

CentOS 7

172.20.0.30

Docker-php

1.2 关闭防火墙

命令示例:关闭所有设备防火墙和临时防护

systemctl disable --now firewalld
setenforce  0

输出结果:

[root@MineGi ~]# systemctl disable --now firewalld
[root@MineGi ~]# setenforce  0
setenforce: SELinux is disabled
[root@MineGi ~]# 

1.3 安装docker

命令示例:查看虚拟机配置

cat /etc/redhat-release
free -h
lscpu |grep ^CPU\(

输出结果:

[root@MineGi ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@MineGi ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           7.8G        159M        7.5G         11M         96M        7.4G
Swap:            0B          0B          0B
[root@MineGi ~]# lscpu |grep ^CPU\(
CPU(s):                4
[root@MineGi ~]# 

命令示例:安装docker

rpm -qa docker-ce*
# yum remove docker-ce* -y
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce-26.1.4 docker-ce-cli-26.1.4 containerd.io
rpm -qa docker-ce*

输出结果:

[root@MineGi ~]# rpm -qa docker-ce*
[root@MineGi ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
--2024-12-23 13:14:43--  https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 106.8.245.232, 182.40.67.227, 220.181.175.236, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|106.8.245.232|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2081 (2.0K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/docker-ce.repo”

100%[=====================================================================================>] 2,081       --.-K/s 用时 0s      

2024-12-23 13:14:43 (140 MB/s) - 已保存 “/etc/yum.repos.d/docker-ce.repo” [2081/2081])

[root@MineGi ~]# yum install -y -q docker-ce-26.1.4 docker-ce-cli-26.1.4 containerd.io
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-buildx-plugin-0.14.1-1.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
docker-buildx-plugin-0.14.1-1.el7.x86_64.rpm 的公钥尚未安装
导入 GPG key 0x621E9F35:
 用户ID     : "Docker Release (CE rpm) <docker@docker.com>"
 指纹       : 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 来自       : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
setsebool:  SELinux is disabled.
[root@MineGi ~]# rpm -qa docker-ce*
docker-ce-cli-26.1.4-1.el7.x86_64
docker-ce-26.1.4-1.el7.x86_64
docker-ce-rootless-extras-26.1.4-1.el7.x86_64
[root@MineGi ~]# 

1.4 配置镜像加速下载

命令示例:

mkdir -p /etc/docker

cat >/etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://41q9iv61.mirror.aliyuncs.com",
    "https://docker.cloudmessage.top",
    "https://registry.docker-cn.com",
    "https://docker.nju.edu.cn",
    "https://registry.cn-hangzhou.aliyuncs.com",
    "https://mirror.ccs.tencentyun.com",
    "https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com",
    "http://hub-mirror.c.163.com",
    "http://f1361db2.m.daocloud.io"
  ]
}
EOF

systemctl daemon-reload
systemctl enable --now docker

输出结果:

[root@MineGi ~]# mkdir -p /etc/docker
[root@MineGi ~]# cat >/etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": [
>     "https://41q9iv61.mirror.aliyuncs.com",
>     "https://docker.cloudmessage.top",
>     "https://registry.docker-cn.com",
>     "https://docker.nju.edu.cn",
>     "https://registry.cn-hangzhou.aliyuncs.com",
>     "https://mirror.ccs.tencentyun.com",
>     "https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com",
>     "http://hub-mirror.c.163.com",
>     "http://f1361db2.m.daocloud.io"
>   ]
> }
> EOF
[root@MineGi ~]# systemctl daemon-reload
[root@MineGi ~]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@MineGi ~]# 

2、Nginx

【lnmp:百度网盘】

2.1 建立工作目录并进行相关操作

命令示例:

mkdir -p /opt/lnmp/nginx/html
wget -P /opt/lnmp/nginx http://nginx.org/download/nginx-1.12.0.tar.gz
wget -P /opt/lnmp/nginx/html https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz

cat >/opt/lnmp/nginx/Dockerfile <<'eof'
FROM centos:7
MAINTAINER this is nginx image <lnmp>
ADD nginx-1.12.0.tar.gz /usr/local/src/
ADD nginx.conf /usr/local/nginx/conf/
ADD html/wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html
WORKDIR /usr/local/src/nginx-1.12.0
RUN curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
    yum -y install pcre-devel zlib-devel gcc gcc-c++ make && yum clean all && \
    useradd -M -s /sbin/nologin nginx && \
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
    --with-http_stub_status_module;make -j 4 && make install && \
    chmod 777 -R /usr/local/nginx/html/
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
VOLUME [ "/usr/local/nginx/html/" ]
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]
eof

输出结果:

[root@MineGi ~]# mkdir -p /opt/lnmp/nginx/html
[root@MineGi ~]# wget -q -P /opt/lnmp/nginx http://nginx.org/download/nginx-1.12.0.tar.gz
[root@MineGi ~]# wget -q -P /opt/lnmp/nginx/html https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@MineGi ~]# cat >/opt/lnmp/nginx/Dockerfile <<'eof'
> FROM centos:7
> MAINTAINER this is nginx image <lnmp>
> ADD nginx-1.12.0.tar.gz /usr/local/src/
> ADD nginx.conf /usr/local/nginx/conf/
> ADD html/wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html
> WORKDIR /usr/local/src/nginx-1.12.0
> RUN curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
>     yum -y install pcre-devel zlib-devel gcc gcc-c++ make && yum clean all && \
>     useradd -M -s /sbin/nologin nginx && \
>     ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
>     --with-http_stub_status_module;make -j 4 && make install && \
>     chmod 777 -R /usr/local/nginx/html/
> ENV PATH /usr/local/nginx/sbin:$PATH
> EXPOSE 80
> EXPOSE 443
> VOLUME [ "/usr/local/nginx/html/" ]
> CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]
> eof
[root@MineGi ~]#

2.2 准备 nginx.conf 配置文件

命令示例:

cat >/opt/lnmp/nginx/nginx.conf <<'eof'
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html;
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   172.30.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
eof

输出结果:

[root@MineGi ~]# cat >/opt/lnmp/nginx/nginx.conf <<'eof'
> worker_processes  1;
> events {
>     worker_connections  1024;
> }
> http {
>     include       mime.types;
>     default_type  application/octet-stream;
>     sendfile        on;
>     keepalive_timeout  65;
>     server {
>         listen       80;
>         server_name  localhost;
>         charset utf-8;
>         location / {
>             root   html;
>             index  index.html index.php;
>         }
>         error_page   500 502 503 504  /50x.html;
>         location = /50x.html {
>             root   html;
>         }
>         location ~ \.php$ {
>             root           html;
>             fastcgi_pass   172.30.0.30:9000;
>             fastcgi_index  index.php;
>             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
>             include        fastcgi_params;
>         }
>     }
> }
> eof
[root@MineGi ~]#

3、Mysql

3.1 建立工作目录并进行相关操作

命令示例:

mkdir -p /opt/lnmp/mysql
wget -P /opt/lnmp/mysql https://cdn.mysql.com//archives/mysql-5.7/mysql-boost-5.7.20.tar.gz

cat >/opt/lnmp/mysql/Dockerfile <<'eof' 
FROM centos:7
MAINTAINER this is mysql image <lnmp>
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
ADD my.cnf /etc/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
    yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make && yum clean all && \
    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 4 && make install && \
    useradd -M -s /sbin/nologin mysql && \
    chown mysql:mysql /etc/my.cnf && \
    chown -R mysql:mysql /usr/local/mysql/ && \
    /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql \
    --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
eof

输出结果:

[root@MineGi ~]# mkdir -p /opt/lnmp/mysql
[root@MineGi ~]# wget -q -P /opt/lnmp/mysql https://cdn.mysql.com//archives/mysql-5.7/mysql-boost-5.7.20.tar.gz
[root@MineGi ~]# cat >/opt/lnmp/mysql/Dockerfile <<'eof' 
> FROM centos:7
> MAINTAINER this is mysql image <lnmp>
> ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
> ADD my.cnf /etc/
> WORKDIR /usr/local/src/mysql-5.7.20/
> RUN curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
>     yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make && yum clean all && \
>     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 4 && make install && \
>     useradd -M -s /sbin/nologin mysql && \
>     chown mysql:mysql /etc/my.cnf && \
>     chown -R mysql:mysql /usr/local/mysql/ && \
>     /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql \
>     --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
> ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
> EXPOSE 3306
> CMD ["/usr/local/mysql/bin/mysqld"]
> eof
[root@MineGi ~]# 

3.2 编写 my.cnf 配置文件

命令示例:

cat >/opt/lnmp/mysql/my.cnf <<'eof'
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
[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
server-id = 1
sql_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
eof

输出结果:

[root@MineGi ~]# cat >/opt/lnmp/mysql/my.cnf <<'eof'
> [client]
> port = 3306
> socket = /usr/local/mysql/mysql.sock

> [mysql]
> port = 3306
> socket = /usr/local/mysql/mysql.sock

> [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
> server-id = 1

> sql_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
> eof
[root@MineGi ~]#

4、PHP

4.1 建立工作目录并进行相关操作

命令示例:

mkdir -p /opt/lnmp/php
wget -P /opt/lnmp/php https://www.php.net/distributions/php-7.1.10.tar.bz2

cat >/opt/lnmp/php/Dockerfile <<'eof'
FROM centos:7
MAINTAINER this is php image <lnmp>
ADD php-7.1.10.tar.bz2 /usr/local/src/
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/
WORKDIR /usr/local/src/php-7.1.10
RUN curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
    yum -y install gd libjpeg libjpeg-devel libpng libpng-devel curl curl-devel openssl gcc gcc-c++ \
    freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel  openssl-devel make pcre-devel && \
    yum clean all && useradd -M -s /sbin/nologin nginx && \
    ./configure --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 \
    --enable-session --enable-ftp --enable-pdo --with-freetype-dir --with-openssl --enable-fpm \
    --enable-mbstring --enable-xml --enable-tokenizer --enable-zip && make -j 4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
EXPOSE 9000
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
eof

输出结果:

[root@MineGi ~]# mkdir -p /opt/lnmp/php
[root@MineGi ~]# wget -q -P /opt/lnmp/php https://www.php.net/distributions/php-7.1.10.tar.bz2
[root@MineGi ~]# cat >/opt/lnmp/php/Dockerfile <<'eof'
> FROM centos:7
> MAINTAINER this is php image <lnmp>
> ADD php-7.1.10.tar.bz2 /usr/local/src/
> 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/
> WORKDIR /usr/local/src/php-7.1.10
> RUN curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
>     yum -y install gd libjpeg libjpeg-devel libpng libpng-devel curl curl-devel openssl gcc gcc-c++ \
>     freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel  openssl-devel make pcre-devel && \
>     yum clean all && useradd -M -s /sbin/nologin nginx && \
>     ./configure --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 \
>     --enable-session --enable-ftp --enable-pdo --with-freetype-dir --with-openssl --enable-fpm \
>     --enable-mbstring --enable-xml --enable-tokenizer --enable-zip && make -j 4 && make install
> ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
> EXPOSE 9000
> ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
> eof
[root@MineGi ~]#

4.2 准备 php.ini、php-fpm.conf、www.conf 配置文件

4.2.1 修改php.ini 配置文件

情况说明:该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置

vim php.ini
 
#第939行,取消注释,修改
date.timezone = Asia/Shanghai
#第1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock

命令示例:

yum install -y bzip2
tar xjf /opt/lnmp/php/php-7.1.10.tar.bz2
cp ./php-7.1.10/php.ini-development /opt/lnmp/php/php.ini

sed -i -e 's#mysqli.default_socket =#& /usr/local/mysql/mysql.sock'# \
    -e '/;date.timezone =/a\date.timezone = Asia/Shanghai' /opt/lnmp/php/php.ini
    
grep "mysqli.default_socket =" /opt/lnmp/php/php.ini
grep "date.timezone" /opt/lnmp/php/php.ini

输出结果:

[root@MineGi ~]# yum install -y -q bzip2
软件包 bzip2-1.0.6-13.el7.x86_64 已安装并且是最新版本
[root@MineGi ~]# tar xjf /opt/lnmp/php/php-7.1.10.tar.bz2
[root@MineGi ~]# cp ./php-7.1.10/php.ini-development /opt/lnmp/php/php.ini
[root@MineGi ~]# sed -i -e 's#mysqli.default_socket =#& /usr/local/mysql/mysql.sock'# \
>     -e '/;date.timezone =/a\date.timezone = Asia/Shanghai' /opt/lnmp/php/php.ini
[root@MineGi ~]# grep "mysqli.default_socket =" /opt/lnmp/php/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock
[root@MineGi ~]# grep "date.timezone" /opt/lnmp/php/php.ini
; http://php.net/date.timezone
;date.timezone =
date.timezone = Asia/Shanghai
[root@MineGi ~]#

4.2.2 修改 php-fpm.conf 配置文件

情况说明:该配置文件位于 /usr/local/php/etc/php-fpm.conf.default

vim php-fpm.conf
 
#第17行,删除注释符号“;”
pid = run/php-fpm.pid

命令示例:

tee /opt/lnmp/php/php-fpm.conf <<-'eof'
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
eof

输出结果:

[root@MineGi ~]# tee /opt/lnmp/php/php-fpm.conf <<-'eof'
> [global]
> pid = run/php-fpm.pid
> include=/usr/local/php/etc/php-fpm.d/*.conf
> eof
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
[root@MineGi ~]# 

4.2.3 修改www.conf 配置文件

情况说明:该配置文件位于 usr/local/php/etc/php-fpm.d/www.conf.default

vim www.conf 
 
#第23、24行,修改用户和组
user = nginx
group = nginx
#第36行,修改监听IP和端口为容器IP:9000端口
listen = 172.30.0.30:9000
#第62行,修改被允许监听的客户端容器地址为回环地址和nginx容器地址
listen.allowed_clients = 127.0.0.1,172.30.0.10

命令示例:

cat >/opt/lnmp/php/www.conf <<-'eof'
[www]
user = nginx
group = nginx
listen = 172.30.0.30:9000
listen.allowed_clients = 127.0.0.1,172.30.0.10
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
eof

输出结果:

[root@MineGi ~]# cat >/opt/lnmp/php/www.conf <<-'eof'
> [www]
> user = nginx
> group = nginx
> listen = 172.30.0.30:9000
> listen.allowed_clients = 127.0.0.1,172.30.0.10
> pm = dynamic
> pm.max_children = 5
> pm.start_servers = 2
> pm.min_spare_servers = 1
> pm.max_spare_servers = 3
> eof
[root@MineGi ~]# 

5、Docker-Compose环境安装

5.1 安装docker-compose

【docker-compose:百度网盘】

情况说明:Docker-Compose 是 Docker 的独立产品,因此需要安装 Docker 之后在单独安装 Docker-Compose

#下载
curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#安装:添加权限就能直接使用
chmod +x /usr/local/bin/docker-compose
#查看版本
docker-compose --version

命令示例:

# curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
ls
ll docker-compose-1.24.1
mv docker-compose-1.24.1 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose 
docker-compose --version

输出结果:

[root@MineGi ~]# ls
php-7.1.10
[root@MineGi ~]# rz -E
rz waiting to receive.
[root@MineGi ~]# ll docker-compose-1.24.1 
-rw-r--r-- 1 root root 16168192 11月  1 17:08 docker-compose-1.24.1
[root@MineGi ~]# mv docker-compose-1.24.1 /usr/local/bin/docker-compose
[root@MineGi ~]# chmod +x /usr/local/bin/docker-compose 
[root@MineGi ~]# docker-compose --version
docker-compose version 1.24.1, build 4667896b
[root@MineGi ~]#

5.2 docker-compose.yml 文件配置

cd /opt/lnmp
 
vim docker-compose.yml
version: '2'
services:
  nginx:
    container_name: nginx     #指定容器的名称为nginx
    hostname: nginx-host      #容器的主机名为nginx-host
    image: nginx:lnmp         #使用的镜像为nginx:lnmp
    build:                    #构建上下文和Dockerfile的位置
      context: ./nginx
      dockerfile: Dockerfile
    ports:             
#将宿主机的80端口映射到容器的80端口,将宿主机的443端口映射到容器的443端口
      - 80:80
      - 443:443
    volumes:           #将/usr/local/nginx/html目录设为共享挂载点
      - /usr/local/nginx/html
    networks:          #将该服务连接到名为lnmp的网络,并指定了容器的IPv4地址
      lnmp:
        ipv4_address: 172.30.0.10
  mysql: 
    container_name: mysql     #容器的名称为mysql
    hostname: mysql-host      #容器的主机名为mysql-host
    image: mysql:lnmp         #使用的镜像为mysql:lnmp
    privileged: true          #使容器内的root拥有真正的root权限
    build:                    #构建上下文和Dockerfile的位置
      context: ./mysql        
      dockerfile: Dockerfile
    ports:                    #将宿主机的3306端口映射到容器的3306端口
      - 3306:3306
    volumes:                  #创建容器共享挂载目录/usr/local/mysql 
      - /usr/local/mysql
    networks:
      lnmp:
        ipv4_address: 172.30.0.20
  php:
    container_name: php       #容器的名称为php
    hostname: php-host        #容器的主机名为php-host
    image: php:lnmp           #使用的镜像为php:lnmp
    build:                    #指定了构建上下文和Dockerfile的位置
      context: ./php
      dockerfile: Dockerfile
    ports:                    #将宿主机的9000端口映射到容器的9000端口
      - 9000:9000
    depends_on:               #声明了该服务依赖于nginx和mysql服务
      - nginx
      - mysql
    volumes_from:             #从nginx和mysql服务中挂载卷
      - nginx
      - mysql
    networks:
      lnmp:
        ipv4_address: 172.30.0.30
networks:                     #定义了一个叫做lnmp的网络,使用了bridge驱动
  lnmp:
    driver: bridge
    ipam:
      config:                 #指定了IP地址池的子网范围
        - subnet: 172.30.0.0/16

命令示例:

cat >/opt/lnmp/docker-compose.yml <<'eof'
version: '2.2'
services:
  nginx:
    container_name: nginx    
    hostname: nginx-host     
    image: nginx:lnmp        
    build:                   
      context: ./nginx
      dockerfile: Dockerfile
    ports:             
      - 80:80
      - 443:443
    volumes:      
      - /usr/local/nginx/html
    networks:         
      lnmp:
        ipv4_address: 172.30.0.10
  mysql: 
    container_name: mysql   
    hostname: mysql-host    
    image: mysql:lnmp       
    privileged: true        
    build:                  
      context: ./mysql        
      dockerfile: Dockerfile
    ports:                
      - 3306:3306
    volumes:                 
      - /usr/local/mysql
    networks:
      lnmp:
        ipv4_address: 172.30.0.20
  php:
    container_name: php  
    hostname: php-host   
    image: php:lnmp      
    build:               
      context: ./php
      dockerfile: Dockerfile
    ports:                  
      - 9000:9000
    volumes_from:
      - nginx
      - mysql
    networks:
      lnmp:
        ipv4_address: 172.30.0.30
networks:                    
  lnmp:
    driver: bridge
    ipam:
      config:               
        - subnet: 172.30.0.0/16
eof

输出结果:

[root@MineGi ~]# cat >/opt/lnmp/docker-compose.yml <<'eof'
> version: '2.2'
> services:
>   nginx:
>     container_name: nginx    
>     hostname: nginx-host     
>     image: nginx:lnmp        
>     build:                   
>       context: ./nginx
>       dockerfile: Dockerfile
>     ports:             
>       - 80:80
>       - 443:443
>     volumes: 
         
>       - /usr/local/nginx/html
>     networks:         
>       lnmp:
>         ipv4_address: 172.30.0.10
>   mysql: 
>     container_name: mysql   
>     hostname: mysql-host    
>     image: mysql:lnmp       
>     privileged: true        
>     build:                  
>       context: ./mysql        
>       dockerfile: Dockerfile
>     ports:                
>       - 3306:3306
>     volumes:                 
>       - /usr/local/mysql
>     networks:
>       lnmp:
>         ipv4_address: 172.30.0.20
>   php:
>     container_name: php  
>     hostname: php-host   
>     image: php:lnmp      
>     build:               
>       context: ./php
>       dockerfile: Dockerfile
>     ports:                  
>       - 9000:9000
>     volumes_from:
>       - nginx
>       - mysql
>     networks:
>       lnmp:
>         ipv4_address: 172.30.0.30
> networks:                    
>   lnmp:
>     driver: bridge
>     ipam:
>       config:               
>         - subnet: 172.30.0.0/16
> eof
[root@MineGi ~]#

5.3 lnmp Docker-Compose 文件结构

命令示例:

tree /opt/lnmp

输出结果:

[root@MineGi ~]# tree -sh /opt/lnmp
/opt/lnmp
├── [1.2K]  docker-compose.yml
├── [  71]  mysql
│   ├── [1.2K]  Dockerfile
│   ├── [ 502]  my.cnf
│   └── [ 47M]  mysql-boost-5.7.20.tar.gz
├── [  81]  nginx
│   ├── [ 778]  Dockerfile
│   ├── [  42]  html
│   │   └── [8.7M]  wordpress-4.9.4-zh_CN.tar.gz
│   ├── [958K]  nginx-1.12.0.tar.gz
│   └── [ 791]  nginx.conf
└── [ 101]  php
    ├── [1.1K]  Dockerfile
    ├── [ 14M]  php-7.1.10.tar.bz2
    ├── [  75]  php-fpm.conf
    ├── [ 69K]  php.ini
    └── [ 210]  www.conf

4 directories, 13 files
[root@MineGi ~]# 

5.4 启动 Docker 容器

cd /opt/lnmp
docker-compose -f docker-compose.yml up -d
#-f, --file FILE:使用特定的 compose 模板文件,默认为 docker-compose.yml
#-p, --project-name NAME:指定项目名称,默认使用目录名称
#up: Docker Compose 要启动配置文件中定义的服务
#-d:在后台运行

命令示例:若网络等原因导致报错,再次执行此命令

docker-compose -f /opt/lnmp/docker-compose.yml up -d

输出结果:

[root@MineGi ~]# docker-compose -f /opt/lnmp/docker-compose.yml up -d
Creating network "lnmp_lnmp" with driver "bridge"
Building nginx
Step 1/12 : FROM centos:7
......
Step 11/11 : ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
 ---> Running in fd030ac7dac3
 ---> Removed intermediate container fd030ac7dac3
 ---> 7268b2ddb7e6
Successfully built 7268b2ddb7e6
Successfully tagged php:lnmp
WARNING: Image for service php was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating nginx ... done
Creating mysql ... done
Creating php   ... done
[root@MineGi ~]# 

命令示例:

docker images
docker ps 
docker-compose -f /opt/lnmp/docker-compose.yml ps

输出结果:

[root@MineGi ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
php          lnmp      7268b2ddb7e6   About a minute ago   965MB
mysql        lnmp      c9dd2e4fd22c   14 minutes ago       7.88GB
nginx        lnmp      c3cec3bc947a   19 hours ago         404MB
centos       7         eeb6ee3f44bd   3 years ago          204MB
[root@MineGi ~]# docker ps 
CONTAINER ID   IMAGE        COMMAND                   CREATED              STATUS              PORTS                                                                      NAMES
91dd40792f0d   php:lnmp     "/usr/local/php/sbin…"   About a minute ago   Up About a minute   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp                                  php
ef7217665b96   nginx:lnmp   "/usr/local/nginx/sb…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx
d4309dcd26e2   mysql:lnmp   "/usr/local/mysql/bi…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp                                  mysql
[root@MineGi ~]# docker-compose -f /opt/lnmp/docker-compose.yml ps
Name               Command               State                                   Ports                                 
-----------------------------------------------------------------------------------------------------------------------
mysql   /usr/local/mysql/bin/mysqld      Up      0.0.0.0:3306->3306/tcp,:::3306->3306/tcp                              
nginx   /usr/local/nginx/sbin/ngin ...   Up      0.0.0.0:443->443/tcp,:::443->443/tcp, 0.0.0.0:80->80/tcp,:::80->80/tcp
php     /usr/local/php/sbin/php-fpm -F   Up      0.0.0.0:9000->9000/tcp,:::9000->9000/tcp                              
[root@MineGi ~]#

#用于显示当前Docker Compose项目中正在运行的服务的状态
docker inspect nginx    #查看 nginx 容器的ip地址等信息
docker inspect mysql    #查看 mysql 容器的ip地址等信息
docker inspect php      #查看 php 容器的ip地址等信息

命令示例:

docker inspect nginx |grep 172.30.0
docker inspect mysql |grep 172.30.0
docker inspect php |grep 172.30.0

输出结果:

[root@MineGi ~]# docker inspect nginx |grep 172.30.0
                        "IPv4Address": "172.30.0.10"
                    "Gateway": "172.30.0.1",
                    "IPAddress": "172.30.0.10",
[root@MineGi ~]# docker inspect mysql |grep 172.30.0
                        "IPv4Address": "172.30.0.20"
                    "Gateway": "172.30.0.1",
                    "IPAddress": "172.30.0.20",
[root@MineGi ~]# docker inspect php |grep 172.30.0
                        "IPv4Address": "172.30.0.30"
                    "Gateway": "172.30.0.1",
                    "IPAddress": "172.30.0.30",
[root@MineGi ~]# 

6、启动 wordpress 服务

6.1 mysql授权

docker exec -it mysql /bin/bash   #进入到 mysql 容器内
mysql -u root -p
Enter password: 
                  #初始密码为空,回车即可
create database wordpress;
#创建wordpress数据库
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
#授予一个名为 wordpress 的数据库中的用户 wordpress 在任何主机上('%')的所有权限,并使用密码 123456 进行身份验证
#将所有权限授予用户 wordpress,允许其从任何主机连接到 wordpress 数据库,并使用密码 123456 进行身份验证
 
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
#授予用户名为 root,从任何主机连接到 MySQL 数据库服务器的用户,对所有数据库中的所有表具有全部权限,并设置密码为 abc123
#这样的设置允许用户 root 从任何地方以密码 abc123 连接到 MySQL 数据库,并具有对所有数据库的全部权限
 
flush privileges;
    #刷新权限
 
exit   #退出 mysql 数据库
 
exit  #退出 mysql 容器

命令示例:

docker exec -it mysql /bin/bash 
mysql -u root -p

create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
exit
exit

输出结果:

[root@MineGi ~]# docker exec -it mysql /bin/bash 
[root@mysql-host mysql-5.7.20]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@mysql-host mysql-5.7.20]# exit
exit
[root@MineGi ~]#

6.2 创建wp-config.php文件

docker exec -it nginx /bin/bash      #进入到nginx容器内
cd /usr/local/nginx/html/wordpress/   #切换目录
 
cp wp-config-sample.php wp-config.php
#手动创建wp-config.php文件
vim wp-config.php 
#第23行,将'database_name_here'改为'wordpress'
define('DB_NAME', 'wordpress');
#第26行,将'username_here'改为'wordpress'
define('DB_USER', 'wordpress');
#第29行,将'password_here'改为'123456'
define('DB_PASSWORD', '123456');
 
exit            #退出 nginx 容器
 
systemctl restart docker   #重启docker服务
docker start nginx;docker start mysql;docker start php   #重启docker容器
docker ps   #查看当前正在运行中的容器,必须得有nginx、mysql、php

命令示例:

docker exec -it nginx /bin/bash
ls /usr/local/nginx/html/wordpress/wp-config-sample.php
cp -a /usr/local/nginx/html/wordpress/{wp-config-sample.php,wp-config.php}
ll /usr/local/nginx/html/wordpress/{wp-config-sample.php,wp-config.php}

sed -i -e 's/database_name_here/wordpress/g' \
    -e 's/username_here/wordpress/g' \
    -e 's/password_here/123456/g' \
    /usr/local/nginx/html/wordpress/wp-config.php

grep -E "DB_NAME|DB_USER|DB_PASSWORD" \
    /usr/local/nginx/html/wordpress/wp-config.php

exit

输出结果:

[root@MineGi ~]# docker exec -it nginx /bin/bash
[root@nginx-host nginx-1.12.0]# ls /usr/local/nginx/html/wordpress/wp-config-sample.php
/usr/local/nginx/html/wordpress/wp-config-sample.php
[root@nginx-host nginx-1.12.0]# cp -a /usr/local/nginx/html/wordpress/{wp-config-sample.php,wp-config.php}
[root@nginx-host nginx-1.12.0]# ll /usr/local/nginx/html/wordpress/{wp-config-sample.php,wp-config.php}
-rwxrwxrwx 1 65534 65534 2930 Feb  8  2018 /usr/local/nginx/html/wordpress/wp-config-sample.php
-rwxrwxrwx 1 65534 65534 2930 Feb  8  2018 /usr/local/nginx/html/wordpress/wp-config.php
[root@nginx-host nginx-1.12.0]# sed -i -e 's/database_name_here/wordpress/g' \
>     -e 's/username_here/wordpress/g' \
>     -e 's/password_here/123456/g' \
>     /usr/local/nginx/html/wordpress/wp-config.php
[root@nginx-host nginx-1.12.0]# grep -E "DB_NAME|DB_USER|DB_PASSWORD" \
>     /usr/local/nginx/html/wordpress/wp-config.php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', '123456');
[root@nginx-host nginx-1.12.0]# exit
exit
[root@MineGi ~]# 

命令示例:

systemctl restart docker
docker ps
docker-compose -f /opt/lnmp/docker-compose.yml up -d
docker ps

输出结果:

[root@MineGi ~]# systemctl restart docker
[root@MineGi ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@MineGi ~]# docker-compose -f /opt/lnmp/docker-compose.yml up -d
Starting nginx ... done
Starting mysql ... done
Starting php   ... done
[root@MineGi ~]# docker ps
CONTAINER ID   IMAGE        COMMAND                   CREATED         STATUS         PORTS                                                                      NAMES
91dd40792f0d   php:lnmp     "/usr/local/php/sbin…"   7 minutes ago   Up 4 seconds   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp                                  php
ef7217665b96   nginx:lnmp   "/usr/local/nginx/sb…"   7 minutes ago   Up 5 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx
d4309dcd26e2   mysql:lnmp   "/usr/local/mysql/bi…"   7 minutes ago   Up 5 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp                                  mysql
[root@MineGi ~]#

6.3 浏览器访问安装wordpress

命令示例:

浏览器访问
http://10.4.7.11/wordpress/index.php

输出结果:


http://www.kler.cn/a/451800.html

相关文章:

  • GoFrame框架介绍
  • 如何在 Ubuntu 22.04 上安装和使用 Composer
  • 【Rust自学】6.3. 控制流运算符-match
  • SDMTSP:黑翅鸢算法(Black-winged kite algorithm,BKA)求解单仓库多旅行商问题,可以更改数据集和起点(MATLAB代码)
  • 【Python】基础语法介绍
  • LabVIEW中什么和C 语言指针类似?
  • Debug的使用
  • 19、vue3组件通信
  • STM32文件详解
  • 从 GitLab.com 到 JihuLab.com 的迁移指南
  • leetcode之hot100---19删除链表的第N个节点(C++)
  • GitLab 将停止为中国区用户提供服务,60天迁移期如何应对? | LeetTalk Daily
  • 【NLP高频面题 - 高效微调篇】什么是提示微调?
  • 全国硕士研究生入学考试(考研)备考要点之备考原则
  • GMV 含义
  • 【R语言遥感技术】“R+遥感”的水环境综合评价方法
  • 接口请求中调试可以看到Origin,其具体的作用
  • 【文档搜索引擎】缓冲区优化和索引模块小结
  • 框架专题:设计模式
  • mvn install:install-file jar 打入本地仓库
  • 虚拟机桥接模式
  • Spark和Hive的联系
  • 【视觉惯性SLAM:SLAM中常用的数学基础知识】
  • BOM清单在制造企业运营中的全方位作用解析
  • 高并发处理 --- Caffeine内存缓存库
  • 【每日学点鸿蒙知识】私仓搭建、resources创建文件夹、hvigor如何动态设置版本、SM3摘要算法、SP存储报错等