溫馨提示×

ubuntu中php-fpm如何與Nginx配合

小樊
43
2025-10-18 14:14:35
欄目: 編程語言

在Ubuntu系統中,PHP-FPM(FastCGI Process Manager)與Nginx配合工作可以提供高效的PHP處理能力。以下是配置PHP-FPM與Nginx配合的步驟:

1. 安裝PHP-FPM和Nginx

首先,確保你已經安裝了PHP-FPM和Nginx。你可以使用以下命令來安裝它們:

sudo apt update
sudo apt install nginx php-fpm

2. 配置PHP-FPM

PHP-FPM的配置文件通常位于 /etc/php/版本號/fpm/pool.d/www.conf。你需要確保PHP-FPM監聽在正確的socket或端口上。

使用Unix Socket

如果你選擇使用Unix Socket,編輯 /etc/php/版本號/fpm/pool.d/www.conf 文件,找到以下行并取消注釋:

listen = /run/php/php7.4-fpm.sock

確保Nginx配置文件中使用相同的socket路徑。

使用TCP端口

如果你選擇使用TCP端口,編輯 /etc/php/版本號/fpm/pool.d/www.conf 文件,找到以下行并取消注釋:

listen = 127.0.0.1:9000

確保Nginx配置文件中使用相同的端口。

3. 配置Nginx

編輯Nginx的默認站點配置文件 /etc/nginx/sites-available/default 或創建一個新的配置文件。

使用Unix Socket

如果你使用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端口

如果你使用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;
    }
}

4. 重啟服務

完成配置后,重啟Nginx和PHP-FPM服務以應用更改:

sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm

5. 測試配置

創建一個簡單的PHP文件(例如 info.php)放在你的網站根目錄下,內容如下:

<?php
phpinfo();
?>

訪問 http://your_domain.com/info.php,如果看到PHP信息頁面,說明配置成功。

通過以上步驟,你就可以在Ubuntu系統中成功配置PHP-FPM與Nginx配合工作。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女