溫馨提示×

PHP-FPM與Nginx在Linux上如何協同

小樊
50
2025-09-29 07:07:46
欄目: 編程語言

PHP-FPM(FastCGI Process Manager)和Nginx是兩個常用的Web服務器組件,它們可以協同工作以提供高性能的PHP應用程序服務。以下是在Linux上配置PHP-FPM與Nginx協同工作的基本步驟:

1. 安裝Nginx和PHP-FPM

首先,確保你的系統上已經安裝了Nginx和PHP-FPM。你可以使用包管理器來安裝它們。

在Debian/Ubuntu上:

sudo apt update
sudo apt install nginx php-fpm

在CentOS/RHEL上:

sudo yum install epel-release
sudo yum install nginx php-fpm

2. 配置PHP-FPM

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

編輯 /etc/php/版本號/fpm/pool.d/www.conf 文件:

sudo nano /etc/php/版本號/fpm/pool.d/www.conf

找到以下行并修改:

listen = /run/php/php版本號-fpm.sock

3. 配置Nginx

Nginx需要配置一個location塊來處理PHP請求,并將它們傳遞給PHP-FPM。

編輯Nginx的默認站點配置文件:

sudo nano /etc/nginx/sites-available/default

添加或修改以下內容:

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/php版本號-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

4. 啟動和啟用服務

啟動并啟用Nginx和PHP-FPM服務:

在Debian/Ubuntu上:

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php版本號-fpm
sudo systemctl enable php版本號-fpm

在CentOS/RHEL上:

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php-fpm
sudo systemctl enable php-fpm

5. 測試配置

確保Nginx和PHP-FPM都在運行,并且可以正確處理PHP請求。

訪問你的域名或服務器IP地址,你應該能夠看到PHP文件被正確解析和執行。

6. 防火墻設置

如果你使用的是防火墻(如ufw),確保開放HTTP(80)和HTTPS(443)端口:

sudo ufw allow 'Nginx Full'

7. 日志檢查

如果遇到問題,檢查Nginx和PHP-FPM的日志文件以獲取更多信息:

  • Nginx錯誤日志:/var/log/nginx/error.log
  • PHP-FPM錯誤日志:/var/log/php版本號-fpm.log

通過以上步驟,你應該能夠在Linux上成功配置PHP-FPM與Nginx協同工作,提供高性能的PHP應用程序服務。

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