在本教程中,我们将介绍一些您应该了解的开发人员或系统管理员最常用的Apache(HTTPD)服务管理命令,并且应该让这些命令保持在您的指尖。 我们将显示Systemd和SysVinit的命令。
另请参阅 : 每个Linux用户必须知道的10个最常用的Nginx命令
确保以下命令必须以root或sudo用户身份执行,并且应该适用于任何Linux发行版,如CentOS , RHEL , Fedora Debian和Ubuntu 。
要安装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]
要在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配置文件是否存在任何语法错误,请运行以下命令,该命令将在重新启动服务之前检查配置文件的有效性。
$ sudo httpd -t OR $ sudo apache2ctl -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using howtoing.com. Set the 'ServerName' directive globally to suppress this message Syntax OK
要启动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服务,要使其在系统引导时自动启动,请运行以下命令。
------------ 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( 停止然后启动该服务),请运行以下命令。
------------ 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服务运行时状态信息,请运行以下命令。
------------ 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服务器配置进行了任何更改,则可以通过运行以下命令指示服务重新加载其配置。
------------ 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服务,请使用以下命令。
------------ 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]
最后但并非最不重要的是,您可以通过运行以下命令获得有关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)
您可以通过咨询以下内容找到有关systemctl的更多信息: 如何在Linux中使用“Systemctl”管理“Systemd”服务和单元 。
您可能还想阅读以下Apache相关文章。
目前为止就这样了! 在本文中,我们已经解释了您应该了解的最常用的Apache / HTTPD服务管理命令,包括启动,启用,重新启动和停止Apache。 如有任何问题或意见,您可以随时通过下面的反馈表联系我们。
关注云架构公众号
Linux入门
QQ交流群:308781113