首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

CentOS 5.4 搭建 Nginx + PHP + spawn-fcgi + MySQL 高效能 LEMP 平台全紀錄

     編輯:MySQL 資料庫相關知識

架構總覽

  • OS: CentOS 5.4 x86_64
  • Web: Nginx 0.8.21 (epoll, 高併發)
  • PHP: 5.2.9 (FastCGI 模式,spawn-fcgi 管理)
  • DB: MySQL 5.1.34
  • 進程管理: spawn-fcgi 1.6.3 監聽 9000/9001 双端口,Nginx upstream 負載均衡

磁碟分區規劃

掛載點大小用途
/boot100M啟動分區
SWAP4G物理記憶體 2 倍(上限 4G)
/50G系統與軟體
/data剩餘MySQL 資料、網站根目錄、日誌

目錄規範

/usr/local/src              # 源碼包存放
/usr/local/軟體名           # 編譯安裝前綴
/usr/local/sbin             # 維護腳本
/data/mysql/data            # MySQL 資料目錄
/data/www/wwwroot           # 網站根目錄
/data/logs                  # 日誌根目錄
/data/cache/ea              # eAccelerator 快取

執行用戶:www:www

系統初始化腳本亮點

  • 關閉 IPv6、SELinux。
  • SSH 加固:UseDNS no、禁用 root 登入、修改預設埠。
  • 關閉非必要服務(僅保留 crond, irqbalance, network, random, sendmail, sshd, syslog, mysqld)。
  • NTP 對時:ntpdate 210.72.145.44(中國國家授時中心)。

關鍵編譯參數

Nginx

./configure \
  --user=www --group=www \
  --prefix=/usr/local/nginx \
  --with-http_stub_status_module \
  --with-http_ssl_module \
  --with-md5=/usr/lib --with-sha1=/usr/lib
# 偽裝 Server header(選配)
# 修改 src/core/nginx.h: NGINX_VERSION "1.0", NGINX_VER "LTWS/"
# 修改 src/http/ngx_http_header_filter_module.c: "Server: LTWS"

MySQL

./configure \
  --prefix=/usr/local/mysql \
  --localstatedir=/data/mysql/data \
  --with-charset=utf8 --with-collation=utf8_general_ci \
  --with-extra-charsets=complex \
  --enable-thread-safe-client \
  --with-big-tables \
  --with-readline --with-ssl \
  --with-embedded-server \
  --enable-local-infile \
  --with-plugins=innobase
# my.cnf 從網路下載優化版,關閉不必要選項

PHP

./configure \
  --prefix=/usr/local/php \
  --enable-fastcgi --enable-fpm \
  --with-config-file-path=/usr/local/php/etc \
  --with-mysql=/usr/local/mysql \
  --with-mysqli=/usr/local/mysql/bin/mysql_config \
  --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir \
  --with-gd --with-zlib --with-libxml-dir --with-curl --with-curlwrappers \
  --with-openssl --with-mhash --with-xmlrpc --with-mcrypt \
  --with-ldap --with-ldap-sasl \
  --enable-xml --disable-rpath --enable-bcmath --enable-shmop \
  --enable-sysvsem --enable-inline-optimization --enable-mbregex \
  --enable-mbstring --enable-gd-native-ttf --enable-ftp --enable-pcntl \
  --enable-sockets --enable-zip --disable-debug --disable-ipv6
make ZEND_EXTRA_LIBS='-liconv' && make install

PHP 擴展(編譯安裝)

  • memcache, eaccelerator, PDO_MySQL, ImageMagick, imagick
  • 統一用 /usr/local/php/bin/phpize → ./configure --with-php-config=... → make && make install

Nginx 核心配置 (nginx.conf)

user www www;
worker_processes 10;                 # CPU 核心數
worker_rlimit_nofile 51200;
events {
    use epoll;
    worker_connections 51200;
}
http {
    include mime.types;
    default_type application/octet-stream;
    charset gbk;                     # 依專案編碼調整
    access_log off;                  # 高併發關閉 access_log
    sendfile on;
    tcp_nopush on; tcp_nodelay on;
    keepalive_timeout 60;

    # FastCGI 參數
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 128k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    fastcgi_intercept_errors on;

    # Gzip
    gzip on; gzip_min_length 0; gzip_buffers 16 8k; gzip_comp_level 9;
    gzip_types text/plain text/css image/x-icon image/png;

    # 暫存目錄掛載 tmpfs
    fastcgi_temp_path /dev/shm/fastcgi_temp;
    client_body_temp_path /dev/shm/client_body_temp;
    proxy_temp_path /dev/shm/proxy_temp;

    # Upstream 雙 FastCGI 端口
    upstream spawn {
        server 127.0.0.1:9000 max_fails=0 fail_timeout=30s;
        server 127.0.0.1:9001 max_fails=0 fail_timeout=30s;
    }

    # 虛擬主機
    server {
        listen 80;
        server_name www.linuxtone.org linuxtone.org;
        root /data/www/wwwroot/linuxtone;
        index index.html index.htm index.php;

        location ~ \.php$ {
            fastcgi_pass spawn;
            include spawn_php5.conf;
            fastcgi_index index.php;
        }

        location ~* \.(js|css|jpg|jpeg|gif|png)$ {
            if (-f $request_filename) {
                access_log off;
                expires 1d;
                break;
            }
        }
    }
}

spawn-fcgi 啟動腳本 (雙端口)

#!/bin/sh
# /usr/local/php/sbin/spawn-fcgi
DAEMON=/usr/local/bin/spawn-fcgi
d_start() {
    $DAEMON -a 127.0.0.1 -p 9000 -C 128 -u www -g www -f /usr/local/php/bin/php-cgi >/dev/null 2>&1
    $DAEMON -a 127.0.0.1 -p 9001 -C 128 -u www -g www -f /usr/local/php/bin/php-cgi >/dev/null 2>&1
}
d_stop() {
    killall -9 php-cgi >/dev/null 2>&1
}
case "$1" in
    start) d_start ;; stop) d_stop ;; restart) d_stop; sleep 1; d_start ;;
    *) echo "Usage: $0 {start|stop|restart}"; exit 1 ;;
esac

PHP.ini 關鍵調優

open_basedir = /data/www/wwwroot:/tmp
disable_functions = phpinfo,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server
expose_php = Off
display_errors = Off
output_buffering = On
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"

# eAccelerator
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/data/cache/ea"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

啟動驗證

  1. /usr/local/php/sbin/spawn-fcgi start
  2. /usr/local/nginx/sbin/nginx -t → nginx
  3. echo "" > /data/www/wwwroot/linuxtone/phpinfo.php
  4. 瀏覽器訪問 https://www.linuxtone.org/phpinfo.php 確認 PHP、MySQL、擴展載入正常。

開機自啟

# /etc/rc.local
ulimit -SHn 51200
/usr/local/nginx/sbin/nginx
/usr/local/php/sbin/spawn-fcgi start

遺留問題與現代化建議

舊架構痛點現代替代方案
CentOS 5 EOL、PHP 5.2 EOL、安全漏洞無修補升級 Rocky Linux 9 / Ubuntu 22.04 + PHP 8.2+ + MySQL 8.0
spawn-fcgi 單點、無優雅重啟、進程管理弱PHP-FPM (內建進程管理、平滑重載、狀態頁、慢日誌)
eAccelerator 早已停止維護OPcache (PHP 5.5+ 內建) + APCu / Redis 快取
MySQL 5.1 無 GTID、並行複制弱MySQL 8.0 / MariaDB 10.6+ / Percona Server
Nginx 0.8 無 HTTP/2、動態模組、WAFNginx 1.24+ (Mainline) + ngx_brotli + ModSecurity
手動編譯升級困難、無套件管理Ansible/Ansible Galaxy 自動化部署、Docker/K8s 容器化

總結

本文記錄了 2010 年代經典 LEMP 手工編譯部署全流程:系統初始化、目錄規範、Nginx/PHP/MySQL/spawn-fcgi 編譯參數、雙 FastCGI 負載均衡、eAccelerator/Zend Optimizer 加速、tmpfs 加速暫存、開機自啟。雖具歷史參考價值,生產環境請務必使用現代化技術棧與自動化運維工具。

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