在Linux系统中配置PHP-FPM(FastCGI Process Manager)通常涉及以下几个步骤。以下是一个基本的指南,假设你使用的是基于Debian的系统(如Ubuntu),但大多数步骤在其他Linux发行版中也适用。
首先,你需要安装PHP-FPM。你可以使用包管理器来安装它。例如,在Ubuntu上:
sudo apt update
sudo apt install php-fpm
安装完成后,你需要配置PHP-FPM。PHP-FPM的配置文件通常位于 /etc/php/{version}/fpm/pool.d/www.conf
,其中 {version}
是你安装的PHP版本号。
打开配置文件进行编辑:
sudo nano /etc/php/{version}/fpm/pool.d/www.conf
你可以根据需要修改以下参数:
listen
: 指定PHP-FPM监听的地址和端口。默认情况下,它可能是 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
: 最大空闲服务器进程数。例如:
listen = 127.0.0.1:9000
user = www-data
group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
保存并关闭配置文件后,重启PHP-FPM服务以应用更改:
sudo systemctl restart php{version}-fpm
例如,如果你安装的是PHP 7.4:
sudo systemctl restart php7.4-fpm
接下来,你需要配置你的Web服务器(如Nginx或Apache)以使用PHP-FPM处理PHP请求。
如果你使用的是Nginx,编辑你的站点配置文件(通常位于 /etc/nginx/sites-available/{your-site}
):
sudo nano /etc/nginx/sites-available/{your-site}
添加以下内容:
server {
listen 80;
server_name example.com www.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:/var/run/php/php{version}-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
例如,如果你使用的是PHP 7.4:
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
保存并关闭文件后,重启Nginx服务:
sudo systemctl restart nginx
如果你使用的是Apache,确保启用了 mod_proxy_fcgi
和 mod_proxy
模块,并编辑你的站点配置文件(通常位于 /etc/apache2/sites-available/{your-site}
):
sudo nano /etc/apache2/sites-available/{your-site}
添加以下内容:
<VirtualHost *:80>
ServerName example.com www.example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>
</VirtualHost>
保存并关闭文件后,重启Apache服务:
sudo systemctl restart apache2
最后,测试你的配置是否正确。创建一个简单的PHP文件(例如 info.php
)并将其放在你的Web服务器文档根目录下:
<?php
phpinfo();
?>
访问 http://example.com/info.php
,你应该能看到PHP信息页面,表明PHP-FPM正在正常工作。
通过以上步骤,你应该能够在Linux系统中成功配置PHP-FPM。