Linux 中检查 Apache Web Server (httpd) 正常运行时间的 4 种方法

发布于:2024-12-22 ⋅ 阅读:(11) ⋅ 点赞:(0)

注:机翻,未校。


4 Ways To Check Uptime of Apache Web Server (httpd) on Linux

November 28, 2019

by Magesh Maruthamuthu

We all know about the purpose of uptime command in Linux.
我们都知道 Linux 中 uptime 命令的目的。

It is used to check the uptime of the Linux system, how long the system has been running without restarting.
它用于检查 Linux 系统的正常运行时间,系统在不重新启动的情况下运行了多长时间。

If you want to check other service uptime like Apache, MySQL, sftp, etc. on Linux, how do you do that?
如果您想在 Linux 上检查其他服务的正常运行时间,如 Apache、MySQL、sftp 等,您该怎么做?

Each service has their own command to check the service uptime.
每个服务都有自己的命令来检查服务正常运行时间。

Also, we can check the service uptime using the ps command because there are many options to collect this information.
此外,我们可以使用 ps 命令检查服务正常运行时间,因为有很多选项可以收集此信息。

It shows when the parent process started (which I say when it restarts or starts) and how long the service runs with the [DD:HH:MM:SS] format.
它显示父进程的启动时间(我说的是它重新启动或启动的时间)以及服务以 [DD:HH:MM:SS] 格式运行的时间。

In this tutorial, we are going to show you how to check Apache web server uptime using different method.
在本教程中,我们将向您展示如何使用不同的方法检查 Apache Web 服务器的正常运行时间。

This is a small tutorial and very useful if you want to get Apache web server uptime for some reason.
这是一个小教程,如果您出于某种原因想要获得 Apache Web 服务器的正常运行时间,则非常有用。

This can be done using the following 4 methods.
这可以使用以下 4 种方法完成。

  • Apache mod_status module
  • ps command ps
  • systemctl command systemctl
  • proc filesystem (procfs)

Method-1: How to Check Apache (httpd) Web Server Uptime Using mod_status Module on Linux

方法 1:如何在 Linux 上使用 mod_status 模块检查 Apache (httpd) Web 服务器的正常运行时间

The Apache mode_status module allows a server administrator to check Apache Web server uptime, concurrent connections, and server performance with a given interval through the CLI and GUI.
Apache mode_status 模块允许服务器管理员通过 CLI 和 GUI 在给定的时间间隔内检查 Apache Web 服务器的正常运行时间、并发连接和服务器性能。

However, it is disabled by default and must be enabled to use the feature.
但是,默认情况下,它是禁用的,必须启用才能使用该功能

The Apache “mod_status” module displays the following useful information about the Apache Web server.
Apache “mod_status” 模块显示以下有关 Apache Web 服务器的有用信息。

  • Apache Web server built & version information
    Apache Web 服务器构建和版本信息
  • Web server last restart & uptime information
    Web 服务器上次重启和正常运行时间信息
  • Total number of incoming requests
    传入请求总数
  • Idle workers 空闲 worker
  • Server load & Web server CPU usage
    服务器负载和 Web 服务器 CPU 使用率
  • Total traffic (Bandwidth usage)
    总流量(带宽使用量)
  • Average number of requests per second
    每秒平均请求数
  • Average number of bytes per request
    每个请求的平均字节数
  • Number of bytes served per second
    每秒提供的字节数
  • How many requests currently being processed
    当前正在处理的请求数
  • Details about running process
    有关正在运行的进程的详细信息

How to Install and Enable Apache’s mod_status Module on Linux

如何在 Linux 上安装和启用 Apache 的 mod_status 模块

By default, the Apache “mod_status” module is installed on Apache. But reports are disabled and you have to uncomment it to access it. To do so, you need to uncomment the codes below in your Apache configuration file based on your distribution.
默认情况下,Apache “mod_status” 模块安装在 Apache 上。但是报表被禁用,您必须取消注释才能访问它。为此,您需要根据您的分配在 Apache 配置文件中取消注释以下代码。

See the following article if you want to verify the Apache web server performance using the mod_status module.
如果要使用 mod_status 模块验证 Apache Web 服务器性能,请参阅以下文章。

For RHEL/CentOS/Fedora systems, open the /etc/httpd/conf/httpd.conf file and uncomment the below codes.
对于 RHEL/CentOS/Fedora 系统,请打开 /etc/httpd/conf/httpd.conf 文件并取消注释以下代码。

[For Apache 2.2]
<Location /server-status>
  SetHandler server-status
  Order deny,allow
  Deny from all
  Allow from all
</Location>

[For Apache 2.4]
<Location "/server-status">
  SetHandler server-status
  Require host localhost
</Location>

For Debian/Ubuntu systems, open /etc/apache2/mods-enabled/status.conf file and uncomment the below codes.
对于 Debian/Ubuntu 系统,请打开 /etc/apache2/mods-enabled/status.conf 文件并取消注释以下代码。

<Location "/server-status">
  SetHandler server-status
  Require local
  #Require ip 192.0.2.024
</Location>

Save and close the file and restart the Apache web server service.
保存并关闭文件,然后重新启动 Apache Web 服务器服务。

Use the below commands to restart the Apache (httpd) server in Linux.
使用以下命令在 Linux 中重新启动 Apache (httpd) 服务器。

For SysVinit Systems – openSUSE & Debian based systems.
对于 SysVinit 系统 – 基于 openSUSE & Debian 的系统。

# service apache2 restart
or
# /etc/init.d/apache2 restart

For SysVinit Systems – RHEL (RedHat) based systems.
对于 SysVinit 系统 – 基于 RHEL (RedHat) 的系统。

# service httpd restart
or
# /etc/init.d/httpd restart

For systemd Systems – openSUSE & Debian based systems.
对于 systemd 系统 – 基于 openSUSE & Debian 的系统。

# systemctl restart apache2.service
or
# systemctl restart apache2

For systemd Systems – RHEL (RedHat) based systems.
对于 systemd 系统 – 基于 RHEL (RedHat) 的系统。

# systemctl restart httpd
or
# systemctl restart httpd.service

Run the following command to get the Apache uptime.
运行以下命令以获取 Apache 正常运行时间。

$ apachectl status
        Apache Server Status for localhost (via 127.0.0.1)
  Server Version: Apache/2.4.29 (Ubuntu)
  Server MPM: prefork
  Server Built: 2019-09-16T12:58:48
  --------------------------------------------------------------------------
  Current Time: Wednesday, 27-Nov-2019 13:06:03 IST
  Restart Time: Wednesday, 27-Nov-2019 12:24:26 IST
  Parent Server Config. Generation: 2
  Parent Server MPM Generation: 1
  Server uptime: 41 minutes 36 seconds
  Server load: 0.00 0.02 0.14
  Total accesses: 8 - Total Traffic: 88 kB
  CPU Usage: u0 s0 cu0 cs0
  .00321 requests/sec - 36 B/second - 11.0 kB/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 worker, "." Open slot with no current process

Alternatively, you can run this command using a**Linux text-based browser**.
或者,您也可以使用基于 Linux 文本的浏览器运行此命令。

$ elinks http://localhost/server-status

Apache uptime with page refresh every N seconds.
Apache 正常运行时间,每 N 秒刷新一次页面。

$ elinks http://localhost/server-status?refresh=5

Method-2: How to Check Apache (httpd) Web Server Uptime Using the ps Command on

Linux 方法 2:如何在 Linux 上使用 ps 命令检查 Apache (httpd) Web 服务器的正常运行时间

The ps command displays information about a selection of the active processes. It displays the process ID (pid=PID), the terminal associated with the process (tname=TTY), the cumulated CPU time in [DD-] hh:mm:ss format (time=TIME), and the executable name (ucmd=CMD). Output is unsorted by default.
ps 命令显示有关所选活动进程的信息。它显示进程 ID (pid=PID)、与进程关联的终端 (tname=TTY)、[DD-] hh:mm:ss 格式的累积 CPU 时间 (time=TIME) 和可执行文件名称 (ucmd=CMD)。默认情况下,输出未排序。

To do so, you need to find the PID of the Apache process, and it can be easily identified using the pidof command.
为此,您需要找到 Apache 进程的 PID,并且可以使用 pidof 命令轻松识别它。

For RPM based systems:
对于基于 RPM 的系统:

# pgrep httpd | head -1
10915

For DEB based systems:
对于基于 DEB 的系统:

# pgrep apache2 | head -1
1111

Once you get the Apache PID, use the following command to get it.
获取 Apache PID 后,使用以下命令获取它。

# ps -p 10915 -o etime
  ELAPSED
  05:59:59

The above output shows the elapsed time since the process was started. As per the above output, it’s up and running 5 hours, 59 mins, and 59 sec.
上述输出显示自进程启动以来经过的时间。根据上述输出,它已启动并运行 5 hours, 59 mins, and 59 sec

Method-3: How to Check Apache (httpd) Web Server Uptime Using the systemctl Command on Linux

方法 3:如何在 Linux 上使用 systemctl 命令检查 Apache (httpd) Web 服务器的正常运行时间

The systemctl command is used to control the systemd service manager. This is a replacement for the old SysVinit system management, and most of the modern Linux operating systems have been moved to the systemd.
systemctl 命令用于控制 systemd 服务管理器。这是旧 SysVinit 系统管理的替代品,大多数现代 Linux 操作系统已迁移到 systemd。

# systemctl status httpd

● httpd.service - Web server Apache
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Active: active (running) since Wed 2019-11-27 03:43:37 UTC; 5h 50min ago
 Process: 10907 ExecStop=/usr/local/apache/bin/apachectl graceful-stop (code=exited, status=0/SUCCESS)
 Process: 11025 ExecReload=/usr/local/apache/bin/apachectl graceful (code=exited, status=0/SUCCESS)
 Process: 10912 ExecStart=/usr/local/apache/bin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 10915 (httpd)
  CGroup: /system.slice/httpd.service
      ├─10915 /usr/local/apache/bin/httpd -k start
      ├─11082 /usr/local/apache/bin/httpd -k start
      ├─11083 /usr/local/apache/bin/httpd -k start
      ├─11084 /usr/local/apache/bin/httpd -k start
      └─11681 /usr/local/apache/bin/httpd -k start

Nov 27 03:43:37 ns1.nsforcdn.com systemd [1]: Started Web server Apache.
Nov 27 03:43:38 ns1.nsforcdn.com systemd [1]: Reloading Web server Apache.
Nov 27 03:43:38 ns1.nsforcdn.com apachectl [11025]: AH00112: Warning: DocumentRoot [/home/nowdigitaleasy/public_html/billing] does not exist
Nov 27 03:43:38 ns1.nsforcdn.com apachectl [11025]: AH00112: Warning: DocumentRoot [/home/nowdigitaleasy/public_html/billing] does not exist
Nov 27 03:43:38 ns1.nsforcdn.com apachectl [11025]: AH00112: Warning: DocumentRoot [/home/nutechnologyinc/public_html/poc.nutechnologyinc.com] does not exist
Nov 27 03:43:38 ns1.nsforcdn.com apachectl [11025]: AH00112: Warning: DocumentRoot [/home/witskills/public_html] does not exist
Nov 27 03:43:38 ns1.nsforcdn.com apachectl [11025]: AH00112: Warning: DocumentRoot [/home/witskills/public_html] does not exist
Nov 27 03:43:38 ns1.nsforcdn.com systemd [1]: Reloaded Web server Apache.
Hint: Some lines were ellipsized, use -l to show in full.

Method-4: How to Check Apache (httpd) Web Server Uptime Using the proc filesystem (procfs) on Linux

方法 4:如何在 Linux 上使用 proc 文件系统 (procfs)检查 Apache (httpd) Web 服务器的正常运行时间

The proc filesystem (procfs) is a special filesystem in Unix-like operating systems that presents information about processes and other system information.
proc 文件系统 (procfs) 是类 Unix 操作系统中的一种特殊文件系统,用于显示有关进程的信息和其他系统信息。

It’s sometimes referred to as a process information pseudo-file system. It doesn’t contain ‘real’ files but run time system information (e.g. system memory, devices mounted, hardware configuration, etc).
它有时称为进程信息伪文件系统。它不包含 “真实” 文件,而是运行时系统信息(例如系统内存、挂载的设备、硬件配置等)。

Once you have Apache PID, use the following proc file system to identify it. As per the below output the httpd process has been running since**Aug 05 at 17:20**.
拥有 Apache PID 后,请使用以下 proc 文件系统来识别它。根据以下输出,httpd 进程自 8 月 5 日 17:20 以来一直在运行。

# ls -ld /proc/10915

dr-xr-xr-x. 9 root root 0 Aug 5 17:20 /proc/10915/

via: