您的位置:首页 >php-fpm与nginx如何协同工作centos
发布于2026-07-31 阅读(0)
扫一扫,手机访问
在CentOS系统里,PHP-FPM和Nginx是怎么配合起来处理PHP请求的?下面就把配置步骤掰开揉碎了说清楚,照着操作就能搞定。

先把系统更新到最新版本,这一步不能省。
sudo yum update -y
然后安装Nginx和PHP-FPM:
sudo yum install nginx php-fpm -y
启动服务,并且设置开机自启,这样重启后也能自动跑起来:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
编辑PHP-FPM的配置文件,位于/etc/php-fpm.d/www.conf,确保监听地址和端口正确。这里有个关键点,用户和组要设置成nginx,不然权限会出问题:
listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx
编辑Nginx的默认站点配置文件,比如/etc/nginx/conf.d/default.conf,添加处理PHP请求的规则。注意location块里要指定fastcgi_pass指向刚才配置的socket文件:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
配置改完后,重启Nginx和PHP-FPM服务,让新配置生效:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
创建一个简单的PHP文件来测试一下。在/usr/share/nginx/html目录下新建info.php,写入内容:
然后在浏览器里访问http://your_server_ip/info.php,如果看到PHP信息页面,说明配置成功。
如果启用了防火墙,记得放行HTTP和HTTPS流量:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
跟着这些步骤走,Nginx和PHP-FPM就能在CentOS上顺利配合,高效处理PHP请求了。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8