| 掛載點 | 大小 | 用途 |
|---|---|---|
| /boot | 100M | 啟動分區 |
| SWAP | 4G | 物理記憶體 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
UseDNS no、禁用 root 登入、修改預設埠。ntpdate 210.72.145.44(中國國家授時中心)。./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"
./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 從網路下載優化版,關閉不必要選項
./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
/usr/local/php/bin/phpize → ./configure --with-php-config=... → make && make installuser 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;
}
}
}
}
#!/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
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"
/usr/local/php/sbin/spawn-fcgi start/usr/local/nginx/sbin/nginx -t → nginxecho "" > /data/www/wwwroot/linuxtone/phpinfo.phphttps://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、動態模組、WAF | Nginx 1.24+ (Mainline) + ngx_brotli + ModSecurity |
| 手動編譯升級困難、無套件管理 | Ansible/Ansible Galaxy 自動化部署、Docker/K8s 容器化 |
本文記錄了 2010 年代經典 LEMP 手工編譯部署全流程:系統初始化、目錄規範、Nginx/PHP/MySQL/spawn-fcgi 編譯參數、雙 FastCGI 負載均衡、eAccelerator/Zend Optimizer 加速、tmpfs 加速暫存、開機自啟。雖具歷史參考價值,生產環境請務必使用現代化技術棧與自動化運維工具。