如何在CentOS/Ubuntu上检查Apache状态和正常运行时间
在本教程中,我将使用各种命令行实用程序和其他方法来解释我们最新的CentOS 7.4和Ubuntu 16.04发行版上的Apache Uptime状态的不同方式。
PS工具
ps是一个显示当前正在运行的进程的程序。
此实用程序显示有关在Linux系统上运行的活动进程选择的信息,我们可以使用Grep命令用它来检查Apache Service正常运行时间,如下所示。
关于ubuntu/debian
下面的示例输出显示Apache2服务已经运行了8小时,30分钟和5秒,考虑到root身份运行。
# ps -eo comm,etime,user | grep apache2 apache2 08:30:05 root apache2 08:13:59 www-data apache2 08:13:59 www-data # ps -eo comm,etime,user | grep root | grep apache2 apache2 08:30:18 root
在CentOS/RHEL
下面的示例输出显示,考虑到root身份运行,Apache服务已运行2天,5小时,53分和17秒。
# ps -eo comm,etime,user | grep httpd httpd 2-05:53:17 root httpd 11:07:49 apache httpd 11:07:49 apache httpd 11:07:49 apache httpd 11:07:49 apache httpd 11:07:49 apache httpd 09:19:24 apache # ps -eo comm,etime,user | grep root | grep httpd httpd 2-05:53:17 root
其中标志:
-e: enables selection of every processes on the system. -o: is used to specify output comm: command etime: mention process execution time user: specifies the process owner
systemctl实用程序
systemctl命令是用于管理和控制系统的基本命令,包括系统服务。
它允许我们启用,禁用,查看,启动,停止或者重新启动系统服务。
我们可以使用此SystemCtl状态子命令查看我们的Apache服务的状态,如下所示:
关于ubuntu/debian
我们可以使用以下命令在Ubuntu/debian服务器上获取Apache2正常运行时间。
# systemctl status apache2 | grep -i active Active: active (running) since Sun 2016-03-04 14:45:59 UTC; 19min ago
在CentOS/RHEL
我们可以使用相同的"systemctl"命令查找httpd状态
# systemctl status httpd | grep -i active Active: active (running) since Fri 2016-03-02 08:47:46 UTC; 2 days ago
apachectl实用程序
Apachectl是Apache超文本传输协议(HTTP)服务器的前端控制接口。
它旨在帮助管理员来控制Apache守护程序的功能。
此方法需要在服务器中安装并启用Mod_Status模块以使其根据需要运行。
在浏览器上呈现HTML页面,它以易于可读的形式给出当前服务器统计信息。
默认情况下,所有服务器都在所有服务器中启用。
要使用从命令行使用此实用程序检查Apache状态,我们需要启用命令行Web浏览器,如Lynx或者Elinks。
或者,它将报告如下:
# apachectl status /usr/sbin/apachectl: 101: /usr/sbin/apachectl: www-browser: not found 'www-browser -dump http://localhost:80/server-status' failed. Maybe you need to install a package providing www-browser or you need to adjust the APACHE_LYNX variable in /etc/apache2/envvars
因此,我安装了此命令行Web浏览器以使其从服务器终端工作。
关于ubuntu/debian
我们可以安装Lynx命令行浏览器以在此处启用Apachectl状态。
# apt install lynx # apachectl status Apache Server Status for localhost (via ::1) Server Version: Apache/2.4.18 (Ubuntu) Server MPM: event Server Built: 2016-09-18T15:09:02 __________________________________________________________________
Current Time: Sunday, 04-Mar-2016 15:24:38 UTC Restart Time: Sunday, 04-Mar-2016 14:45:58 UTC Parent Server Config. Generation: 1 Parent Server MPM Generation: 0 Server uptime: 38 minutes 39 seconds Server load: 0.00 0.00 0.00 Total accesses: 0 - Total Traffic: 0 kB CPU Usage: u0 s0 cu0 cs0 0 requests/sec - 0 B/second 1 requests currently being processed, 49 idle workers PID Connections Threads Async connections total accepting busy idle writing keep-alive closing 18424 0 yes 1 24 0 0 0 18425 0 yes 0 25 0 0 0 Sum 0 1 49 0 0 0 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 worker, "." Open slot with no current process
在CentOS/RHEL
我们可以安装Elinks命令行浏览器以在此处启用Apachectl状态。
# yum install elinks # apachectl status * httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Sun 2016-03-04 15:28:20 UTC; 6min ago Docs: man:httpd(8) man:apachectl(8) Process: 21280 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS) Process: 14448 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS) Main PID: 21286 (httpd) Status: "Total requests: 1; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service |-21286 /usr/sbin/httpd -DFOREGROUND |-21288 /usr/sbin/httpd -DFOREGROUND |-21289 /usr/sbin/httpd -DFOREGROUND |-21290 /usr/sbin/httpd -DFOREGROUND |-21291 /usr/sbin/httpd -DFOREGROUND |-21292 /usr/sbin/httpd -DFOREGROUND `-21293 /usr/sbin/httpd -DFOREGROUND Mar 04 15:28:20 li694-77.members.linode.com systemd[1]: Starting The Apache HTTP Server... Mar 04 15:28:20 li694-77.members.linode.com systemd[1]: Started The Apache HTTP Server.
启用Mod-Status模块
或者,要从图形Web浏览器查看Apache Web服务器状态信息,我们需要在Apache配置中启用服务器状态模块。
在CentOS/RHEL
要启用服务器状态模块,我们需要在服务器中创建名为"/etc/httpd/conf.d/server-status.conf"的文件,其中包含以下内容并重新启动Apache以使更改有效。
# cat /etc/httpd/conf.d/server-status.conf <Location "/server-status"> SetHandler server-status #Require host localhost #uncomment to only allow requests from localhost </Location>
现在,我们可以浏览URLhttp://server-ip/server-status
以查看状态。
关于ubuntu/debian
同样,我们可以在Debian/Ubuntu服务器上注释在"/etc/apache2/mods-enabled/status.conf中的以下条目。
重新加载Apache2配置以使这些更改有效。
# cat /etc/apache2/mods-enabled/status.conf <IfModule mod_status.c> # Allow server status reports generated by mod_status, # with the URL of http://servername/server-status # Uncomment and change the "192.0.2.0/24" to allow access from other hosts. <Location /server-status> SetHandler server-status #Require local #uncomment to only allow requests from localhost #Require ip 192.0.2.0/24 #uncomment and change the “192.0.2.0/24” to allow access from other hosts. </Location> # Keep track of extended status information for each request ExtendedStatus On # Determine if mod_status displays the first 63 characters of a request or # the last 63, assuming the request itself is greater than 63 chars. # Default: Off #SeeRequestTail On <IfModule mod_proxy.c> # Show Proxy LoadBalancer status in mod_status ProxyStatus On </IfModule> </IfModule> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
现在,我们可以浏览URLhttp://server-ip/server-status
以查看状态。
或者,我们可以使用像Elinks或者Lynx这样的文本模式浏览器来运行命令,以查看Apache正常运行时间
#elinks http://localhost/server-status or #lynx http://localhost/server-status
从Apache日志跟踪正常运行时间
在Ubuntu/Debian服务器上,我们可以查看位于/var/log/apache2/error.log
的Apache日志中,以标识使用以下命令的最新Apache Restart尝试:
# tail -f /var/log/apache2/error.log | grep resuming [Sun Mar 04 06:25:02.611615 2016] [mpm_event:notice] [pid 15230:tid 140159687124864] AH00489: Apache/2.4.18 (Ubuntu) configured -- resuming normal operations [Sun Mar 04 14:45:58.950825 2016] [mpm_event:notice] [pid 18421:tid 139913481500544] AH00489: Apache/2.4.18 (Ubuntu) configured -- resuming normal operations [Sun Mar 04 15:42:25.768467 2016] [mpm_event:notice] [pid 18421:tid 139913481500544] AH00489: Apache/2.4.18 (Ubuntu) configured -- resuming normal operations
同样,在CentOS/RHEL服务器上,我们可以查看位于/var/log/httpd/error_log
的Apache日志,以标识下面使用此命令的最新Apache Restart尝试:
# tail -f /var/log/httpd/error_log | grep resuming [Sun Mar 04 15:28:20.579455 2016] [mpm_prefork:notice] [pid 21286] AH00163: Apache/2.4.6 (CentOS) configured -- resuming normal operations
从WebHosting控制面板跟踪正常运行时间
我们熟悉WHM,Directadmin,Plesk,Interworx,Webmin,Kloxo等的各种网络托管控制面板。
我将通过以下两个广泛使用的Web托管面板上查看Apache正常运行时间来查看两个广泛使用的WHM和Interworx的步骤:
WHM
在WHM服务器中,我们可以通过通过路径WHM主页>>服务器状态>> Apache状态浏览到URL >>> https://serverip或者hostname:2087
如屏幕截图所示。
interworx.
Interworx Web控制面板是基于Linux的Web托管控制面板,用于专用和VPS托管。
它可以由系统管理员和管理员轻松使用。
在此面板中,我们可以通过通过以下步骤来查看Apache正常运行时间:
登录nodeworx面板>>> https://hostname或者serverip:2443 浏览路径nodeworx主页>>系统服务>> Web服务器
在那些没有用户友好界面的面板中用于检查Web服务器状态,我们可以启用Mod_Status模块从浏览器查看其正常运行时间。
所有这些方法允许服务器管理员了解他们的服务器执行程度。