在CentOS上配置PHP-FPM与Nginx的步骤如下:
首先,确保你的系统是最新的,并安装Nginx和PHP-FPM。
sudo yum update -y
sudo yum install epel-release -y
sudo yum install nginx php-fpm php-mysqlnd -y
启动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,确保以下配置正确:
; Unix user/group of processes
user = nginx
group = nginx
; Listen for local connection on Unix socket
listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
; User and group of processes
user = nginx
group = nginx
; Risky setting, only enable if you know what you're doing
;listen.backlog = -1
; Set worker processes
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
; Maximum request time in seconds
request_terminate_timeout = 0
编辑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;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
应用配置更改后,重启Nginx和PHP-FPM服务。
sudo systemctl restart nginx
sudo systemctl restart php-fpm
创建一个简单的PHP文件来测试配置是否正确。
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/index.php
在浏览器中访问 http://your_domain.com/index.php,你应该能看到PHP信息页面。
如果你使用的是CentOS 7或更高版本,并且启用了防火墙,确保开放HTTP和HTTPS端口。
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
完成以上步骤后,你应该已经成功在CentOS上配置了PHP-FPM与Nginx。