如何監控 Apache Web 伺服器負載和頁面統計

在這篇文章中,您將學習如何使用您的Linux發行版(如CentOS、RHEL和Fedora)監控Apache網絡伺服器的負載和請求,使用mod_status模組。

什麼是mod_status?

mod_status是一個Apache模組,可幫助監控網絡伺服器的負載和當前httpd連接,並提供一個可以通過網頁瀏覽器訪問的HTML界面。

Apache的mod_status顯示一個包含有關當前網絡伺服器統計信息的純HTML頁面,包括:

  • 總請求數
  • 總字節數和伺服器計數
  • Web伺服器的CPU使用率
  • 伺服器負載
  • 伺服器正常運行時間
  • 總流量
  • 空閒工作者的總數
  • 具有相應客戶端的PID以及許多其他信息。

默認情況下,Apache Project啟用了他們的伺服器統計頁面以向公眾開放。要查看繁忙網站狀態頁的演示,請訪問:

測試環境

我們在本文中使用了以下測試環境,以探索更多關於mod_status的信息,並提供一些實際示例和截圖。

  1. 作業系統 – CentOS 8/7
  2. 應用程式 – Apache Web 伺服器
  3. IP 位址 – 5.175.142.66
  4. 文件根目錄 – /var/www/html
  5. Apache 設定檔 – /etc/httpd/conf/httpd.conf
  6. 預設 HTTP 連接埠 – 80 TCP
  7. 測試設定設定 – httpd -t

本教程的先決條件是您應該已經知道如何安裝和配置一個基本 Apache 伺服器。如果您不知道如何設置 Apache,請閱讀以下文章,這可能會幫助您設置自己的 Apache Web 伺服器。

  1. 在 Linux 中創建您自己的 Web 伺服器並託管網站

如何在 Apache 中啟用 mod_status

默認的 Apache 安裝已啟用mod_status。如果沒有,請確保在 Apache 設定檔中啟用它。

[root@tecmint ~]# vi /etc/httpd/conf/httpd.conf

搜索“mod_status”這個詞,或者繼續向下滾動,直到找到包含該詞的行。

#LoadModule status_module modules/mod_status.so

如果您在“LoadModule”開頭看到一個‘#’字符,這意味著 mod_status 被禁用。刪除‘#’以啟用 mod_status。

LoadModule status_module modules/mod_status.so

配置 mod_status

現在再次搜索“位置”這個詞,或者向下滾動,直到找到一個看起來像以下的mod_status部分。

# Allow server status reports generated by mod_status,
# with the URL of http://servername/server-status
# Change the ".example.com" to match your domain to enable.
#
#<Location /server-status>
#    SetHandler server-status
#    Order deny,allow
#    Deny from all
#    Allow from .example.com
#</Location>

在上述部分,根据您的需求取消注释位置指令SetHandler目录限制。例如,我简单地保留Order Allowdeny和允许所有的allowed for all

<Location /server-status>
   SetHandler server-status
   Order allow,deny
   Deny from all
   Allow from all 
</Location>

注意:上述配置是默认的Apache网站(单个网站)的默认配置。如果您创建了一个或多个Apache虚拟主机,上述配置将不起作用。

因此,基本上,您需要为在Apache中配置的任何域名的每个虚拟主机定义相同的配置。例如,mod_status的虚拟主机配置将如下所示。

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html/example.com
    ServerName example.com
    ErrorLog logs/example.com-error_log
    CustomLog logs/example.com-access_log common
<Location /server-status>
   SetHandler server-status
   Order allow,deny
   Deny from all
   Allow from example.com 
</Location>
</VirtualHost>

启用ExtendedStatus

ExtendedStatus”设置会向统计页面添加更多信息,如CPU使用率每秒请求总流量等。要启用它,请编辑相同的httpd.conf文件,并搜索“Extended”一词,取消注释该行,并为ExtendedStatus指令设置状态“On”。

# ExtendedStatus controls whether Apache will generate "full" status
# information (ExtendedStatus On) or just basic information (ExtendedStatus
# Off) when the "server-status" handler is called. The default is Off.
#
ExtendedStatus On

重新启动Apache

现在确保您已正确启用和配置了Apache服务器状态页面。您还可以使用以下命令检查httpd.conf配置中的错误。

[root@tecmint ~]# httpd -t

Syntax OK

一旦您得到语法是OK,您就可以重新启动 httpd服务。

[root@tecmint ~]# service httpd restart
OR
[root@tecmint ~]# systemctl restart httpd
Stopping httpd:                                          [  OK  ]
Starting httpd:                                          [  OK  ]

访问mod_status页面

Apache状态页面将通过您的域名以“/server-status”的形式访问以下URL。

http://serveripaddress/server-status

OR

http://serev-hostname/server-status

启用ExtendedStatus后,您将看到类似以下页面。

Apache mod_status View

在上面的快照中,您可以看到一个HTML界面,显示有关服务器正常运行时间进程ID及其相应客户端、他们正在尝试访问的页面的所有信息。

它还显示了用于显示状态的所有缩写的含义和用法,这有助于我们更好地了解情况。

您还可以每隔一段时间(比如5秒)刷新页面以查看更新的统计信息。要设置自动刷新,请在URL的末尾添加“?refresh=N”。其中N可以替换为您希望页面刷新的秒数。

http://serveripaddress/server-status/?refresh=5
Apache mod_status Refresh

命令行状态页面查看

您还可以使用名为links或lynx的特殊命令行浏览器从命令行界面查看Apache状态页面。您可以按照以下所示使用名为yum的默认软件包管理器工具安装它们。

# yum install links

OR

# yum install lynx

一旦安装完成,您可以通过以下命令在终端上获得相同的统计信息。

[root@tecmint ~]# links http://serveripaddress/server-status
OR
[root@tecmint ~]# lynx http://serveripaddress/server-status
OR
[root@tecmint ~]#  /etc/init.d/httpd fullstatus
                     Apache Server Status for localhost
   Server Version: Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3
   Server Built: Aug 13 2013 17:29:28

   --------------------------------------------------------------------------
   Current Time: Tuesday, 14-Jan-2014 04:34:13 EST
   Restart Time: Tuesday, 14-Jan-2014 00:33:05 EST
   Parent Server Generation: 0
   Server uptime: 4 hours 1 minute 7 seconds
   Total accesses: 2748 - Total Traffic: 9.6 MB
   CPU Usage: u.9 s1.06 cu0 cs0 - .0135% CPU load
   .19 requests/sec - 695 B/second - 3658 B/request
   1 requests currently being processed, 4 idle workers
 .__.__W...

   Scoreboard Key:
   "_" Waiting for Connection, "S" Starting up, "R" Reading Request,
   "W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,
   "C" Closing connection, "L" Logging, "G" Gracefully finishing,
   "I" Idle cleanup of a worker, "." Open slot with no current process

Srv PID     Acc    M CPU   SS  Req Conn Child Slot     Client        VHost             Request
0-0 -    0/0/428   . 0.30 5572 0   0.0  0.00  1.34 127.0.0.1      5.175.142.66 OPTIONS * HTTP/1.0
                                                                               GET
1-0 5606 0/639/639 _ 0.46 4    0   0.0  2.18  2.18 115.113.134.14 5.175.142.66 /server-status?refresh=5
                                                                               HTTP/1.1
                                                                               GET
2-0 5607 0/603/603 _ 0.43 0    0   0.0  2.09  2.09 115.113.134.14 5.175.142.66 /server-status?refresh=5
                                                                               HTTP/1.1
3-0 -    0/0/337   . 0.23 5573 0   0.0  0.00  1.09 127.0.0.1      5.175.142.66 OPTIONS * HTTP/1.0
                                                                               GET
4-0 5701 0/317/317 _ 0.23 9    0   0.0  1.21  1.21 115.113.134.14 5.175.142.66 /server-status?refresh=5
                                                                               HTTP/1.1
                                                                               GET
5-0 5708 0/212/213 _ 0.15 6    0   0.0  0.85  0.85 115.113.134.14 5.175.142.66 /server-status?refresh=5
                                                                               HTTP/1.1
6-0 5709 0/210/210 W 0.16 0    0   0.0  0.84  0.84 127.0.0.1      5.175.142.66 GET /server-status
                                                                               HTTP/1.1
7-0 -    0/0/1     . 0.00 5574 0   0.0  0.00  0.00 127.0.0.1      5.175.142.66 OPTIONS * HTTP/1.0

   --------------------------------------------------------------------------

    Srv  Child Server number - generation
    PID  OS process ID
    Acc  Number of accesses this connection / this child / this slot
     M   Mode of operation
    CPU  CPU usage, number of seconds
    SS   Seconds since the beginning of the most recent request
    Req  Milliseconds required to process most recent request
   Conn  Kilobytes transferred this connection
   Child Megabytes transferred this child
   Slot  Total megabytes transferred this slot
   --------------------------------------------------------------------------

    Apache/2.2.15 (CentOS) Server at localhost Port 80

結論

Apache的mod_status模組是一個非常方便的監控工具,用於監控網頁伺服器活動的表現,並能夠自行突顯問題。欲瞭解更多資訊,請閱讀能幫助您成為更成功的網頁伺服器管理員的狀態頁面。

  1. Apache mod_status 首頁

目前就介紹這麼多關於mod_status,我們將在未來的教學中提供更多Apache 的技巧和提示。在那之前,請保持 Geeky,關注Tecmint.com,並不要忘記留下您寶貴的評論。

Source:
https://www.tecmint.com/monitor-apache-web-server-load-and-page-statistics/