記錄在 RHEL4/CentOS 上以 RPM 安裝 Nginx、配置 PHP-FPM (spawn-fcgi)、解決 No input file specified、spawn-fcgi: child exited 等常見問題,並示範虛擬主機配置。
rpm -ivh nginx-0.6.39-4.el4.i386.rpm
# RHEL4 無依賴問題可直接安裝
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;
}
}
}
rpm -qa | grep php 逐個 rpm -espawn-fcgi-1.6.3-1.el4.i386.rpm (屬 lighttpd 專案)/usr/bin/php-cgi 存在
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
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 **之後**。
spawn-fcgi: child exited with: 126 / 13 / 2| 錯誤碼 | 原因 | 解決 |
|---|---|---|
| 126 | PHP 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 內容。