繁体   English   中英

链接 NGINX + PHP-FPM on Docker [使用 Docker 运行]?

[英]Link NGINX + PHP-FPM on Docker [Using Docker Run]?

我试图运行 NGINX + PHP-FPM 应用程序,但我在这里和那里卡住了。

我在 StackOverFlow 上关注了这个主要问题: 问题浏览了所有评论,进行了很多更改,但都没有奏效......

每次我打开 PHP 文件时,我的浏览器都会下载它,而不是在 PHP FPM 上运行脚本。

我所做的步骤如下:

# Create Network
docker network create nginx-php
docker network ls

# Run NGINX and PHP-FPM.
docker run -d -p 9000:9000 --name php-fpm `
 --network=nginx-php `
 -v D:\Docker\Html:/usr/share/nginx/html php:7.4-fpm-alpine

docker run --name nginx -p 80:80 `
 --network=nginx-php `
 -v D:\Docker\Html:/usr/share/nginx/html `
 -v D:\Docker\Config\nginx:/etc/nginx `
 -d nginx:1.23.1-alpine

我的 NGINX 配置文件:

user  nginx;
worker_processes  auto;

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

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    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;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;

    server {
      listen 80 default_server;
      listen [::]:80 default_server;
      root /usr/share/nginx/html;
      index index.php index.html index.htm index.nginx-debian.html;

      server_name _;

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

      location ~ \.php$ {
        fastcgi_pass php-fpm:9000; # Nome do container PHP-FPM e onde ele ta hospedado
        fastcgi_index index.php;
        include fastcgi_params; # Adaptado para Docker
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }

      location ~ /\.ht {
        deny all;
      }
    } # End of PHP FPM Nginx config example
}

如您所见,带有 PHP-FPM Pass 的块与问题和文档中所述的块完全相同。

所以我真的不知道发生了什么,有人可以帮助我吗?

PS: Just for clarity, you may be wondering why not take the Docker Compose / DockerFile approach, but i am taking this approach because im currently studying docker and do not know how Docker Compose / DockerFile works that well, so i sticky with the basics目前。

编辑:我发现问题只发生在使用 TCP 连接到容器的 fastcgi_pass 上。 如果我从 PHP-FPM 复制 /run/php/ 并将其作为卷安装在两个容器上,并使用 unix:/run/php/php7.4-fpm.sock 而不是 php-fpm:9000 它可以工作... ._. 是的,但我仍然想使用 TCP 方法,因为它可以更快地部署在任何计算机上。

出于某种原因,在 conf.d 中使用 default.conf 中的“服务器块”而不是留在 HTTP 块中解决了问题......

真的不知道为什么,我只是对其进行了测试,看看它是否可以正常工作。

所以,nginx.conf 保持不变(等于原来的......没有服务器块)和 Default.conf 保持这样:

server {
      listen 80 default_server;
      listen [::]:80 default_server;
      root /usr/share/nginx/html;
      index index.php index.html index.htm index.nginx-debian.html;

      server_name _;

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

      location ~ \.php$ {
        fastcgi_pass php-fpm:9000; # Nome do container PHP-FPM e onde ele ta hospedado
        fastcgi_index index.php;
        include fastcgi_params; # Adaptado para Docker
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }

      location ~ /\.ht {
        deny all;
      }
    } # End of PHP FPM Nginx config example

Go 图♂️

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM