1、docker-compose.yml ⽂件
[root@doc lnmp]# vim docker-compose.yml
version: '2'
volumes:
mysql-conf:
php-conf:
networks:
lnmp_net:
external: true
services:
nginx:
image: nginx
container_name: nginx-lnmp
hostname: nginx-lnmp
privileged: true
ports:
- 90:80
volumes:
-
/root/lnmp/nginx/html/:/usr/share/nginx/html/:rw
-
/root/lnmp/nginx/conf.d/:/etc/nginx/conf.d/:rw- /root/lnmp/nginx/logs/:/var/log/nginx/:rw
restart: always
networks:
- lnmp_net
php:
image: php:7.3.29-fpm
container_name: php-lnmp
hostname: php-lnmp
privileged: true
volumes:
- /root/lnmp/nginx/html/:/var/www/html/:rw
- php-conf:/usr/local/etc/php
volumes_from:
- mysql
restart: always
networks:
- lnmp_net
mysql:
image: mysql:5.6
container_name: mysql-lnmp
ports:
- 3306:3306
privileged: true
volumes:
- /var/lib/mysql
- /root/lnmp/mysql/data/:/var/lib/mysql/:rw2、⼯程⽬录结构树状图
- mysql-conf:/etc/mysql/
restart: always
networks:
- lnmp_net
environment:
MYSQL_ROOT_PASSWORD: "123456"
[root@doc lnmp]# tree ./
./
"## docker-compose.yml
"## mysql
$ %## data
"## mysql-conf
$ "## conf.d
"## nginx
$ "## conf.d
$ "## html
$ $ "## index.html
$ $ "## testa.php
$ $ "## testb.php
$ $ %## wordpress
$ %## logs
%## php-conf
"## conf.d
$ "## docker-php-ext-pdo_mysql.ini3、修改并导⼊配置⽂件
(1)nginx 配置⽂件
$ %## docker-php-ext-sodium.ini
"## php.ini
"## php.ini-development
%## php.ini-production
[root@doc lnmp]# vim nginx/conf.d/default.conf
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm index.php;
# redirect server error pages to the static
page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
index index.html index.htm index.php ;(2)导⼊ mysql 和 php 的配置⽂件到容器内
try_files $uri $uri/ /index.php?
$query_string;
autoindex on;
}
location ~ \.php$ {
fastcgi_pass php-lnmp:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PATH_INFO
$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME
/var/www/html/$fastcgi_script_name;
}
}
[root@doc lnmp]# mv mysql-conf/*
/var/lib/docker/volumes/lnmp_mysql-conf/_data/
[root@doc lnmp]# mv php-conf/*
/var/lib/docker/volumes/lnmp_php-conf/_data/(3)php 容器安装 mysql 扩展插件
[root@doc lnmp]# docker compose exec php bash
root@php-lnmp:/var/www/html# docker-php-ext
install pdo_mysql
root@php-lnmp:/var/www/html# docker-php-ext
install mysqli
root@php-lnmp:/var/www/html#
exit
[root@doc lnmp]# docker compose restart php
[+] Restarting 1/1
✔ Container php-lnmp Started
0.3s