Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Nginx + PHP-FPM (FastCGI) 在 CentOS/RHEL 上的配置與除錯實錄

編輯:Linux 教學

記錄在 RHEL4/CentOS 上以 RPM 安裝 Nginx、配置 PHP-FPM (spawn-fcgi)、解決 No input file specified、spawn-fcgi: child exited 等常見問題,並示範虛擬主機配置。

安裝 Nginx (RPM)


rpm -ivh nginx-0.6.39-4.el4.i386.rpm
# RHEL4 無依賴問題可直接安裝

核心配置 (/etc/nginx/nginx.conf)


user www www;
worker_processes 2;  # 依 CPU 核心數調整

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 2048;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] $request '
                    '"$status" $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/logs/access.log main;

    sendfile on;
    keepalive_timeout 65;

    # --- 主虛擬主機 ---
    server {
        listen 8080;
        server_name localhost;

        location / {
            root /var/www/html;
            index index.html index.htm;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html { root html; }

        # --- PHP FastCGI ---
        location ~ \.php$ {
            root /var/www/html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
            include fastcgi_params;
        }
    }

    # --- 虛擬主機範例 (port 8000) ---
    server {
        listen 8000;
        server_name somename alias another.alias;

        location / {
            root /var/www/html/www;
            index index.html index.htm;
        }
    }
}

安裝 PHP 5.3.6 (CLI/CGI) 與 spawn-fcgi

  1. 移除系統預設 PHP 4.x:rpm -qa | grep php 逐個 rpm -e
  2. 下載 Remi 源 PHP 5.3.6 相關 RPM (php-cli, php-common, php-mysql, php-pdo, php-gd 等)
  3. 安裝 spawn-fcgi-1.6.3-1.el4.i386.rpm (屬 lighttpd 專案)
  4. 確認 /usr/bin/php-cgi 存在

啟動 PHP-FPM


spawn-fcgi -a 127.0.0.1 -p 9000 -u www -f /usr/bin/php-cgi
# 輸出: spawn-fcgi: child spawned successfully: PID: 7980

ps aux | grep php-cgi  # 確認進程存在
service nginx restart  # 重載 Nginx

常見問題與解決

問題 1:瀏覽 .php 顯示 No input file specified

原因:SCRIPT_FILENAME 路徑錯誤或 cgi.fix_pathinfo 未開啟。

方案 A (推薦):編輯 /etc/php.ini,設定 cgi.fix_pathinfo=1,重啟 PHP-FPM。

方案 B:在每個 location ~ \.php$ 區塊中精確設定 fastcgi_param SCRIPT_FILENAME 為該虛擬主機的實際根目錄 + $fastcgi_script_name,且必須放在 fastcgi_pass **之後**。

問題 2:spawn-fcgi: child exited with: 126 / 13 / 2

錯誤碼原因解決
126PHP 4 無 php-cgi 執行檔升級至 PHP 5.3+ 並確保 php-cli 套件安裝
13 / 2指令多了 -C 64 參數 (舊版 spawn-fcgi 不支援)移除 -C 參數,僅保留 -a -p -u -f

驗證虛擬主機


netstat -antup | grep nginx
# tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 19013/nginx.conf
# tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 19013/nginx.conf

瀏覽器訪問 https://IP:8000 應顯示 /var/www/html/www/index.htm 內容。

Copyright © Windows教程網 All Rights Reserved