在Ubuntu系統中,PHP-FPM(FastCGI Process Manager)與Nginx配合工作可以提供高效的PHP處理能力。以下是配置PHP-FPM與Nginx配合的步驟:
首先,確保你已經安裝了PHP-FPM和Nginx。你可以使用以下命令來安裝它們:
sudo apt update
sudo apt install nginx php-fpm
PHP-FPM的配置文件通常位于 /etc/php/版本號/fpm/pool.d/www.conf。你需要確保PHP-FPM監聽在正確的socket或端口上。
如果你選擇使用Unix Socket,編輯 /etc/php/版本號/fpm/pool.d/www.conf 文件,找到以下行并取消注釋:
listen = /run/php/php7.4-fpm.sock
確保Nginx配置文件中使用相同的socket路徑。
如果你選擇使用TCP端口,編輯 /etc/php/版本號/fpm/pool.d/www.conf 文件,找到以下行并取消注釋:
listen = 127.0.0.1:9000
確保Nginx配置文件中使用相同的端口。
編輯Nginx的默認站點配置文件 /etc/nginx/sites-available/default 或創建一個新的配置文件。
如果你使用Unix Socket,Nginx配置文件中的 location ~ \.php$ 部分應該如下所示:
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;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
如果你使用TCP端口,Nginx配置文件中的 location ~ \.php$ 部分應該如下所示:
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 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
完成配置后,重啟Nginx和PHP-FPM服務以應用更改:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
創建一個簡單的PHP文件(例如 info.php)放在你的網站根目錄下,內容如下:
<?php
phpinfo();
?>
訪問 http://your_domain.com/info.php,如果看到PHP信息頁面,說明配置成功。
通過以上步驟,你就可以在Ubuntu系統中成功配置PHP-FPM與Nginx配合工作。