在CentOS上監控Apache性能可以通過多種方法和工具來實現,以下是一些常用的監控方法:
/etc/httpd/conf/httpd.conf
或/etc/apache2/apache2.conf
),添加或修改以下行:IfModule mod_status.c
ExtendedStatus On
Location /server-status
SetHandler server-status
Require local
</Location>
保存配置文件并重啟Apache服務:sudo systemctl restart httpd
然后,您可以通過訪問http://your_server_ip/server-status
來查看Apache的狀態信息。Categraf:
安裝Categraf并配置監控Apache的性能指標,編輯conf/input.apache/apache.toml
文件:
[[instances]]
scrape_uri = "http://localhost/server-status/?auto"
host_override = ""
insecure = false
custom_headers = {}
log_level = "info"
測試配置并啟動Categraf服務:
./categraf --test --inputs apachesudo systemctl restart categraf
Munin: 安裝Munin服務器端和客戶端:
sudo yum install munin httpd
配置Munin服務器端,編輯/etc/munin/munin.conf
文件,添加需要監控的主機詳情。
配置Apache web服務器使其包括Munin配置,編輯/etc/apache2/conf.d/munin.conf
文件:
Alias /munin /var/www/munin
Order allow,deny
Allow from localhost 127.0.0.0/ 8 :: 1
AllowOverride None
Options ExecCGI FollowSymlinks
AddHandler cgi-script .cgi
DirectoryIndex index.cgi
AuthUserFile /etc/munin/munin.passwd
AuthType basic
AuthName "Munin stats"
require valid-user
ExpiresActive On
ExpiresDefault M310
重啟Apache服務器:
sudo service httpd restart
在客戶端機器上安裝Munin客戶端并配置munin-node.conf
文件。
使用tail
命令實時查看日志文件的最新內容:
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log
使用less
命令實時查看日志文件內容并追蹤文件末尾的內容:
less -F /var/log/apache2/access.log
創建一個名為apache_status.sh
的Shell腳本文件,并寫入以下內容:
#!/bin/bash
if ! pgrep -x httpd /dev/null; then
echo "Apache is not running. Starting httpd..."
systemctl start httpd
fi
使用crontab -e
命令添加定時任務,例如每5分鐘檢查一次:
*/5 /path/to/apache_status.sh