首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Nginx + PHP-FPM 環境配置與啟動腳本實戰筆記

     編輯:PHP 相關知識

Nginx 核心配置 nginx.conf

server {
    listen 8080;
    server_name localhost;

    location / {
        root   /home/work/htdocs;
        expires 1d;
        autoindex on;
        index  index.php index.html;
    }

    location ~* \.php$ {
        root   /home/work/htdocs;
        fastcgi_pass  127.0.0.1:30000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/work/htdocs/$fastcgi_script_name;
        client_max_body_size 100m;
        include fastcgi_params;
        fastcgi_connect_timeout 1000s;
        fastcgi_send_timeout 1000s;
        fastcgi_read_timeout 1000s;
    }
}

Nginx 管理腳本 nginx.sh

#!/bin/sh
case "$1" in
    'start')
        sudo /usr/local/nginx/sbin/nginx -s start
        ;;
    'stop')
        sudo /usr/local/nginx/sbin/nginx -s stop
        ;;
    'restart')
        sudo /usr/local/nginx/sbin/nginx -s reload
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

PHP-FPM 啟動腳本 spawn-php.sh

#!/bin/sh
dir=`dirname $0`
PID_FILE=/home/work/htdocs/spawn-php.pid
PHP_FCGI="/home/work/php/bin/php-cgi -f /home/work/htdocs/php.ini"

case "$1" in
    'start')
        spawn-fcgi -C 3 -p 30000 -f "$PHP_FCGI" -P $PID_FILE
        ;;
    'stop')
        kill `cat $PID_FILE`
        ;;
    'restart')
        kill `cat $PID_FILE`
        spawn-fcgi -C 3 -p 30000 -f "$PHP_FCGI" -P $PID_FILE
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

關鍵配置說明

  • fastcgi_pass:指向 PHP-FPM 監聽埠(範例為 30000)。
  • SCRIPT_FILENAME:必須為絕對路徑,Nginx 傳遞給 PHP-FPM 的關鍵參數。
  • spawn-fcgi -C 3:啟動 3 個 PHP-CGI 子進程處理並發請求。
  • PID 檔案:用於停止/重啟時精準殺掉進程。

現代化建議

現代部署建議改用 PHP-FPM 原生管理(systemctl start php-fpm)取代 spawn-fcgi,並搭配 nginx -t 測試配置後再重載。


摘自 ideawu

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved