下面我将提供一个完整的Docker Compose方案来搭建PHP+Nginx+Swoole+MySQL+Redis的开发环境。
首先创建以下目录结构:
your_project/
├── docker-compose.yml
├── nginx/
│ └── conf.d/
│ └── default.conf
├── php/
│ ├── Dockerfile
│ └── php.ini
├── mysql/
│ └── my.cnf
└── www/
└── (你的PHP项目文件)
创建docker-compose.yml
文件:
version: '3.8'
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./www:/var/www/html
depends_on:
- php
networks:
- app-network
php:
build: ./php
container_name: php
volumes:
- ./www:/var/www/html
networks:
- app-network
mysql:
image: mysql:8.0
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: app_db
MYSQL_USER: app_user
MYSQL_PASSWORD: userpassword
volumes:
- mysql-data:/var/lib/mysql
- ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
ports:
- "3306:3306"
networks:
- app-network
redis:
image: redis:latest
container_name: redis
ports:
- "6379:6379"
volumes:
- redis-data:/data
networks:
- app-network
swoole:
build: ./php
container_name: swoole
command: php /var/www/html/swoole_server.php
volumes:
- ./www:/var/www/html
ports:
- "9501:9501"
depends_on:
- php
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
mysql-data:
redis-data:
在php/Dockerfile
中:
FROM php:8.1-fpm
# 安装系统依赖
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
libzip-dev
# 安装PHP扩展
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip
# 安装Redis扩展
RUN pecl install redis && docker-php-ext-enable redis
# 安装Swoole扩展
RUN pecl install swoole && docker-php-ext-enable swoole
# 安装Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# 设置工作目录
WORKDIR /var/www/html
在nginx/conf.d/default.conf
中:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
在mysql/my.cnf
中:
[mysqld]
default_authentication_plugin=mysql_native_password
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
在www/swoole_server.php
中:
<?php
$http = new Swoole\Http\Server("0.0.0.0", 9501);
$http->on("start", function ($server) {
echo "Swoole http server is started at http://0.0.0.0:9501\n";
});
$http->on("request", function ($request, $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello Swoole!");
});
$http->start();
运行以下命令启动所有服务:
docker-compose up -d --build
http://localhost
应该能看到你的PHP应用http://localhost:9501
应该能看到 "Hello Swoole!"mysql -h 127.0.0.1 -u app_user -p
连接redis-cli -h 127.0.0.1
连接docker-compose down
docker-compose logs [service_name]
docker exec -it [container_name] bash
docker-compose up -d --build
这个配置提供了一个完整的开发环境,你可以根据实际项目需求进行调整。