容器化 PHP 和 Nginx 需要为每个服务创建 Docker 容器,并将它们配置为协同工作。以下是创建基本 Docker 配置所涉及的步骤,该示例基于一个简单的 PHP 应用程序。
1、创建项目结构:
首先创建具有以下结构的项目:
//projet
├── index.php
├── nginx
│ └── nginx.conf
├── Dockerfile
├── Dockerfile-nginx
└── docker-compose.yml
2. 创建 PHP 应用程序
创建一个简单的 PHP 应用程序。例如,在根目录中创建一个名为 index.php 的文件:
<?php
echo "Hello!";
3. 配置 Nginx
在 nginx/nginx.conf 文件中创建 Nginx 配置文件:
server {
listen 80;
index index.php;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
error_page 404 /index.php;
root /var/www;
client_max_body_size 20m;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
4、编写 Dockerfile :
FROM php:8.0.2-fpm
WORKDIR /var/www
5、创建 Nginx Dockerfile
在项目根目录中创建 Dockerfile-nginx 文件,内容如下:
FROM nginx:1.19-alpine
WORKDIR /var/www
6、创建 .dockerignore 文件
在项目根目录中创建 .dockerignore 文件,内容如下:
docker-compose
Dockerfile*
7、编写 Docker Compose 文件
在项目根目录中创建 docker-compose.yml 文件,内容如下:
version: "3"
services:
app:
container_name: app-container
build:
context: ./
dockerfile: ./Dockerfile
working_dir: /var/www/
restart: always
volumes:
- ./:/var/www
networks:
- app-network
nginx:
container_name: nginx-container
build:
context: ./
dockerfile: ./Dockerfile-nginx
restart: always
ports:
- "8000:80"
volumes:
- ./nginx:/etc/nginx/conf.d
- ./:/var/www
depends_on:
- app
networks:
- app-network
networks:
app-network:
driver: bridge
我们创建了一个应用程序网络,以便将 PHP 和 Nginx 容器连接到同一网络。这允许它们相互通信,而不会与本地系统的网络隔离。
8、构建并运行容器
使用以下命令构建并运行容器:
docker-compose up --build
访问应用程序:
在浏览器中打开 http://localhost:8000。您应该看到消息“Hello!”。
匿名
2025-10-22
盖楼盖楼!
匿名
2025-08-11
沙发沙发
匿名
2025-08-10
https://at.oiik.cn/bing.html
匿名
2025-02-21
实用,我在开发https://minmail.app/时候使用到了
王飞翔
2024-12-30
亲爱的朋友:您好!中国疫情持续蔓延,很多人症状非常严重持久不愈,医院人满为患,各年龄段随地倒猝死的现象暴增,多省感染手足口、甲流、乙流、支原体、合胞及腺病毒的儿童不断攀升,目前各种天灾人祸,天气异象频发。古今中外的很多预言都说了这几年人类有大灾难,如刘伯温在预言中说 “贫者一万留一千,富者一万留二三”,“贫富若不回心转,看看死期到眼前”, 预言中也告诉世人如何逃离劫难的方法,真心希望您能躲过末劫中的劫难,有个美好的未来,请您务必打开下方网址认真了解,内有躲避瘟疫保平安的方法。网址1:https://github.com/1992513/www/blob/master/README.md?abhgc#1 网址2:bitly.net/55bbbb 网址3:https://d3ankibxiji86m.cloudfront.net/30gj 如打不开请多换几个浏览器试
匿名
2024-12-12
Backdata 搜索引擎网址提交入口:https://backdata.net/submit-site.html