PHP-FPM(FastCGI Process Manager)是一个PHP FastCGI实现,具有进程管理功能。以下是在Linux系统中安装和配置PHP-FPM的步骤:
对于基于Debian的系统(如Ubuntu):
sudo apt update
sudo apt install php-fpm
对于基于Red Hat的系统(如CentOS):
sudo yum install php-fpm
PHP-FPM的配置文件通常位于/etc/php/{version}/fpm/目录下,其中{version}是PHP的版本号。
打开主配置文件:
sudo nano /etc/php/{version}/fpm/php-fpm.conf
池配置文件通常位于/etc/php/{version}/fpm/pool.d/目录下,例如www.conf:
sudo nano /etc/php/{version}/fpm/pool.d/www.conf
在www.conf中,你可以配置以下参数:
listen: 监听的地址和端口,例如/run/php/php7.4-fpm.sock或127.0.0.1:9000。user 和 group: 运行PHP-FPM进程的用户和组。pm: 进程管理方式,可选值有dynamic、static、ondemand等。pm.max_children: 最大子进程数。pm.start_servers: 启动时的服务器进程数。pm.min_spare_servers: 最小空闲服务器进程数。pm.max_spare_servers: 最大空闲服务器进程数。sudo systemctl start php{version}-fpm
sudo systemctl enable php{version}-fpm
编辑Nginx配置文件(例如/etc/nginx/sites-available/default):
server {
listen 80;
server_name example.com;
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/php7.4-fpm.sock; # 或者使用127.0.0.1:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
确保启用了mod_proxy_fcgi模块:
sudo a2enmod proxy_fcgi setenvif
编辑Apache配置文件(例如/etc/apache2/sites-available/000-default.conf):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<FilesMatch \.php$>
SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>
</VirtualHost>
重启Nginx或Apache服务以应用更改:
sudo systemctl restart nginx
# 或者
sudo systemctl restart apache2
创建一个PHP文件(例如/var/www/html/info.php)并添加以下内容:
<?php
phpinfo();
?>
访问该文件(例如http://example.com/info.php),如果看到PHP信息页面,则配置成功。
通过以上步骤,你应该能够在Linux系统中成功安装和配置PHP-FPM,并使其与Web服务器(如Nginx或Apache)协同工作。