在CentOS系统下,配置Nginx以支持PHP处理需要以下几个步骤:
首先,确保已经安装了Nginx和PHP-FPM。如果尚未安装,请按照以下命令进行安装:
sudo yum install epel-release
sudo yum install nginx
sudo yum install php-fpm
启动Nginx和PHP-FPM服务,并将它们设置为开机自启:
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),找到以下行:
;listen = 127.0.0.1:9000
取消注释(删除行首的分号),以便PHP-FPM监听9000端口。
编辑Nginx配置文件(通常位于/etc/nginx/nginx.conf或/etc/nginx/conf.d/default.conf),在server块中添加以下内容:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
这段配置告诉Nginx将所有以.php结尾的请求转发给PHP-FPM处理。
为了使更改生效,请重启Nginx和PHP-FPM服务:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
现在,Nginx应该已经成功配置为使用PHP-FPM处理PHP请求。可以通过创建一个名为info.php的文件(例如,在/var/www/html目录下)并输入以下内容来测试配置:
<?php
phpinfo();
?>
在浏览器中访问http://your_server_ip/info.php,如果看到PHP信息页面,则表示配置成功。