手動編譯 LNMP 組件耗時且易出錯。本文提供一個完整的 Bash 自動化安裝腳本,涵蓋依賴環境準備、MySQL、PHP、Nginx、Memcached、eAccelerator、ImageMagick 等組件的編譯安裝與配置,支援選單式操作與狀態管理。
#!/bin/bash
yum -y install gcc gcc-c++ autoconf libjpeg-devel libpng-devel \
freetype-devel libxml2-devel zlib-devel glibc-devel \
openssl-devel curl-devel pcre-devel libevent-devel
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/var/mysql/data -DMYSQL_USER=mysql
make && make install
# 套用 FPM 補丁
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-iconv-dir=/usr/local --enable-fpm --with-fpm-user=www --with-fpm-group=www \
--enable-mbstring --with-gd --with-openssl --with-mhash --enable-zip
make ZEND_EXTRA_LIBS='-liconv' && make install
cd pcre-8.31 && ./configure && make && make install
cd ../nginx-1.2.1
./configure --user=www --group=www --prefix=/usr/local/nginx \
--with-http_stub_status_module --with-http_ssl_module
make && make install
| 功能 | 實現方式 |
|---|---|
| 服務管理 | chkconfig 註冊 systemd/init.d 服務,支援開機自啟 |
| 配置模板化 | sed 批量修改 php-fpm.conf、my.cnf、nginx.conf 關鍵參數 |
| 安全加固 | 建立 www 低權限用戶、目錄權限隔離、ulimit 限制 |
| 擴展模組 | PDO_MYSQL、imagick、eAccelerator 自動編譯安裝 |
完整腳本內容較長,建議保存為 install_lnmp.sh 並賦予執行權限後使用。