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

LNMP 架構深度解析:Nginx + MySQL + PHP 高效能網站服務器

     編輯:PHP 綜合知識

什麼是 LNMP?

LNMP 代表 Linux + Nginx + MySQL + PHP,是現代高併發網站主流架構。四者均為免費開源軟體,組合後形成高效、擴展性強的 Web 服務系統。

核心組件介紹

Nginx(Engine X)

  • 由 Igor Sysoev 為俄羅斯 Rambler 網站開發,專為高併發設計。
  • 事件驅動、非阻塞 I/O 模型,單進程可處理數萬並發連線。
  • 靜態資源處理速度極快,記憶體佔用極低(約 2-3 MB / 10,000 非活躍連線)。
  • 支援 HTTP/HTTPS 反向代理、負載均衡、FastCGI、uWSGI、SCGI、Memcached 代理。
  • 亦可作為 IMAP/POP3/SMTP 郵件代理伺服器。

MySQL / MariaDB

關聯式資料庫,支援事務、行級鎖定、多種儲存引擎。生產環境建議使用 Percona Server 或 MariaDB 以獲得更好效能。

PHP-FPM (FastCGI Process Manager)

PHP 的 FastCGI 實現,提供進程管理、平滑重啟、慢日誌記錄、動態/靜態進程池模式,解決傳統 mod_php 效能與穩定性瓶頸。

LNMP 優於傳統 LAMP 的關鍵優勢

維度Apache (prefork)Nginx + PHP-FPM
並發連線處理進程/執行緒模型,記憶體佔用大,易達 MaxClients 瓶頸事件驅動單進程,C10K 問題輕鬆應對,記憶體佔用恆定
靜態檔案服務需經完整請求流程直接由 Nginx 高效服務,可啟用 sendfile、aio、gzip_static
負載均衡需 mod_proxy_balancer,配置複雜內建 upstream 模組,支援權重、ip_hash、least_conn、健康檢查
配置複雜度模組繁多,配置檔冗長配置語法簡潔,支援 include、變數、map、正則
平滑升級需重啟服務支援熱重載配置、二進制平滑升級,零停機

LNMP 快速部署流程(源碼編譯示例)

1. 系統準備

yum groupinstall "Development Tools" -y
yum install pcre-devel openssl-devel libxml2-devel curl-devel libjpeg-devel libpng-devel freetype-devel -y

2. 安裝 PCRE(Nginx rewrite 必須)

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

3. 安裝 Nginx

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

4. 安裝 MySQL(同上文源碼編譯步驟)

5. 安裝 PHP + PHP-FPM

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

6. 配置 Nginx 支援 PHP

# /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;
}

7. 建立 systemd 服務並啟動

# 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

效能調優關鍵點

  • Nginx:worker_processes auto、worker_cpu_affinity auto、worker_rlimit_nofile 65535、events { use epoll; worker_connections 65535; }、啟用 gzip、open_file_cache。
  • PHP-FPM:pm = dynamic、pm.max_children 依記憶體計算、pm.start_servers、pm.min_spare_servers、pm.max_spare_servers、開啟 slowlog、request_slowlog_timeout。
  • MySQL: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。

常見問題排查

  1. 502 Bad Gateway:檢查 PHP-FPM 是否運行、fastcgi_pass 埠/通訊檔是否正確、PHP-FPM 錯誤日誌。
  2. 上傳大檔案失敗:調整 client_max_body_size (Nginx)、upload_max_filesize/post_max_size (PHP)、fastcgi_read_timeout。
  3. 靜態資源 404:確認 root 指令正確、location 匹配順序、權限問題。
  4. PHP 暴露版本資訊:expose_php = Off (php.ini)、server_tokens off (nginx.conf)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved