PHP-FPM(FastCGI Process Manager)和Nginx是两个常用的Web服务器组件,它们可以协同工作以提供高性能的PHP应用程序服务。以下是在Linux上配置PHP-FPM与Nginx协同工作的基本步骤:

首先,确保你的系统上已经安装了Nginx和PHP-FPM。你可以使用包管理器来安装它们。
sudo apt updatesudo apt install nginx php-fpmsudo yum install epel-releasesudo yum install nginx php-fpmPHP-FPM的配置文件通常位于 /etc/php/版本号/fpm/pool.d/www.conf。你需要确保PHP-FPM监听在Unix socket上,而不是TCP端口。
编辑 /etc/php/版本号/fpm/pool.d/www.conf 文件:
sudo nano /etc/php/版本号/fpm/pool.d/www.conf找到以下行并修改:
listen = /run/php/php版本号-fpm.sockNginx需要配置一个location块来处理PHP请求,并将它们传递给PHP-FPM。
编辑Nginx的默认站点配置文件:
sudo nano /etc/nginx/sites-available/default添加或修改以下内容:
server {listen 80;server_name your_domain.com;root /var/www/html;index index.php index.html index.htm;location / {try_files $uri $uri/ =404;}location ~ .php$ {include snippets/fastcgi-php.conf;fastcgi_pass unix:/run/php/php版本号-fpm.sock;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}}启动并启用Nginx和PHP-FPM服务:
sudo systemctl start nginxsudo systemctl enable nginxsudo systemctl start php版本号-fpmsudo systemctl enable php版本号-fpmsudo systemctl start nginxsudo systemctl enable nginxsudo systemctl start php-fpmsudo systemctl enable php-fpm确保Nginx和PHP-FPM都在运行,并且可以正确处理PHP请求。
访问你的域名或服务器IP地址,你应该能够看到PHP文件被正确解析和执行。
如果你使用的是防火墙(如ufw),确保开放HTTP(80)和HTTPS(443)端口:
sudo ufw allow 'Nginx Full'如果遇到问题,检查Nginx和PHP-FPM的日志文件以获取更多信息:
/var/log/nginx/error.log/var/log/php版本号-fpm.log通过以上步骤,你应该能够在Linux上成功配置PHP-FPM与Nginx协同工作,提供高性能的PHP应用程序服务。