⚠️ 註: 版本極老 (2009 年左右),現代部署請升級至:Nginx 1.24+、PHP 8.2+、MySQL 8.0、Cacti 1.2+、RRDTool 1.8+、AlmaLinux 9/Rocky Linux 9。本文保留歷史實踐邏輯,關鍵步驟標註現代替代方案。
/usr/sbin/ntpdate stdtime.sinica.edu.tw
# 現代:chronyd / systemd-timesyncd
yum -y install yum-fastestmirror
rpm --import /etc/pki/rpm-gpg/RPM* # 解決 NOKEY 警告
# 編譯工具鏈與庫 (現代:dnf groupinstall "Development Tools")
yum -y install gcc gcc-c++ autoconf \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel \
bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel \
e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel \
openssl openssl-devel openldap openldap-devel nss_ldap \
openldap-clients openldap-servers
# 圖形庫與 SNMP (Cacti/RRDTool 依賴)
yum -y install elfutils elfutils-devel beecrypt beecrypt-devel \
net-snmp net-snmp-devel lm_sensors lm_sensors-devel \
libart_lgpl libart_lgpl-devel tcl tcl-devel perl-XML-Parser \
fontconfig fontconfig-devel cairo cairo-devel pango pango-devel \
intltool intltool-devel gd gd-devel net-snmp-utils php-snmp
mkdir -p /usr/local/soft && cd /usr/local/soft
# libiconv
tar zxvf libiconv-1.13.tar.gz && cd libiconv-1.13
./configure --prefix=/usr/local && make && make install
# libmcrypt
tar zxvf libmcrypt-2.5.8.tar.gz && cd libmcrypt-2.5.8
./configure && make && make install
cd libltdl && ./configure --enable-ltdl-install && make && make install
# mhash
tar zxvf mhash-0.9.9.9.tar.gz && cd mhash-0.9.9.9
./configure && make && make install
# mcrypt
tar zxvf mcrypt-2.6.8.tar.gz && cd mcrypt-2.6.8
/sbin/ldconfig && ./configure && make && make install
# 符號鏈接 (解決庫查找路徑)
ln -sf /usr/local/lib/libmcrypt.* /usr/lib/
ln -sf /usr/local/lib/libmhash.* /usr/lib/
groupadd mysql && useradd -r -g mysql -s /bin/bash -d /var/lib/mysql mysql
tar zxvf mysql-5.1.38.tar.gz && cd mysql-5.1.38
./configure \
--prefix=/usr/local/web/mysql \
--sysconfdir=/etc \
--localstatedir=/var/lib/mysql \
--with-charset=gbk \
--with-extra-charsets=complex \
--with-low-memory \
--enable-assembler \
--enable-thread-safe-client \
--with-big-tables \
--with-readline \
--with-ssl \
--with-embedded-server \
--enable-local-infile \
--with-plugins=innobase \
&& make && make install
/usr/local/web/mysql/bin/mysql_install_db --user=mysql
chown -R mysql:mysql /var/lib/mysql /usr/local/web/mysql
chmod 755 /var/lib/mysql
cp support-files/my-medium.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld && chkconfig mysqld on
ln -sf /usr/local/web/mysql/bin/* /usr/bin/
ln -sf /usr/local/web/mysql/lib/mysql /usr/lib/mysql
ln -sf /usr/local/web/mysql/include/mysql /usr/include/mysql
service mysqld start
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'StrongPass#2024' WITH GRANT OPTION; FLUSH PRIVILEGES;"
tar zxvf php-5.2.10.tar.gz
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
cd php-5.2.10
./configure \
--prefix=/usr/local/web/php \
--with-config-file-path=/usr/local/web/php/etc \
--with-mysql=/usr/local/web/mysql \
--with-mysqli=/usr/local/web/mysql/bin/mysql_config \
--with-iconv-dir=/usr/local \
--with-freetype-dir --with-jpeg-dir --with-png-dir \
--with-zlib --with-libxml-dir=/usr \
--enable-xml --disable-rpath \
--enable-bcmath --enable-shmop --enable-sysvsem \
--enable-inline-optimization --with-curl --with-curlwrappers \
--enable-mbregex --enable-fpm --enable-mbstring \
--with-mcrypt --with-gd --enable-gd-native-ttf \
--with-openssl --with-mhash --enable-pcntl --enable-sockets \
--with-ldap --with-ldap-sasl --with-xmlrpc \
--enable-zip --enable-soap --enable-ftp \
--without-pear \
&& make ZEND_EXTRA_LIBS='-liconv' && make install
cp php.ini-dist /usr/local/web/php/etc/php.ini
# 擴展安裝 (memcache, eaccelerator, pdo_mysql, imagick)
# ... 略,現代用 pecl install 或 dnf install php-pecl-*
extension_dir = "/usr/local/web/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension = memcache.so
extension = pdo_mysql.so
extension = imagick.so
output_buffering = On
date.timezone = Asia/Shanghai
mkdir -p /usr/local/web/eaccelerator_cache && chmod 755 $_
cat >> /usr/local/web/php/etc/php.ini <<'EOF'
[eaccelerator]
zend_extension="/usr/local/web/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/web/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
EOF
# 修改 php-fpm.conf: user=nginx, group=nginx, display_errors=0
ulimit -SHn 65535
/usr/local/web/php/sbin/php-fpm start
tar zxvf pcre-7.9.tar.gz && cd pcre-7.9 && ./configure && make && make install
tar zxvf nginx-0.8.15.tar.gz && cd nginx-0.8.15
# 可選 AccessKey 模塊
bzcat ../nginx-Accesskey-2.0.3.diff.bz2 | patch -p1
./configure \
--user=nginx --group=nginx \
--prefix=/usr/local/web/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-md5=/usr/lib \
&& make && make install
mkdir -p /usr/local/web/html && chown -R nginx:nginx $_ && chmod -R 755 $_
user nginx;
worker_processes 8; # 現代:auto
error_log logs/error.log crit;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
access_log off;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name localhost;
root /usr/local/web/html;
index index.html index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht { deny all; }
}
}
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REDIRECT_STATUS 200; # PHP --enable-force-cgi-redirect 必需
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535
# 生效
/sbin/sysctl -p
com2sec notConfigUser 127.0.0.1 public
access notConfigGroup "" any noauth exact all none none
view all included .1 80
service snmpd restart
tar zxvf rrdtool-1.3.9.tar.gz && cd rrdtool-1.3.9
./configure --prefix=/usr/local/web/rrdtool --disable-tcl --disable-python
make && make install
ln -sf /usr/local/web/rrdtool/bin/rrdtool /usr/bin/rrdtool
ln -sf /usr/local/web/rrdtool/bin/rrdupdate /usr/bin/rrdupdate
ln -sf /usr/local/web/rrdtool/bin/rrdcgi /usr/bin/rrdcgi
tar zxvf cacti-0.8.7d-cn-utf8.gz
mv cacti-0.8.7d-cn-utf8 /usr/local/web/html/cacti
mysql -uroot -p -e "CREATE DATABASE cacti DEFAULT CHARACTER SET utf8;"
mysql -uroot -p cacti < /usr/local/web/html/cacti/cacti.sql
chown -R nginx:nginx /usr/local/web/html/cacti
chmod -R 777 /usr/local/web/html/cacti/rra /usr/local/web/html/cacti/log
# 編輯 config.php 配置 DB 連接
vim /usr/local/web/html/cacti/include/config.php
# 定時輪詢
crontab -u root -e
*/5 * * * * /usr/local/web/php/bin/php /usr/local/web/html/cacti/poller.php > /dev/null 2>&1
# nginx.conf 中 server 區塊添加
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
nginx -s reload
tar zxvf cacti-nginx.tar.gz
cp cacti-nginx/get_nginx_*_status.pl /usr/local/web/html/cacti/scripts/
chmod 755 /usr/local/web/html/cacti/scripts/get_nginx_*
# Cacti Web 界面:Console → Import Template
# 導入 cacti_graph_template_nginx_clients_stat.xml
# 導入 cacti_graph_template_nginx_sockets_stat.xml
# 測試腳本
/usr/local/web/html/cacti/scripts/get_nginx_clients_status.pl https://127.0.0.1/nginx_status
# httpd.conf
LoadModule status_module modules/mod_status.so
ExtendedStatus On
SetHandler server-status
Order Deny,Allow
Allow from 127.0.0.1
| 組件 | 舊版本 | 現代推薦 | 部署方式 |
|---|---|---|---|
| OS | CentOS 5.2 | AlmaLinux 9 / Rocky Linux 9 / Ubuntu 22.04 LTS | ISO 安裝/雲鎮像 |
| Nginx | 0.8.15 源碼 | 1.24+ (官方 RPM/APT/模塊化) | dnf install nginx / apt install nginx |
| PHP | 5.2.10 + FPM patch | 8.2+ (FPM 內建、JIT、OPcache) | dnf module install php:8.2 / ondrej PPA |
| MySQL | 5.1.38 源碼 | 8.0 / 8.4 LTS (官方 RPM/APT) | dnf install mysql-server / MySQL APT Repo |
| Cacti | 0.8.7d | 1.2.26+ (PHP 8 支持、現代 UI、API) | GitHub Release / Docker |
| RRDTool | 1.3.9 | 1.8.0+ | 系統包管理器 |
| 監控 | Cacti 單一 | Prometheus + Grafana + Alertmanager (主流) | Kubernetes Operator / 二進制 / Docker |
| 部署 | 手動編譯 | Ansible Playbook / Terraform / Helm Chart | 基礎設施即代碼 |
本文記錄了 2009 年經典 LNMP + Cacti 全棧手工編譯部署全過程,涵蓋系統調優、依賴解決、核心組件編譯參數、配置文件最佳實踐、監控整合。雖版本陳舊,但編譯邏輯、目錄規範、權限模型、性能調優參數、故障排查思路 具有跨時代的參考價值。現代生產環境請務必採用長期支持版本、包管理器部署、容器化編排、聲明式配置、GitOps 流程,將運維從「手工藝」升級為「工程學」。