网站搜索

在 Linux 中管理 Apache Web 服务器的有用命令


在本教程中,我们将描述一些最常用的 Apache (HTTPD) 服务管理命令,您作为开发人员或系统管理员应该了解这些命令,并且应该将这些命令放在您的指尖。我们将显示 SystemdSysVinit 的命令。

另请阅读:每个 Linux 用户必须了解的 10 个最常用的 Nginx 命令

确保以下命令必须以 root 或 sudo 用户身份执行,并且应适用于任何 Linux 发行版,例如 CentOSRHELFedora DebianUbuntu

安装 Apache 服务器

要安装 Apache Web 服务器,请使用默认的分发包管理器,如图所示。

sudo apt install apache2	    [On Debian/Ubuntu]
sudo yum install httpd	    [On RHEL/CentOS]
sudo dnf install httpd	    [On Fedora 22+]
sudo zypper install apache2	    [On openSUSE]

检查 Apache 版本

要检查 Linux 系统上已安装的 Apache Web 服务器版本,请运行以下命令。

sudo httpd -v
OR
sudo apache2 -v
样本输出
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov  5 2018 01:47:09

如果要显示 Apache 版本号和编译设置,请使用 -V 标志,如下所示。

sudo httpd -V
OR
sudo apache2 -V
样本输出
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov  5 2018 01:47:09
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

检查 Apache 配置语法错误

要检查 Apache 配置文件是否有任何语法错误,请运行以下命令,这将在重新启动服务之前检查配置文件的有效性。

sudo httpd -t
OR
sudo apache2ctl -t
样本输出
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using linux-console.net. 
Set the 'ServerName' directive globally to suppress this message
Syntax OK

启动 Apache 服务

要启动 Apache 服务,请运行以下命令。

------------ On CentOS/RHEL ------------ 
sudo systemctl start httpd     [On Systemd]
sudo service httpd start 	 [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl start apache2   [On Systemd]
sudo service apache2 start     [On SysVInit]

启用 Apache 服务

前面的命令仅同时启动 Apache 服务,要使其在系统启动时自动启动,请运行以下命令。

------------ On CentOS/RHEL ------------ 
sudo systemctl enable httpd     [On Systemd]
sudo chkconfig httpd on 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl enable apache2   [On Systemd]
sudo chkconfig apache2 on       [On SysVInit]

重新启动 Apache 服务

要重新启动 Apache(停止,然后启动服务),请运行以下命令。

------------ On CentOS/RHEL ------------ 
sudo systemctl restart httpd     [On Systemd]
sudo service httpd restart 	   [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl restart apache2   [On Systemd]
sudo service apache2 restart     [On SysVInit]

查看 Apache 服务状态

要查看Apache服务运行时状态信息,请运行以下命令。

------------ On CentOS/RHEL ------------ 
sudo systemctl status httpd     [On Systemd]
sudo service httpd status 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl status apache2   [On Systemd]
sudo service apache2 status     [On SysVInit]

重新加载 Apache 服务

如果您对 Apache 服务器配置进行了任何更改,则可以通过运行以下命令来指示服务重新加载其配置。

------------ On CentOS/RHEL ------------ 
sudo systemctl reload httpd     [On Systemd]
sudo service httpd reload 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl reload apache2   [On Systemd]
sudo service apache2 reload     [On SysVInit]

停止 Apache 服务

要停止 Apache 服务,请使用以下命令。

------------ On CentOS/RHEL ------------ 
sudo systemctl stop httpd       [On Systemd]
sudo service httpd stop 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
sudo systemctl stop apache2     [On Systemd]
sudo service apache2 stop     [On SysVInit]

显示 Apache 命令帮助

最后但并非最不重要的一点是,您可以通过运行以下命令来获取有关 systemd 下的 Apache 服务命令的帮助。

sudo httpd -h
OR
sudo apache2 -h		
OR
systemctl -h apache2	
样本输出
Usage: httpd [-D name] [-d directory] [-f file]
             [-C "directive"] [-c "directive"]
             [-k start|restart|graceful|graceful-stop|stop]
             [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
Options:
  -D name            : define a name for use in  directives
  -d directory       : specify an alternate initial ServerRoot
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -c "directive"     : process directive after reading config files
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
  -V                 : show compile settings
  -h                 : list available command line options (this page)
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed vhost settings
  -t -D DUMP_RUN_CFG : show parsed run settings
  -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
  -t -D DUMP_MODULES : show all loaded modules 
  -M                 : a synonym for -t -D DUMP_MODULES
  -t                 : run syntax check for config files
  -T                 : start without DocumentRoot(s) check
  -X                 : debug mode (only one worker, do not detach)

您可以通过查阅:如何在 Linux 中使用“Systemctl”管理“Systemd”服务和单元来找到有关 systemctl 的更多信息。

您可能还想阅读以下 Apache 相关文章。

  1. 提升 Apache Web 服务器性能的 5 个技巧
  2. 如何监控 Apache Web 服务器负载和页面统计信息
  3. 如何使用“Apache GUI”工具管理 Apache Web 服务器
  4. 如何在 Linux 中更改 Apache HTTP 端口
  5. 13 个 Apache Web 服务器安全和强化技巧
  6. 使用 Mod_Security 和 Mod_evasive 模块保护 Apache 免受暴力或 DDoS 攻击

目前为止就这样了!在本文中,我们解释了您应该了解的最常用的 Apache/HTTPD 服务管理命令,包括启动、启用、重新启动和停止 Apache。如有任何问题或意见,您可以随时通过下面的反馈表联系我们。