在CentOS系统中,使用LNMP(Linux, Nginx, MySQL, PHP)搭建网站时,Nginx需要与PHP-FPM进行通信以处理PHP脚本。以下是配置Nginx与PHP-FPM通信的步骤:
首先,确保你已经安装了Nginx和PHP-FPM。你可以使用以下命令进行安装:
sudo yum install epel-release
sudo yum install nginx php-fpm
编辑PHP-FPM的配置文件 /etc/php-fpm.d/www.conf
,确保监听地址和端口设置正确。通常情况下,默认配置即可:
listen = /run/php-fpm/www.sock
;listen = 127.0.0.1:9000
如果你希望PHP-FPM监听TCP端口而不是Unix套接字,可以将 listen
行改为:
listen = 127.0.0.1:9000
编辑Nginx的配置文件 /etc/nginx/nginx.conf
或者在 /etc/nginx/conf.d/
目录下创建一个新的配置文件(例如 default.conf
),添加或修改以下内容:
server {
listen 80;
server_name your_domain.com; # 替换为你的域名或IP地址
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-fpm/www.sock; # 如果使用Unix套接字
;fastcgi_pass 127.0.0.1:9000; # 如果使用TCP端口
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
启动Nginx和PHP-FPM服务,并设置它们在系统启动时自动启动:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
确保Nginx和PHP-FPM的配置没有错误:
sudo nginx -t
sudo systemctl status nginx
sudo systemctl status php-fpm
最后,重启Nginx以应用新的配置:
sudo systemctl restart nginx
现在,Nginx应该能够与PHP-FPM正常通信,并处理PHP脚本了。你可以通过访问你的网站来测试配置是否成功。