在Ubuntu中配置PHP-FPM(FastCGI Process Manager)通常涉及以下几个步骤:
安装PHP-FPM:
使用以下命令来安装PHP-FPM:
sudo apt update
sudo apt install php-fpm
配置PHP-FPM:
PHP-FPM的主要配置文件是 /etc/php/{version}/fpm/pool.d/www.conf
,其中 {version}
是你安装的PHP版本。例如,对于PHP 7.4,路径将是 /etc/php/7.4/fpm/pool.d/www.conf
。
使用文本编辑器打开配置文件进行编辑,例如使用 nano
:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
在 www.conf
文件中,你可以配置以下参数:
listen
:指定PHP-FPM监听的地址和端口。默认情况下,它监听 /run/php/php7.4-fpm.sock
。如果你想使用TCP/IP端口,可以设置为 127.0.0.1:9000
。user
和 group
:指定运行PHP-FPM进程的用户和组。通常,这应该是 www-data
:www-data
。pm
:进程管理方式,可以是 dynamic
、static
或 ondemand
。pm.max_children
:最大子进程数。pm.start_servers
:启动时的服务器进程数。pm.min_spare_servers
:最小空闲服务器进程数。pm.max_spare_servers
:最大空闲服务器进程数。保存并关闭配置文件后,重启PHP-FPM服务以应用更改:
sudo systemctl restart php7.4-fpm
配置Web服务器:
Nginx配置:
编辑Nginx的站点配置文件(通常位于 /etc/nginx/sites-available/default
或 /etc/nginx/sites-enabled/your-site
),并添加或修改以下内容:
server {
listen 80;
server_name your_domain.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; # 或者使用 tcp:127.0.0.1:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
然后重启Nginx:
sudo systemctl reload nginx
Apache配置:
确保启用了 proxy_fcgi
和 setenvif
模块:
sudo a2enmod proxy_fcgi setenvif
编辑Apache的站点配置文件(通常位于 /etc/apache2/sites-available/your-site.conf
),并添加或修改以下内容:
<VirtualHost *:80>
ServerName your_domain.com
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:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
然后重启Apache:
sudo systemctl reload apache2
测试配置:
创建一个简单的PHP文件(例如 info.php
),内容如下:
<?php
phpinfo();
?>
将这个文件放在你的Web服务器的根目录下,并通过浏览器访问它。如果一切配置正确,你应该能够看到PHP的信息页面。
请注意,这些步骤可能会根据你的具体需求和Ubuntu版本有所不同。始终确保查阅最新的官方文档以获取最准确的指导。