在CentOS上配置PHP-FPM(FastCGI Process Manager)服務,可以按照以下步驟進行:
首先,確保你的系統已經安裝了PHP。你可以使用以下命令來安裝PHP及其相關模塊:
sudo yum install epel-release
sudo yum install php php-fpm
PHP-FPM的配置文件通常位于 /etc/php-fpm.d/www.conf
。你可以根據需要進行修改。
默認情況下,PHP-FPM監聽Unix套接字。如果你希望它監聽TCP端口,可以修改 listen
參數。例如,監聽在 127.0.0.1:9000
:
listen = 127.0.0.1:9000
確保PHP-FPM以正確的用戶和組運行。通常是 apache
或 www-data
,具體取決于你的Web服務器配置。
user = apache
group = apache
你可以根據需要調整其他參數,例如進程管理、日志級別等。
啟動PHP-FPM服務并設置開機自啟:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
根據你使用的Web服務器(如Apache或Nginx),配置它以使用PHP-FPM處理PHP請求。
如果你使用的是Apache,確保啟用了 proxy_fcgi
模塊,并配置虛擬主機以使用PHP-FPM。
sudo yum install mod_proxy_fcgi
編輯Apache配置文件(例如 /etc/httpd/conf/httpd.conf
或 /etc/httpd/conf.d/vhost.conf
),添加以下內容:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
</VirtualHost>
重啟Apache服務:
sudo systemctl restart httpd
如果你使用的是Nginx,配置它以將PHP請求轉發給PHP-FPM。
編輯Nginx配置文件(例如 /etc/nginx/nginx.conf
或 /etc/nginx/conf.d/default.conf
),添加以下內容:
server {
listen 80;
server_name example.com;
root /var/www/html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
重啟Nginx服務:
sudo systemctl restart nginx
創建一個簡單的PHP文件(例如 /var/www/html/index.php
),內容如下:
<?php
phpinfo();
?>
訪問該文件,如果看到PHP信息頁面,說明配置成功。
curl http://example.com/index.php
通過以上步驟,你應該能夠在CentOS上成功配置PHP-FPM服務。