在CentOS上配置PHP-FPM(FastCGI Process Manager)通常涉及以下几个步骤:
安装PHP和PHP-FPM:
首先,你需要安装PHP以及PHP-FPM。你可以使用yum
包管理器来安装它们。
sudo yum install epel-release
sudo yum install php php-fpm
配置PHP-FPM:
安装完成后,你需要配置PHP-FPM。PHP-FPM的配置文件通常位于/etc/php-fpm.d/
目录下,主要的配置文件是www.conf
。
sudo vi /etc/php-fpm.d/www.conf
在这个文件中,你可以配置以下参数:
user
和 group
:指定运行PHP-FPM进程的用户和组。listen
:指定PHP-FPM监听的地址和端口。例如,/run/php-fpm/www.sock
或 127.0.0.1:9000
。pm
:指定进程管理方式,常用的有dynamic
、static
、ondemand
等。pm.max_children
、pm.start_servers
、pm.min_spare_servers
、pm.max_spare_servers
:这些参数用于控制进程管理。例如,如果你想让PHP-FPM监听Unix socket,可以这样配置:
listen = /run/php-fpm/www.sock
listen.owner = apache
listen.group = apache
user = apache
group = apache
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
启动和启用PHP-FPM服务: 配置完成后,你可以启动PHP-FPM服务并设置开机自启动。
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
配置Web服务器: 如果你使用的是Apache或Nginx作为Web服务器,你需要配置它们以使用PHP-FPM处理PHP请求。
Apache:
确保启用了mod_proxy_fcgi
模块,并在虚拟主机配置中添加以下内容:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>
Nginx: 在Nginx配置文件中添加以下内容:
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
重启Web服务器: 最后,重启你的Web服务器以应用更改。
Apache:
sudo systemctl restart httpd
Nginx:
sudo systemctl restart nginx
完成以上步骤后,你的CentOS系统应该已经成功配置了PHP-FPM,并且可以通过Web服务器处理PHP请求了。