LNMP 代表 Linux + Nginx + MySQL + PHP,是現代高併發網站主流架構。四者均為免費開源軟體,組合後形成高效、擴展性強的 Web 服務系統。
關聯式資料庫,支援事務、行級鎖定、多種儲存引擎。生產環境建議使用 Percona Server 或 MariaDB 以獲得更好效能。
PHP 的 FastCGI 實現,提供進程管理、平滑重啟、慢日誌記錄、動態/靜態進程池模式,解決傳統 mod_php 效能與穩定性瓶頸。
| 維度 | Apache (prefork) | Nginx + PHP-FPM |
|---|---|---|
| 並發連線處理 | 進程/執行緒模型,記憶體佔用大,易達 MaxClients 瓶頸 | 事件驅動單進程,C10K 問題輕鬆應對,記憶體佔用恆定 |
| 靜態檔案服務 | 需經完整請求流程 | 直接由 Nginx 高效服務,可啟用 sendfile、aio、gzip_static |
| 負載均衡 | 需 mod_proxy_balancer,配置複雜 | 內建 upstream 模組,支援權重、ip_hash、least_conn、健康檢查 |
| 配置複雜度 | 模組繁多,配置檔冗長 | 配置語法簡潔,支援 include、變數、map、正則 |
| 平滑升級 | 需重啟服務 | 支援熱重載配置、二進制平滑升級,零停機 |
yum groupinstall "Development Tools" -y
yum install pcre-devel openssl-devel libxml2-devel curl-devel libjpeg-devel libpng-devel freetype-devel -y
tar zxvf pcre-8.xx.tar.gz -C /usr/local/src
cd /usr/local/src/pcre-8.xx
./configure --prefix=/usr/local/pcre
make && make install
tar zxvf nginx-1.xx.x.tar.gz -C /usr/local/src
cd /usr/local/src/nginx-1.xx.x
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre=/usr/local/src/pcre-8.xx \
--user=www --group=www
make && make install
tar zxvf php-7.x.x.tar.gz -C /usr/local/src
cd /usr/local/src/php-7.x.x
./configure \
--prefix=/usr/local/php \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-config-file-path=/usr/local/php/etc \
--enable-mbstring \
--enable-opcache \
--with-openssl \
--with-curl \
--with-zlib \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir
make && make install
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; # 或 unix:/tmp/php-fpm.sock
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Nginx
cat > /etc/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# PHP-FPM
cat > /etc/systemd/system/php-fpm.service <<EOF
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable nginx php-fpm mysqld
systemctl start nginx php-fpm mysqld
worker_processes auto、worker_cpu_affinity auto、worker_rlimit_nofile 65535、events { use epoll; worker_connections 65535; }、啟用 gzip、open_file_cache。pm = dynamic、pm.max_children 依記憶體計算、pm.start_servers、pm.min_spare_servers、pm.max_spare_servers、開啟 slowlog、request_slowlog_timeout。innodb_buffer_pool_size = 物理記憶體 60-70%、innodb_log_file_size = 256M、innodb_flush_log_at_trx_commit = 2、max_connections = 2000、關閉查詢快取(query_cache_type = 0)。/etc/sysctl.conf TCP 參數、/etc/security/limits.conf 檔案描述符限制、掛載選項加 noatime,nodiratime。fastcgi_pass 埠/通訊檔是否正確、PHP-FPM 錯誤日誌。client_max_body_size (Nginx)、upload_max_filesize/post_max_size (PHP)、fastcgi_read_timeout。root 指令正確、location 匹配順序、權限問題。expose_php = Off (php.ini)、server_tokens off (nginx.conf)。