1.创建项目目录, mkdir -p sh04/ 这里将其命名为 php-nginx-supervisord

2.在该项目下创建 nginx 子目录,准备 nginx 服务的配置文件,这里包括 nginx.conf 和 conf.d/site.conf  

3.在该目录下创建 supervisord 子目录,再创建一个下级目录 conf.d ,在其准备 supervisord 的配置文件 supervisord.conf 

4.在项目目录下编写 Dockerfile 

5.在项目目录下基于 Dockerfile 构建镜像

6.在项目目录下基于构建的镜像重启容器

7.实际测试 Web 服务

!!!代码及其解决报错完整如下:

[root@host1 ~]# mkdir -p php-nginx-supervisord
[root@host1 ~]# cd php-nginx-supervisord
[root@host1 php-nginx-supervisord]# mkdir -p nginx/conf.d
[root@host1 php-nginx-supervisord]# touch nginx/nginx.conf
[root@host1 php-nginx-supervisord]# touch nginx/conf.d/site.conf
[root@host1 php-nginx-supervisord]# mkdir -p supervisord/conf.d
[root@host1 php-nginx-supervisord]# touch supervisord/conf.d/supervisord.conf
[root@host1 php-nginx-supervisord]# mkdir html
[root@host1 php-nginx-supervisord]# touch html/index.php
[root@host1 php-nginx-supervisord]# touch Dockerfile
[root@host1 php-nginx-supervisord]# vi nginx.conf
[root@host1 php-nginx-supervisord]# cat nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 1024;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;

    include /etc/nginx/conf.d/*.conf;
}

[root@host1 php-nginx-supervisord]# vi site.conf
[root@host1 php-nginx-supervisord]# cat site.conf
server {
    listen 80;
    server_name localhost;
    root /var/www/html;
    index index.php index.html index.htm;

    access_log /var/log/nginx/site-access.log;
    error_log /var/log/nginx/site-error.log;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
    }
}

[root@host1 php-nginx-supervisord]# vi index.php
[root@host1 php-nginx-supervisord]# cat index.php
<?php
echo "<h1>PHP-Nginx-Supervisord 测试页面</h1>";
echo "<p>当前时间: " . date('Y-m-d H:i:s') . "</p>";
echo "<p>PHP版本: " . phpversion() . "</p>";

echo "<h3>服务器信息:</h3>";
echo "<pre>";
print_r($_SERVER);
echo "</pre>";
?>

[root@host1 php-nginx-supervisord]# vi supervisord.conf
[root@host1 php-nginx-supervisord]# cat supervisord.conf
[supervisord]
nodaemon=true
logfile=/var/log/supervisord/supervisord.log
pidfile=/var/run/supervisord.pid

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisord/nginx.log
stderr_logfile=/var/log/supervisord/nginx.err.log
user=root

[program:php-fpm]
command=/usr/sbin/php-fpm8.1 -F
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisord/php-fpm.log
stderr_logfile=/var/log/supervisord/php-fpm.err.log
user=root

[root@host1 php-nginx-supervisord]# vi Dockerfile
[root@host1 php-nginx-supervisord]# cat Dockerfile
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    nginx \
    php8.1 \
    php8.1-fpm \
    php8.1-cli \
    php8.1-mbstring \
    php8.1-curl \
    php8.1-gd \
    php8.1-mysql \
    php8.1-xml \
    supervisor \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /var/log/supervisord \
    && mkdir -p /var/www/html \
    && chown -R www-data:www-data /var/www/html \
    && chmod 755 /var/www/html

COPY nginx/nginx.conf /etc/nginx/nginx.conf
COPY nginx/conf.d/site.conf /etc/nginx/conf.d/site.conf

COPY supervisord/conf.d/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

COPY html/ /var/www/html/

EXPOSE 80

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

[root@host1 php-nginx-supervisord]# docker build -t php-nginx-supervisor:v1 .
[+] Building 99.5s (12/12) FINISHED                                                   docker:default
 => [internal] load build definition from Dockerfile                                            0.0s
 => => transferring dockerfile: 851B                                                            0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04                                 3.7s
 => [internal] load .dockerignore                                                               0.0s
 => => transferring context: 2B                                                                 0.0s
 => [1/7] FROM docker.io/library/ubuntu:22.04@sha256:4e0171b9275e12d375863f2b3ae9ce00a4c53ddda  5.2s
 => => resolve docker.io/library/ubuntu:22.04@sha256:4e0171b9275e12d375863f2b3ae9ce00a4c53ddda  0.0s
 => => sha256:4e0171b9275e12d375863f2b3ae9ce00a4c53ddda176bd55868df97ac6f21a6e 6.69kB / 6.69kB  0.0s
 => => sha256:d0afa9fbcf16134b776fbba4a04c31d476eece2d080c66c887fdd2608e4219a9 424B / 424B      0.0s
 => => sha256:b1dc6972547a6fd2bd691a8d37cfecb7f66f3b27279018ca61657c77c8c32b3c 2.30kB / 2.30kB  0.0s
 => => sha256:60d98d907669dc22e547405da3e409eb14496606f4ac90692c5f2ef5081c4b 29.54MB / 29.54MB  4.1s
 => => extracting sha256:60d98d907669dc22e547405da3e409eb14496606f4ac90692c5f2ef5081c4b1e       1.1s
 => [internal] load build context                                                               0.0s
 => => transferring context: 863B                                                               0.0s
 => [2/7] RUN apt-get update && apt-get install -y     nginx     php8.1     php8.1-fpm     ph  89.8s
 => [3/7] RUN mkdir -p /var/log/supervisord     && mkdir -p /var/www/html     && chown -R www-  0.1s 
 => [4/7] COPY nginx/nginx.conf /etc/nginx/nginx.conf                                           0.0s 
 => [5/7] COPY nginx/conf.d/site.conf /etc/nginx/conf.d/site.conf                               0.0s 
 => [6/7] COPY supervisord/conf.d/supervisord.conf /etc/supervisor/conf.d/supervisord.conf      0.0s 
 => [7/7] COPY html/ /var/www/html/                                                             0.0s 
 => exporting to image                                                                          0.6s 
 => => exporting layers                                                                         0.6s
 => => writing image sha256:b5a3a88ca831741627e38c3d4e87daf3ee5ecf20c527f0519be852c60ebacc67    0.0s
 => => naming to docker.io/library/php-nginx-supervisor:v1                                      0.0s
[root@host1 php-nginx-supervisord]# docker stop php-web-app && docker rm php-web-app
Error response from daemon: No such container: php-web-app
[root@host1 php-nginx-supervisord]# docker run -d -p 8080:80 --name php-web-app php-nginx-supervisor:v1
d17ff606c3e1d849085fbc83dec63b46d74c4566815f711cb7a2409ccf3ad953
[root@host1 php-nginx-supervisord]# docker ps | grep php-web-app
[root@host1 php-nginx-supervisord]# docker exec -it php-web-app supervisorctl status
Error response from daemon: container d17ff606c3e1d849085fbc83dec63b46d74c4566815f711cb7a2409ccf3ad953 is not running
[root@host1 php-nginx-supervisord]# docker logs php-web-app
Error: .ini file does not include supervisord section
For help, use /usr/bin/supervisord -h
[root@host1 php-nginx-supervisord]# vi supervisord.conf
[root@host1 php-nginx-supervisord]# cat supervisord.conf
[supervisord]
nodaemon=true 
logfile=/var/log/supervisord/supervisord.log
pidfile=/var/run/supervisord.pid

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;" 
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisord/nginx.log
stderr_logfile=/var/log/supervisord/nginx.err.log

[program:php-fpm]
command=/usr/sbin/php-fpm8.1 -F 
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisord/php-fpm.log
stderr_logfile=/var/log/supervisord/php-fpm.err.log
[root@host1 php-nginx-supervisord]# docker build -t php-nginx-supervisor:v1 .
[+] Building 0.8s (12/12) FINISHED                                                    docker:default
 => [internal] load build definition from Dockerfile                                            0.0s
 => => transferring dockerfile: 851B                                                            0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04                                 0.8s
 => [internal] load .dockerignore                                                               0.0s
 => => transferring context: 2B                                                                 0.0s
 => [1/7] FROM docker.io/library/ubuntu:22.04@sha256:4e0171b9275e12d375863f2b3ae9ce00a4c53ddda  0.0s
 => [internal] load build context                                                               0.0s
 => => transferring context: 847B                                                               0.0s
 => CACHED [2/7] RUN apt-get update && apt-get install -y     nginx     php8.1     php8.1-fpm   0.0s
 => CACHED [3/7] RUN mkdir -p /var/log/supervisord     && mkdir -p /var/www/html     && chown   0.0s
 => CACHED [4/7] COPY nginx/nginx.conf /etc/nginx/nginx.conf                                    0.0s
 => CACHED [5/7] COPY nginx/conf.d/site.conf /etc/nginx/conf.d/site.conf                        0.0s
 => CACHED [6/7] COPY supervisord/conf.d/supervisord.conf /etc/supervisor/conf.d/supervisord.c  0.0s
 => CACHED [7/7] COPY html/ /var/www/html/                                                      0.0s
 => exporting to image                                                                          0.0s
 => => exporting layers                                                                         0.0s
 => => writing image sha256:fa05d07448a9ce590f6b2bc3aa53aa88adb56175f7e4a9189a1efb71ddbc49bb    0.0s
 => => naming to docker.io/library/php-nginx-supervisor:v1                                      0.0s
[root@host1 php-nginx-supervisord]# docker rm -f php-web-app
php-web-app
[root@host1 php-nginx-supervisord]# docker run -d -p 8080:80 --name php-web-app php-nginx-supervisor:v1
474ebecea5cb1b3a9bc6ae21a463166fc1401002ce9d374b653416d671f9e2e4
[root@host1 php-nginx-supervisord]# docker ps | grep php-web-app
[root@host1 php-nginx-supervisord]# docker logs php-web-app
Error: .ini file does not include supervisord section
For help, use /usr/bin/supervisord -h
[root@host1 php-nginx-supervisord]# docker exec -it php-web-app supervisorctl status
Error response from daemon: container 474ebecea5cb1b3a9bc6ae21a463166fc1401002ce9d374b653416d671f9e2e4 is not running
[root@host1 php-nginx-supervisord]# curl http://localhost:8080
curl: (7) Failed to connect to localhost port 8080: 拒绝连接
[root@host1 php-nginx-supervisord]# vi supervisord.conf
[root@host1 php-nginx-supervisord]# cat supervisord.conf
# 文件路径:supervisord/conf.d/supervisord.conf
[supervisord]
nodaemon=true          # 强制前台运行,容器不退出
logfile=/var/log/supervisord/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/var/run/supervisord.pid

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"  # Nginx 前台运行
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisord/nginx.log
stderr_logfile=/var/log/supervisord/nginx.err.log

[program:php-fpm]
command=/usr/sbin/php-fpm8.1 -F  # PHP-FPM 前台运行(注意版本号是否匹配)
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisord/php-fpm.log
stderr_logfile=/var/log/supervisord/php-fpm.err.log
[root@host1 php-nginx-supervisord]# COPY supervisord/conf.d/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
bash: COPY: 未找到命令...
[root@host1 php-nginx-supervisord]# mkdir -p supervisord/conf.d
[root@host1 php-nginx-supervisord]# mv supervisord.conf supervisord/conf.d/
mv:是否覆盖'supervisord/conf.d/supervisord.conf'? y
[root@host1 php-nginx-supervisord]# COPY supervisord/conf.d/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
bash: COPY: 未找到命令...
[root@host1 php-nginx-supervisord]# cat Dockerfile
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    nginx \
    php8.1 \
    php8.1-fpm \
    php8.1-cli \
    php8.1-mbstring \
    php8.1-curl \
    php8.1-gd \
    php8.1-mysql \
    php8.1-xml \
    supervisor \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /var/log/supervisord \
    && mkdir -p /var/www/html \
    && chown -R www-data:www-data /var/www/html \
    && chmod 755 /var/www/html

COPY nginx/nginx.conf /etc/nginx/nginx.conf
COPY nginx/conf.d/site.conf /etc/nginx/conf.d/site.conf

COPY supervisord/conf.d/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

COPY html/ /var/www/html/

EXPOSE 80

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

[root@host1 php-nginx-supervisord]# cd php-nginx-supervisord
-bash: cd: php-nginx-supervisord: 没有那个文件或目录
[root@host1 php-nginx-supervisord]# docker build -t php-nginx-supervisor:v1 .
[+] Building 0.9s (12/12) FINISHED                                                    docker:default
 => [internal] load build definition from Dockerfile                                            0.0s
 => => transferring dockerfile: 851B                                                            0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04                                 0.9s
 => [internal] load .dockerignore                                                               0.0s
 => => transferring context: 2B                                                                 0.0s
 => [1/7] FROM docker.io/library/ubuntu:22.04@sha256:4e0171b9275e12d375863f2b3ae9ce00a4c53ddda  0.0s
 => [internal] load build context                                                               0.0s
 => => transferring context: 1.57kB                                                             0.0s
 => CACHED [2/7] RUN apt-get update && apt-get install -y     nginx     php8.1     php8.1-fpm   0.0s
 => CACHED [3/7] RUN mkdir -p /var/log/supervisord     && mkdir -p /var/www/html     && chown   0.0s
 => CACHED [4/7] COPY nginx/nginx.conf /etc/nginx/nginx.conf                                    0.0s
 => CACHED [5/7] COPY nginx/conf.d/site.conf /etc/nginx/conf.d/site.conf                        0.0s
 => [6/7] COPY supervisord/conf.d/supervisord.conf /etc/supervisor/conf.d/supervisord.conf      0.0s
 => [7/7] COPY html/ /var/www/html/                                                             0.0s
 => exporting to image                                                                          0.0s
 => => exporting layers                                                                         0.0s
 => => writing image sha256:0e48e0a7c0344e3e6b9fefb1301c0558125b20196e87b57f5a6e864b57596a52    0.0s
 => => naming to docker.io/library/php-nginx-supervisor:v1                                      0.0s
[root@host1 php-nginx-supervisord]# docker rm -f php-web-app
php-web-app
[root@host1 php-nginx-supervisord]# docker run -d -p 8080:80 --name php-web-app php-nginx-supervisor:v1
3def00b6ca7a99572af5948f3cdbf1fc159ab402885f30275d6d716fe15074ef
[root@host1 php-nginx-supervisord]# docker ps | grep php-web-app
3def00b6ca7a   php-nginx-supervisor:v1   "/usr/bin/supervisor…"   9 seconds ago   Up 9 seconds    0.0
.0.0:8080->80/tcp, [::]:8080->80/tcp       php-web-app
[root@host1 php-nginx-supervisord]# docker logs php-web-app
2025-09-15 02:36:11,439 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
2025-09-15 02:36:11,440 INFO supervisord started with pid 1
2025-09-15 02:36:12,442 INFO spawned: 'nginx' with pid 7
2025-09-15 02:36:12,443 INFO spawned: 'php-fpm' with pid 8
2025-09-15 02:36:12,445 INFO exited: nginx (exit status 1; not expected)
2025-09-15 02:36:13,466 INFO spawned: 'nginx' with pid 11
2025-09-15 02:36:13,466 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2025-09-15 02:36:13,469 INFO exited: nginx (exit status 1; not expected)
2025-09-15 02:36:15,472 INFO spawned: 'nginx' with pid 12
2025-09-15 02:36:15,476 INFO exited: nginx (exit status 1; not expected)
2025-09-15 02:36:18,480 INFO spawned: 'nginx' with pid 13
2025-09-15 02:36:18,483 INFO exited: nginx (exit status 1; not expected)
2025-09-15 02:36:18,483 INFO gave up: nginx entered FATAL state, too many start retries too quickly
[root@host1 php-nginx-supervisord]# docker exec -it php-web-app supervisorctl status
unix:///var/run/supervisor.sock no such file
[root@host1 php-nginx-supervisord]# curl http://localhost:8080
curl: (56) Recv failure: 连接被对方重设
[root@host1 php-nginx-supervisord]# docker exec -it php-web-app bash
root@3def00b6ca7a:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] no "events" section in configuration
nginx: configuration file /etc/nginx/nginx.conf test failed
root@3def00b6ca7a:/# exit
exit
[root@host1 php-nginx-supervisord]# vi nginx.conf
[root@host1 php-nginx-supervisord]# cat nginx.conf
# 本地路径:php-nginx-supervisord/nginx/nginx.conf
user www-data;  # Ubuntu 下 Nginx 默认运行用户,与 PHP-FPM 一致
worker_processes auto;  # 自动匹配 CPU 核心数
pid /run/nginx.pid;     # Nginx 进程 PID 文件路径

# 【必需】events 区块:定义网络连接参数
events {
    worker_connections 1024;  # 每个工作进程的最大连接数(默认1024,可根据需求调整)
}

# http 区块:定义 Web 服务相关配置
http {
    sendfile on;        # 启用高效文件传输模式
    tcp_nopush on;      # 优化 TCP 传输,减少网络包数量
    tcp_nodelay on;     # 禁用 Nagle 算法,降低延迟
    keepalive_timeout 65;  # 客户端连接超时时间
    types_hash_max_size 2048;  # 类型哈希表大小,优化 mime 类型查找

    # 引入 MIME 类型配置(Nginx 自带,无需修改)
    include /etc/nginx/mime.types;
    default_type application/octet-stream;  # 默认 MIME 类型

    # 日志配置(可选,用于排查问题)
    access_log /var/log/nginx/access.log;  # 访问日志
    error_log /var/log/nginx/error.log;    # 错误日志

    # 引入虚拟主机配置(对应 site.conf)
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;    # Ubuntu 默认的虚拟主机目录,可保留
}
[root@host1 php-nginx-supervisord]# cd php-nginx-supervisord
-bash: cd: php-nginx-supervisord: 没有那个文件或目录
[root@host1 php-nginx-supervisord]# docker build -t php-nginx-supervisor:v1 .
[+] Building 1.1s (12/12) FINISHED                                                    docker:default
 => [internal] load build definition from Dockerfile                                            0.0s
 => => transferring dockerfile: 851B                                                            0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04                                 1.1s
 => [internal] load .dockerignore                                                               0.0s
 => => transferring context: 2B                                                                 0.0s
 => [1/7] FROM docker.io/library/ubuntu:22.04@sha256:4e0171b9275e12d375863f2b3ae9ce00a4c53ddda  0.0s
 => [internal] load build context                                                               0.0s
 => => transferring context: 850B                                                               0.0s
 => CACHED [2/7] RUN apt-get update && apt-get install -y     nginx     php8.1     php8.1-fpm   0.0s
 => CACHED [3/7] RUN mkdir -p /var/log/supervisord     && mkdir -p /var/www/html     && chown   0.0s
 => CACHED [4/7] COPY nginx/nginx.conf /etc/nginx/nginx.conf                                    0.0s
 => CACHED [5/7] COPY nginx/conf.d/site.conf /etc/nginx/conf.d/site.conf                        0.0s
 => CACHED [6/7] COPY supervisord/conf.d/supervisord.conf /etc/supervisor/conf.d/supervisord.c  0.0s
 => CACHED [7/7] COPY html/ /var/www/html/                                                      0.0s
 => exporting to image                                                                          0.0s
 => => exporting layers                                                                         0.0s
 => => writing image sha256:44d3a96d479182101e252ee26133d436b9528dc08a0efbcd7e31a58d366e1f64    0.0s
 => => naming to docker.io/library/php-nginx-supervisor:v1                                      0.0s
[root@host1 php-nginx-supervisord]# docker rm -f php-web-app
php-web-app
[root@host1 php-nginx-supervisord]# docker run -d -p 8080:80 --name php-web-app php-nginx-supervisor:v1
6db81738feb7f71f97d6c5d80acb540587ce934760009ba48c00ca1407a72429
[root@host1 php-nginx-supervisord]# docker exec -it php-web-app bash
root@6db81738feb7:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] no "events" section in configuration
nginx: configuration file /etc/nginx/nginx.conf test failed
root@6db81738feb7:/# cd php-nginx-supervisord
bash: cd: php-nginx-supervisord: No such file or directory
root@6db81738feb7:/# exit
exit
[root@host1 php-nginx-supervisord]# cd php-nginx-supervisord
-bash: cd: php-nginx-supervisord: 没有那个文件或目录
[root@host1 php-nginx-supervisord]# cd
[root@host1 ~]# cd php-nginx-supervisord
[root@host1 php-nginx-supervisord]# ls -l
总用量 16
-rw-r--r--. 1 root root  752  9月 15 10:22 Dockerfile
drwxr-xr-x. 2 root root   23  9月 15 10:13 html
-rw-r--r--. 1 root root  249  9月 15 10:20 index.php
drwxr-xr-x. 3 root root   38  9月 15 10:12 nginx
-rw-r--r--. 1 root root 1315  9月 15 10:40 nginx.conf
-rw-r--r--. 1 root root  543  9月 15 10:19 site.conf
drwxr-xr-x. 3 root root   20  9月 15 10:12 supervisord
[root@host1 php-nginx-supervisord]# mv nginx.conf nginx/
mv:是否覆盖'nginx/nginx.conf'? y
[root@host1 php-nginx-supervisord]# cat nginx/nginx.conf
# 本地路径:php-nginx-supervisord/nginx/nginx.conf
user www-data;  # Ubuntu 下 Nginx 默认运行用户,与 PHP-FPM 一致
worker_processes auto;  # 自动匹配 CPU 核心数
pid /run/nginx.pid;     # Nginx 进程 PID 文件路径

# 【必需】events 区块:定义网络连接参数
events {
    worker_connections 1024;  # 每个工作进程的最大连接数(默认1024,可根据需求调整)
}

# http 区块:定义 Web 服务相关配置
http {
    sendfile on;        # 启用高效文件传输模式
    tcp_nopush on;      # 优化 TCP 传输,减少网络包数量
    tcp_nodelay on;     # 禁用 Nagle 算法,降低延迟
    keepalive_timeout 65;  # 客户端连接超时时间
    types_hash_max_size 2048;  # 类型哈希表大小,优化 mime 类型查找

    # 引入 MIME 类型配置(Nginx 自带,无需修改)
    include /etc/nginx/mime.types;
    default_type application/octet-stream;  # 默认 MIME 类型

    # 日志配置(可选,用于排查问题)
    access_log /var/log/nginx/access.log;  # 访问日志
    error_log /var/log/nginx/error.log;    # 错误日志

    # 引入虚拟主机配置(对应 site.conf)
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;    # Ubuntu 默认的虚拟主机目录,可保留
}
[root@host1 php-nginx-supervisord]# docker build --no-cache -t php-nginx-supervisor:v1 .
[+] Building 285.5s (12/12) FINISHED                                                  docker:default
 => [internal] load build definition from Dockerfile                                            0.0s
 => => transferring dockerfile: 851B                                                            0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04                                 0.7s
 => [internal] load .dockerignore                                                               0.0s
 => => transferring context: 2B                                                                 0.0s
 => CACHED [1/7] FROM docker.io/library/ubuntu:22.04@sha256:4e0171b9275e12d375863f2b3ae9ce00a4  0.0s
 => [internal] load build context                                                               0.0s
 => => transferring context: 2.18kB                                                             0.0s
 => [2/7] RUN apt-get update && apt-get install -y     nginx     php8.1     php8.1-fpm     p  284.0s
 => [3/7] RUN mkdir -p /var/log/supervisord     && mkdir -p /var/www/html     && chown -R www-  0.2s 
 => [4/7] COPY nginx/nginx.conf /etc/nginx/nginx.conf                                           0.0s 
 => [5/7] COPY nginx/conf.d/site.conf /etc/nginx/conf.d/site.conf                               0.0s 
 => [6/7] COPY supervisord/conf.d/supervisord.conf /etc/supervisor/conf.d/supervisord.conf      0.0s 
 => [7/7] COPY html/ /var/www/html/                                                             0.0s 
 => exporting to image                                                                          0.6s 
 => => exporting layers                                                                         0.6s
 => => writing image sha256:3793d94deff7f8262c93f7cd867ae9747a3d8a293555d7307aef95d83ec2cf0b    0.0s
 => => naming to docker.io/library/php-nginx-supervisor:v1                                      0.0s
[root@host1 php-nginx-supervisord]# docker rm -f php-web-app
php-web-app
[root@host1 php-nginx-supervisord]# docker run -d -p 8080:80 --name php-web-app php-nginx-supervisor:v1
62b9981bf08e2749b65680b91bca67c3dfdab7a446a169dfd841c0e4088b8f7c
[root@host1 php-nginx-supervisord]# docker exec -it php-web-app bash
root@62b9981bf08e:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@62b9981bf08e:/# exit
exit
[root@host1 php-nginx-supervisord]# curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

更多推荐