
我的問題很簡單(我希望如此)。
我在網路上到處都讀到,如果我想更改 Nginx 中的 PHP 版本,我需要更改fastcgi_pass
參數的 UNIX 套接字。但我的fastcgi_pass
參數指向一個 TCP 套接字:127.0.0.1:9250
。我嘗試將其更改為 UNIX 套接字,但這給了我 502 Bad Gateway 錯誤,並且我不知道如何從那裡繼續。
我目前運行的是 PHP 8.2,但我需要 PHP 8.1。我已經使用安裝了它apt-get
,並且在執行時列出了它sudo update-alternatives --list php
。我嘗試了該sudo update-alternatives
方法,但這只會更改 CLI 中的 PHP 版本。不是 FPM。
我的 nginx 設定非常簡單:
/etc/nginx/nginx.conf
:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
# master_process off;
events {
worker_connections 768;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
}
/etc/nginx/sites-enabled/daan.conf
:
server {
listen 80;
server_name "~(?'project'([^.]+))\.dev.local$";
return 301 https://$project.dev.local$request_uri;
}
server {
listen 443 ssl http2;
server_name "~(?'project'([^.]+))\.dev.local$";
root "/etc/nginx/development/$project";
index index.php;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
ssl_prefer_server_ciphers on;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /var/log/nginx/dev.log;
error_log /var/log/nginx/dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_pass 127.0.0.1:9250;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
中的listen 參數etc/php/8.2/fpm/pool.d/www.conf
指向同一個TCP 套接字:127.0.0.1:9250
。
編輯:根據要求,這是我更改的內容/etc/php/8.1/fpm/pool.d/www.conf
。這些編輯在/etc/php/8.2/fpm/pool.d/www.conf
.
; Unix user/group of the child processes. This can be used only if the master
; process running user is root. It is set after the child process is created.
; The user and group can be specified either by their name or by their numeric
; IDs.
; Note: If the user is root, the executable needs to be started with
; --allow-to-run-as-root option to work.
; Default Values: The user is set to master process running user by default.
; If the group is not set, the user's group is used.
user = daanvandenbergh
group = daanvandenbergh
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9250
答案1
你的問題有點困惑 - 至少有3種配置(8.2帶網路套接字,8.1帶網路套接字,8.1帶網路套接字)只有其中一種配置的nginxconf,沒有詳細說明如何更改php-fpm配置。
我嘗試將其更改為 UNIX 套接字,但這給了我 502 Bad Gateway 錯誤
您需要伺服器在用戶端嘗試連線的位置進行偵聽。 Unix 網域套接字(與TCP 套接字相比)的另一個複雜之處在於,它要求檔案有一個擁有者和一個群組(通常與Web 伺服器運行的uid 相同的擁有者),例如在www. conf 中
listen = /var/run/php8.1-fpm.sock
listen.owner = www-data
listen.group = www-data
你還需要成為非常如果同一主機上安裝了兩個版本的 PHP,請小心。在嘗試安裝新的之前,我會確保您不想要的那個已被完全刪除。
除非您明確需要 TCP 套接字,否則我建議使用 Unix 網域套接字。儘管 localhost 比綁定到實體介面的位址快得多,並且具有隱含的安全優勢,但 Unix 套接字仍然更快。
答案2
也許我的問題不清楚,我不是系統管理員,所以對我來說這並不像應該的那麼清楚。然而,閱讀 @symcbean 的答案讓我清楚地意識到,切換到 UNIX 套接字不應該成為問題的根源——這是我在編寫問題時所假設的。所以,我決定走這條路,處理路上遇到的一切。
以下是我切換到另一個 PHP 版本的編輯,作為額外的好處,我弄清楚如何讓我輕鬆地在 Nginx 中的 PHP 版本之間切換:
我將fastcgi_pass
參數更改/etc/nginx/sites-enabled/daan.conf
為/var/run/php/php-fpm.sock
.
我將listen
參數更改/etc/php8.1/fpm/pool.d/www.conf
為相同的值。我對/etc/php8.2/fpm/pool.d/www.conf
和 也做了同樣的事情/etc/php8.0/fpm/pool.d/www.conf
。
現在,執行時sudo update-alternatives --config php
,該php-fpm.sock
檔案將成為所選 PHP 版本的符號鏈接,PHP FPM 和 Nginx 都會監聽該版本。
之後(這部分可能導致我在之前的問題中提到的錯誤網關錯誤)正確重新啟動 Nginx 和 PHP-FPM 很重要(我從這裡):
sudo mkdir -p /var/run/php
sudo service restart nginx
sudo service php(old version)-fpm stop -v
sudo service php(new version)-fpm start -v
sudo chown mysql:mysql /var/run/mysqld
sudo service mysql start
現在可以了。