如何监视Apache Web服务器的负载和页面统计

在本文中,您将学习如何使用您的Linux发行版(如CentOS、RHEL和Fedora)中的mod_status模块来监视Apache Web服务器的负载和请求。

什么是mod_status?

mod_status是一个Apache模块,它帮助监视Web服务器负载和当前httpd连接,并提供一个可以通过Web浏览器访问的HTML界面。

Apache的mod_status显示一个简单的HTML页面,包含有关Web服务器当前统计信息的信息,包括:

  • 传入请求的总数
  • 字节总数和服务器计数
  • Web服务器的CPU使用率
  • 服务器负载
  • 服务器运行时间
  • 总流量
  • 空闲工作进程的总数
  • 具有相应客户端的PID和许多其他信息。

默认的Apache项目将其服务器统计页面开放给公众。要查看繁忙网站的状态页面演示,请访问。

测试环境

我们使用以下测试环境来探索有关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,目录限制。例如,我将其简化为允许顺序拒绝以及它允许全部

<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指令设置状态“开启”。

# 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模块是一个非常方便的监控工具,用于监控Web服务器活动的性能,并且能够自行突出显示问题。有关更多信息,请阅读状态页面,这可以帮助您成为更成功的Web服务器管理员。

  1. Apache mod_status主页

目前就介绍这么多关于mod_status的内容,我们将在未来的教程中提供一些关于Apache的技巧和提示。在那之前,请保持对Tecmint.com的关注,并不要忘记添加您宝贵的评论。

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