想徹底掌握環境,自行編譯 LAMP 加上 Cacti 監控是最好的練習。以下依序說明依賴套件、四支程式的編譯,以及 Cacti 的部署與排程設定。
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
tar zxvf mysql-5.0.92.tar.gz
cd mysql-5.0.92
./configure --prefix=/usr/local/mysql --sysconfdir=/etc --with-unix-socket-path=/usr/local/mysql/var/mysql.sock --without-debug --with-mysqld-user=mysql --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static
make && make install
useradd mysql
/usr/local/mysql/bin/mysql_install_db
cp support-files/my-medium.cnf /etc/my.cnf
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod 755 /etc/rc.d/init.d/mysqld
chown -R mysql:mysql /usr/local/mysql/var
/etc/rc.d/init.d/mysqld start
tar zxvf httpd-2.2.17.tar.gz
cd httpd-2.2.17
./configure --prefix=/usr/local/http --enable-so --enable-mods-shared=all --enable-vhost-alias --enable-deflate --enable-expires --enable-rewrite --enable-authn-dbm=shared --enable-ssl
make && make install
cp /usr/local/http/bin/apachectl /etc/rc.d/init.d/httpd
chmod 755 /etc/rc.d/init.d/httpd
/etc/rc.d/init.d/httpd start
此時在瀏覽器輸入主機 IP,看到 It works! 即代表 Apache 正常。
tar zxvf php-5.2.17.tar.gz
cd php-5.2.17
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/http/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mcrypt --enable-zip --with-bz2 --with-jpeg-dir --with-png-dir --with-freetype-dir --without-iconv --with-libxml-dir --with-zlib-dir --with-xmlrpc --with-gd --with-curl --enable-mbstring --with-xsl --enable-sockets --disable-rpath --disable-debug
make && make install
cp php.ini-production /usr/local/php/etc/php.ini
接著編輯 /usr/local/http/conf/httpd.conf,在 AddType application/x-gzip .gz .tgz 那行下方加入:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
並在 DirectoryIndex 的 index.html 後補上 index.php。
建立測試頁 /usr/local/http/htdocs/index.php:
<?php phpinfo(); ?>
重啟 service httpd restart,瀏覽 https://ip/index.php 看到資訊頁即完成。
tar -xzvf cacti-0.8.7g.tar.gz
mv cacti-0.8.7g /usr/local/http/htdocs/cacti
cd /usr/local/http/htdocs/cacti
chmod 777 rra log poller.php
編輯 include/config.php 的資料庫設定:
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "127.0.0.1";
$database_username = "cacti";
$database_password = "cacti";
$database_port = "3306";
建立資料庫與權限並匯入初始資料:
mysql> create database cacti;
mysql> grant all privileges on cacti.* to cacti@localhost identified by 'cacti' with grant option;
mysql> grant all privileges on cacti.* to cacti@127.0.0.1 identified by 'cacti' with grant option;
mysql> flush privileges;
mysql> use cacti;
mysql> source /usr/local/http/htdocs/cacti.sql;
開啟 https://ip/cacti 進入安裝指南,點擊 Next,選擇 New Install;接著確認 rrdtool、php、snmp 的路徑全部顯示 FOUND(PHP 路徑要改為 /usr/local/php/bin/php),按 Finish 完成。預設帳密為 admin/admin,第一次登入會被強制要求改密碼。完成後到 graphs 頁即可看到本機監控圖表。
Cacti 必須靠 poller 定時蒐集數據,執行 crontab -e 或編輯 /etc/crontab 加入:
*/5 * * * * root /usr/local/php/bin/php /usr/local/http/htdocs/cacti/poller.php > /dev/null 2>&1
最後安裝 rrdtool 相關套件(三個包需同時安裝,否則相依性會報錯):
yum install perl-rrdtool rrdtool rrdtool-devel
接著再依需求完成 snmp 的安裝與設定,整套監控即告就緒。