在CentOS上将PHP-FPM与Nginx联调,以便处理PHP请求,可以按照以下步骤进行:
首先,确保你的系统是最新的,并安装Nginx和PHP-FPM。
sudo yum update -y
sudo yum install epel-release -y
sudo yum install nginx php-fpm php-mysqlnd -y
编辑PHP-FPM的配置文件 /etc/php-fpm.d/www.conf,确保以下配置正确:
user = nginx
group = nginx
listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
编辑Nginx的默认站点配置文件 /etc/nginx/conf.d/default.conf,添加或修改以下内容:
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;
}
location ~ /\.ht {
deny all;
}
}
sudo systemctl start nginx
sudo systemctl enable nginx
创建一个简单的PHP文件来测试配置是否正确。在 /usr/share/nginx/html 目录下创建一个名为 info.php 的文件:
echo "<?php phpinfo(); ?>" | sudo tee /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服务:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
通过以上步骤,你应该能够在CentOS上成功地将PHP-FPM与Nginx联调,处理PHP请求。