【问题标题】:Minimal Docker Nginx config for multiple PHP-FPM virtual hosts多个 PHP-FPM 虚拟主机的最小 Docker Nginx 配置
【发布时间】:2021-06-20 15:38:35
【问题描述】:

我需要升级一些仍然运行 PHP 5.6 的非常旧的 PHP 站点,并且由于我还有其他运行 PHP 7 和 8 的站点,我想我会利用 Docker 来解决这个问题。

我正在使用来自hub.docker.com 的默认 Docker PHP-FPM 和 Nginx 映像。

我在 Ubuntu 20 下使用 WSL2 在 Docker for Windows 上运行它。如果您也在 Windows 10 上,here is the best guide I could find on setting up WSL2 with Docker for PHP Development to work flawlessly

Dockerfile

FROM nginx

COPY nginx.conf /etc/nginx/nginx.conf
COPY site1.conf /etc/nginx/conf.d/site1.conf
COPY site2.conf /etc/nginx/conf.d/site2.conf

nginx.conf

这是 /etc/nginx/nginx.conf 中的 nginx 官方镜像中的默认设置,除了注明的 2 行。

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    # added this once some other SO posts suggested it for multiple server_name directives
    server_names_hash_bucket_size 64;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;

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

    # had to add this, the default nginx.conf in the docker image doesn't include sites-enabled
    include /etc/nginx/sites-enabled/*.conf;
}

site1.conf

server {
    listen         80 default_server;
    listen         [::]:80 default_server;
    server_name    example1.com www.example1.com;
    root           /var/www/example1.com;
    index          index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

site2.conf

server {
    listen         80;
    listen         [::]:80;
    server_name    example2.com www.example2.com;
    root           /var/www/example2.com;
    index          index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

目录结构

/home/me/nginx-test/Dockerfile
/home/me/nginx-test/site1/index.html
/home/me/nginx-test/site1/test.php
/home/me/nginx-test/site2/index.html
/home/me/nginx-test/site2/test.php

<h1>Site1</h1> 分别放入site1/index.html 和<h1>Site2</h1> 放入site2/index.html,因此很明显我们加载的是正确的。

/etc/hosts

127.0.0.1  example1.com
127.0.0.1  example2.com

这是我站起来测试一切的方法。

$ cd ~/nginx-test
$ docker pull nginx
$ docker pull php:5.6.40-fpm
$ docker build -t web:test .
$ docker run --name php -v /home/me/nginx-test/site1:/var/www/example1 -v /home/me/nginx-test/site2:/var/www/example2 -d php:5.6.40-fpm
$ docker run --name test -p 80:80 -v /home/me/nginx-test/site1:/var/www/example1 -v /home/me/nginx-test/site2:/var/www/example2 --link php:php -d web:test

请注意,您必须先启动 php 容器,并将其命名为 php,这样当您将 web 容器和 --link 放到 php 容器中时,它可以找到它。鉴于两个 nginx 配置文件中的 fastcgi_pass php:9000 指令,这也是必需的。

docker ps -a

$ docker ps -a
CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS         PORTS                NAMES
37edb342d7b2   web:test         "/docker-entrypoint.…"   5 seconds ago    Up 4 seconds   0.0.0.0:80->80/tcp   test
45ac2fb3d1d4   php:5.6.40-fpm   "docker-php-entrypoi…"   10 seconds ago   Up 9 seconds   9000/tcp             php

现在,当我浏览到 http://example1.comhttp://example2.com 时,我会看到相应的 index.html 页面。

docker 日志测试 --follow

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [23/Mar/2021:22:29:19 +0000] "GET / HTTP/1.1" 200 135 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" "-"

现在为了更容易地 grep 日志,让我们将 $server_name 添加到 nginx.conf log_format。更多详情请关注offical list of nginx variables

nginx.conf 编辑,第 16 行

    log_format  main  '$remote_addr - $remote_user [$time_local] "$server_name $request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

拆除和重建

$ docker stop test && docker rm test && docker stop php && docker rm php
$ docker build -t web:test .
$ docker run --name php -v /home/me/nginx-test/site1:/var/www/example1 -v /home/me/nginx-test/site2:/var/www/example2 -d php:5.6.40-fpm
$ docker run --name test -p 80:80 -v /home/me/nginx-test/site1:/var/www/example1 -v /home/me/nginx-test/site2:/var/www/example2 --link php:php -d web:test
$ docker logs test --follow
172.17.0.1 - - [23/Mar/2021:22:41:25 +0000] "example2.com GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" "-"
172.17.0.1 - - [23/Mar/2021:22:41:36 +0000] "example1.com GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" "-"
172.17.0.1 - - [23/Mar/2021:22:41:41 +0000] "example1.com GET /test.php HTTP/1.1" 200 85924 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" "-"

这里有更多诊断细节。

卷是否已安装?

$ docker exec -it test bash
root@37edb342d7b2:/# cd /var/www/example1
root@37edb342d7b2:/var/www/example1# ls
index.html  test.php

是的。

在两个容器中?

$ docker exec -it php bash
root@45ac2fb3d1d4:/var/www/html# ls -la /var/www/example2
total 16
drwxr-xr-x 2 1000 1000 4096 Mar 23 21:47 .
drwxr-xr-x 1 root root 4096 Mar 23 22:21 ..
-rw-r--r-- 1 1000 1000  135 Mar 23 21:37 index.html
-rw-r--r-- 1 1000 1000   31 Mar 23 20:33 test.php

【问题讨论】:

    标签: php docker nginx


    【解决方案1】:

    在提出一些问题的情况下,我想通了一切,所以我决定继续保持这一点,作为未来读者的指南。

    阅读上面的指南。

    【讨论】:

      猜你喜欢
      • 2014-10-12
      • 2019-01-25
      • 2011-02-22
      • 2011-08-17
      • 2014-07-07
      • 2014-04-23
      • 2023-03-18
      • 2017-09-25
      相关资源
      最近更新 更多