在Ubuntu系统中,PHP-FPM(FastCGI Process Manager)通常用于处理PHP脚本的执行。它是一个替代传统的mod_php的解决方案,提供了更好的性能和资源管理。PHP-FPM可以与其他Web服务器(如Nginx或Apache)以及系统服务协同工作。以下是一些常见的配置示例:
安装PHP-FPM:
sudo apt update
sudo apt install php-fpm
配置Nginx:
编辑Nginx的站点配置文件(通常位于/etc/nginx/sites-available/目录下),添加或修改以下内容:
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:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
启动并启用PHP-FPM:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
测试配置: 重新加载Nginx配置并测试:
sudo nginx -t
sudo systemctl reload nginx
安装PHP-FPM:
sudo apt update
sudo apt install php-fpm
配置Apache: 启用必要的Apache模块:
sudo a2enmod proxy_fcgi setenvif
sudo systemctl restart apache2
编辑Apache的站点配置文件(通常位于/etc/apache2/sites-available/目录下),添加或修改以下内容:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
启动并启用PHP-FPM:
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
测试配置: 重新加载Apache配置并测试:
sudo apache2ctl configtest
sudo systemctl reload apache2
PHP-FPM通常独立的服务运行,与其他系统服务的协同工作主要体现在它如何处理来自Web服务器的请求。以下是一些常见的协同工作场景:
PHP-FPM在Ubuntu系统中与其他服务的协同工作主要通过Web服务器(如Nginx或Apache)进行请求转发和处理。确保PHP-FPM服务正常运行,并正确配置Web服务器以将PHP请求转发给PHP-FPM,即可实现高效的协同工作。