一 摘要
本文主要介绍基于docker docker-compose 安装 lnmp 三件套,以及用phpmysadmin 验证下部署可正确。
二 环境信息
2.1 操作系统
[root@2023001 ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
[root@2023001 ~]#
2.2 docker
[root@2023001 soft]# docker-compose -v
Docker Compose version v2.16.0
[root@2023001 soft]# docker -v
Docker version 23.0.1, build a5ee5b1
[root@2023001 soft]#
三 实施
相关镜像在docker hub 官网下载 https://hub.docker.com/
3.1 安装docker 以及dockercompose
具体参看该文https://blog.csdn.net/nasooo/article/details/129753140
3.1 安装nginx
3.1.1 获取镜像
[root@2023001 soft]# docker pull nginx:stable-perl
[root@2023001 soft]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx stable-perl b4c217053922 22 hours ago 190MB
[root@2023001 soft]#
3.1.2 运行
[root@2023001 soft]# docker run -p 80:80 --name commonnginx -d nginx:stable-perl
8a3487ef5818167e1475856cfd9707c55f713e8d146e08131671c78149c3fd07
[root@2023001 soft]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8a3487ef5818 nginx:stable-perl "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp commonnginx
[root@2023001 soft]#
3.1.3 验证
3.1.4 基于docker-compose 安装
3.1.4.1 docker-compose 脚本
version: '1.1'
services:
nginx:
image: nginx:stable-perl # 镜像名称
container_name: commonappnginx # 容器名字
restart: always # 开机自动重启
ports: # 端口号绑定(宿主机:容器内)
- '80:80'
- '443:443'
volumes: # 目录映射(宿主机:容器内)
- /data/yunweipro/commonapp/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- /data/yunweipro/commonapp/nginx/conf.d:/etc/nginx/conf.d
- /data/yunweipro/commonapp/nginx/html:/usr/share/nginx/html
- /data/yunweipro/commonapp/nginx/logs:/var/log/nginx
3.1.4.2 目录结构
[root@2023001 commonapp]# tree -a
.
├── dockercomposescript
│ └── commonappnginx.yml
└── nginx
├── conf
│ └── nginx.conf
├── conf.d
│ └── default.conf
├── html
│ └── index.html
└── logs
├── access.log
└── error.log
启动之前,必须在该目录下 添加,一般先启动个同版本的容器 docker cp 出来
nginx.conf
default.conf
index.html
缺少这三个文件 ,nginx 会出现异常。
通过注释脚本每一行 定位错误
3.1.4.3 启动
[root@2023001 dockercomposescript]# docker-compose -f commonappnginx.yml up -d
3.1.4.4 验证目录映射
修改/data/yunweipro/commonapp/nginx/html/index.html
3.2 安装php-fpm
3.2.1 获取镜像
[root@2023001 commonapp]# docker pull bitnami/php-fpm:7.4.32
7.4.32: Pulling from bitnami/php-fpm
1d8866550bdd: Pull complete
586e9578d6ca: Pull complete
Digest: sha256:75b8ad4221f0215ad73947e342659a43d7d9bc382fd591a3ecfc3970706ff0a3
Status: Downloaded newer image for bitnami/php-fpm:7.4.32
docker.io/bitnami/php-fpm:7.4.32
[root@2023001 commonapp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx stable-perl b4c217053922 25 hours ago 190MB
bitnami/php-fpm 7.4.32 85b93689d207 5 months ago 329MB
[root@2023001 commonapp]#
3.2.2 配置
version: '2'
services:
nginx:
image: nginx:stable-perl # 镜像名称
container_name: commonappnginx # 容器名字
restart: always # 开机自动重启
ports: # 端口号绑定(宿主机:容器内)
- '80:80'
volumes: # 目录映射(宿主机:容器内)
- /data/yunweipro/commonapp/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- /data/yunweipro/commonapp/nginx/conf.d:/etc/nginx/conf.d
- /data/yunweipro/commonapp/nginx/html:/usr/share/nginx/html
- /data/yunweipro/commonapp/nginx/logs:/var/log/nginx
php:
image: bitnami/php-fpm:7.4.32 # 镜像名称
container_name: php # 容器名字
restart: always # 开机自动重启
# ports: # 端口号绑定(宿主机:容器内)
# - '9000:9000'
volumes: # 目录映射(宿主机:容器内)
- /data/yunweipro/commonapp/nginx/html:/usr/share/nginx/html
conf.d/default.conf 添加此段
#当请求网站下php文件的时候,反向代理到php-fpm
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
3.2.3 运行
[root@2023001 dockercomposescript]# docker-compose -f commonapp.yml up -d
[root@2023001 dockercomposescript]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ea54c26796c nginx:stable-perl "/docker-entrypoint.…" 12 seconds ago Up 10 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp commonappnginx
935121a60fc6 bitnami/php-fpm:7.4.32 "php-fpm -F --pid /o…" 16 hours ago Up 16 hours 9000/tcp php
3.2.4 验证
在/data/yunweipro/commonapp/nginx/html 目录下 放个info.php 文件,文件内容如下
<html>
<head>
<title>PHP 测试</title>
</head>
<body><?php phpinfo(); ?></body>
</html>
3.4 安装mysql
3.4.1 获取镜像
3.4.2 配置
mysql:
image: registry.kxdigit.com/mysql:5.7.33
restart: always
container_name: commonappmysql
environment:
MYSQL_ROOT_PASSWORD: xxxxx.123
ports:
- 3306:3306
volumes:
- /data/yunweipro/commonapp/mysql:/var/lib/mysql
- /data/yunweipro/backup/commonapp/mysqlbackupdata:/data/backup
command:
--lower_case_table_names=1
--transaction-isolation=READ-COMMITTED
--max_allowed_packet=34M
--innodb_log_file_size=256M
--character-set-server=utf8
--collation-server=utf8_bin
--log-bin=mysql-bin
--server-id=1
新建目录
[root@2023001 dockercomposescript]# mkdir -p /data/yunweipro/commonapp/mysql
[root@2023001 dockercomposescript]# mkdir -p /data/yunweipro/backup/commonapp/mysqlbackupdata
3.4.3 运行
[root@2023001 dockercomposescript]# docker-compose -f commonapp.yml up -d
[+] Running 3/3
⠿ Container commonappmysql Started 0.9s
⠿ Container php Running 0.0s
⠿ Container commonappnginx Started 0.8s
[root@2023001 dockercomposescript]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca2bdd70e0d4 nginx:stable-perl "/docker-entrypoint.…" 59 seconds ago Up 58 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp commonappnginx
a10b59299085 registry.xxxxx.com/mysql:5.7.33 "docker-entrypoint.s…" 59 seconds ago Up 58 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp commonappmysql
935121a60fc6 bitnami/php-fpm:7.4.32 "php-fpm -F --pid /o…" 16 hours ago Up 16 hours 9000/tcp php
[root@2023001 dockercomposescript]#
3.5 安装phpmysadmin
我这里是下载phpmyadmin 文件然后放到 nginx 下运行,而不是直接安装phpmyadmin 容器
3.5.1 关键配置
config.inc.php 配置数据库地址
commonappmysql 即是上面mysql 的容器名。
$cfg['Servers'][$i]['host'] = 'commonappmysql';
发表评论 取消回复