在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,即可實現高效的協同工作。