本文記錄一台全新 Linux VPS 從拿到手到構建高效能 LNMP (Linux + Nginx + MySQL + PHP) 生產環境的完整實戰過程,涵蓋系統加固、磁碟掛載、依賴解決、編譯參數調優、服務管理與常見錯誤排查,適合運維工程師參考建立標準化部署 SOP。
# 1. 修改 root 密碼
passwd root
# 2. SSH 保活防斷開
sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 60/' /etc/ssh/sshd_config
sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 30/' /etc/ssh/sshd_config
systemctl reload sshd
# 3. 安裝系統管理工具
yum install -y setuptool ntsysv iptables-services system-config-firewall-tui system-config-network-tui
# 4. 關閉 SELinux (生產建議配置而非關閉)
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
假設新掛載數據盤為 /dev/vdb (或 /dev/xvdb),掛載至 /data:
# 1. 建立分區
fdisk /dev/vdb # 依次輸入 n, p, 1, 回車, 回車, w
# 2. 格式化為 ext4 (推薦)
mkfs.ext4 /dev/vdb1
# 3. 掛載與開機自掛載
mkdir /data
echo '/dev/vdb1 /data ext4 defaults,noatime 0 0' >> /etc/fstab
mount -a
df -h /data # 驗證
yum install -y gcc gcc-c++ autoconf automake libtool make cmake \
libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel \
freetype-devel libmcrypt-devel mhash-devel libxslt-devel \
bzip2-devel ncurses-devel readline-devel zlib-devel \
pcre-devel openldap-devel postgresql-devel
關鍵點:pcre-devel 解決 Nginx rewrite 模組依賴;libmcrypt-devel/mhash-devel 解決 PHP mcrypt/哈希擴展依賴。
# 1. 下載源碼 (含 OpenSSL 1.1.1+ 支援 TLS 1.3)
wget https://nginx.org/download/nginx-1.24.0.tar.gz
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar xzf nginx-1.24.0.tar.gz && tar xzf openssl-1.1.1w.tar.gz
# 2. 編譯安裝
cd nginx-1.24.0
./configure \
--prefix=/usr/local/nginx \
--user=www --group=www \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre \
--with-openssl=../openssl-1.1.1w \
--with-ld-opt="-ljemalloc" \
--add-module=../ngx_devel_kit-0.3.2 \
--add-module=../lua-nginx-module-0.10.22
make -j$(nproc) && make install
# 3. 建立用戶與日誌目錄
useradd -r -s /sbin/nologin www
mkdir -p /var/log/nginx && chown www:www /var/log/nginx
cd /usr/local/src
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-8.0.36.tar.gz
tar xzf mysql-boost-8.0.36.tar.gz
cd mysql-8.0.36
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DWITH_INNODB_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DMYSQL_TCP_PORT=3306 \
-DWITH_BOOST=./boost \
-DENABLE_DOWNLOADS=1
make -j$(nproc) && make install
# 初始化
/user/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
chown -R mysql:mysql /data/mysql /usr/local/mysql
cd /usr/local/src
wget https://www.php.net/distributions/php-8.2.18.tar.gz
tar xzf php-8.2.18.tar.gz
cd php-8.2.18
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/php.d \
--enable-fpm \
--with-fpm-user=www --with-fpm-group=www \
--with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-iconv-dir --with-freetype --with-jpeg --with-zlib \
--with-libxml --with-curl --with-openssl --with-mhash \
--with-xsl --with-zip --with-sodium --with-pear \
--enable-mbstring --enable-bcmath --enable-sockets \
--enable-intl --enable-gd --enable-opcache \
--disable-short-tags
make -j$(nproc) && 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
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 10000;
reset_timedout_connection on;
# Gzip
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# FastCGI 緩存
fastcgi_cache_path /data/nginx_cache levels=1:2 keys_zone=PHP_CACHE:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_valid 200 301 302 1h;
fastcgi_cache_valid 404 1m;
# 上游 PHP-FPM
upstream php_backend {
server 127.0.0.1:9000;
keepalive 64;
}
}
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 1000
request_terminate_timeout = 60
rlimit_files = 65535
slowlog = /var/log/php-fpm/$pool-slow.log
request_slowlog_timeout = 10
[mysqld]
innodb_buffer_pool_size = 4G # 物理內存 60-70%
innodb_log_file_size = 512M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
max_connections = 2000
open_files_limit = 65535
table_open_cache = 4000
query_cache_type = 0
query_cache_size = 0
# Systemd 服務文件示例 (nginx.service)
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
# 啟用並啟動
systemctl daemon-reload
systemctl enable --now nginx php-fpm mysqld
| 錯誤信息 | 原因 | 解決命令 |
|---|---|---|
| configure: error: no acceptable C compiler | 缺 gcc | yum install gcc gcc-c++ |
| libpcre.so.1 not found | 動態庫路徑未更新 | ln -s /usr/local/lib/libpcre.so.1 /lib64/ && ldconfig |
| SSL modules require OpenSSL library | 缺 openssl-devel | yum install openssl-devel |
| gzip module requires zlib library | 缺 zlib-devel | yum install zlib-devel |
| Cannot find MySQL header files | 缺 mysql-devel | yum install mysql-devel |
| phpize: Cannot find autoconf | 缺 autoconf | yum install autoconf |
遵循本文流程與參數,可構建出穩定、高效、易維護的 LNMP 生產環境基線。後續建議接入 Ansible/Playbook 實現基礎設施即代碼 (IaC) 管理。