Docker compose 配置
docker compose 配置文件 docker-lnmp.yml
version: '3'
services:
db:
image: mysql:8
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./app:/usr/share/nginx/html
- ./nginx.conf:/etc/nginx/nginx.conf
restart: always
php:
image: php:7.4-fpm
volumes:
- ./app:/var/www/html
restart: always
volumes:
db_data:
这个 docker-lnmp.yml
文件中,定义了三个服务:db(MySQL)、nginx和php(PHP-FPM)。定义了一个卷db_data,用于存储MySQL的数据。
其中 ./app是指docker-compose.yml这个文件目录下的app文件夹,这是你Web应用程序目录,把你的php文件放这。
Nginx 配置
还要创建一个Nginx配置文件nginx.conf,配置Nginx代理请求到PHP-FPM服务:
worker_processes 1;
events {
worker_connections 1024;
}
http {
upstream php {
server php:9000;
}
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
在你的docker-lnmp.yml
文件目录执行这个命令启动服务,会自动解析yml文件,拉取镜像并启动容器,一步完成。
docker-compose up -d
访问http://serverIP:80
测试。