簡體   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