root@tr-desktop:/home/tr# tree lnmp-docker/
lnmp-docker/
├── docker-compose.yml
├── Dockerfile
├── html
│ ├── index.html
│ └── index.php
├── nginx.conf
└── redis.conf
1 directory, 6 files
root@tr-desktop:/home/tr# cat lnmp-docker/docker-compose.yml
version: '3.8'
services:
# Nginx服务
nginx:
image: nginx:latest
container_name: nginx
ports:
- "8099:80" # 将宿主机的8099端口映射到容器的80端口
volumes:
#- ./nginx.conf:/etc/nginx/nginx.conf # 配置文件映射
- ./nginx.conf:/etc/nginx/conf.d/default.conf # 配置文件映射
- ./html:/usr/share/nginx/html # 网站根目录
depends_on:
- php
networks:
- lnmp_network
# MySQL服务
mysql:
image: mysql:5.7
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: rootpassword # 设置 MySQL 根密码
MYSQL_DATABASE: exampledb # 创建数据库
MYSQL_USER: user # 创建用户
MYSQL_PASSWORD: userpassword # 设置用户密码
volumes:
- mysql_data:/var/lib/mysql # 数据持久化
networks:
- lnmp_network
# PHP-FPM服务
php:
image: php:7.4-fpm
build:
context: .
dockerfile: Dockerfile # 这里使用自定义的 Dockerfile
container_name: php
volumes:
- ./html:/var/www/html # PHP 网站根目录
#depends_on:
# - mysql
networks:
- lnmp_network
# redis服务
redis:
image: redis:latest
#ports:
# - "6379:6379"
#environment:
# - REDIS_PASSWORD=yourpassword
#command: ["redis-server", "--requirepass", "123456"]
volumes:
- ./redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
container_name: redis
networks:
- lnmp_network
#网络
networks:
lnmp_network:
driver: bridge
#卷
volumes:
mysql_data:
driver: local
root@tr-desktop:/home/tr# cat lnmp-docker/
docker-compose.yml Dockerfile html/ nginx.conf redis.conf
root@tr-desktop:/home/tr# cat lnmp-docker/Dockerfile
#FROM php:7.4-fpm
# 安装 mysqli 和 pdo_mysql 扩展
#RUN docker-php-ext-install mysqli pdo_mysql
FROM php:7.4-fpm
RUN docker-php-ext-install mysqli pdo_mysql
# 安装系统依赖(需要安装 redis 扩展的编译依赖)
RUN apt-get update && apt-get install -y \
libssl-dev \
zlib1g-dev \
libcurl4-openssl-dev \
&& pecl install redis \
&& docker-php-ext-enable redis
# 清理缓存
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
root@tr-desktop:/home/tr# cat lnmp-docker/html/index.php
<?php
#容器名称
$host = "mysql"; // 数据库主机
$username = "root"; // 数据库用户名
$password = "rootpassword"; // 数据库密码
$dbname = "exampledb"; // 数据库名称
// 创建连接
$conn = mysqli_connect($host, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
echo "连接成功!";
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->auth('123456');
$redis->set('key', 'value');
echo $redis->get('key');
phpinfo();
?>
root@tr-desktop:/home/tr# cat lnmp-docker/
docker-compose.yml Dockerfile html/ nginx.conf redis.conf
root@tr-desktop:/home/tr# cat lnmp-docker/nginx.conf
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass php:9000; # PHP-FPM服务
#fastcgi_pass 172.19.0.2:9000; # PHP-FPM服务
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
}
root@tr-desktop:/home/tr# cat lnmp-docker/redis.conf
requirepass 123456
root@tr-desktop:/home/tr#docker-compose up -d