站长资讯网
最全最丰富的资讯网站

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

在本教程中,我们将介绍一些您应该了解的开发人员或系统管理员最常用的Apache(HTTPD)服务管理命令,并且应该让这些命令保持在您的指尖。 我们将显示Systemd和SysVinit的命令。

确保以下命令必须以root或sudo用户身份执行,并且应该适用于任何Linux发行版,如CentOS,RHEL,Fedora Debian和Ubuntu。

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

安装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

$ sudo apache2 -v

输出示例

Server version: Apache/2.4.6 (CentOS)
Server built:  Nov  5 2018 01:47:09

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

$ sudo httpd -V
或者
$ 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
或者
$ sudo apache2ctl -t

输出示例

AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using linuxidc.com.
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
或者
$ sudo apache2 -h 
或者
$ 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)

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

如何在Ubuntu 18.04上安装和配置Apache 2 Web服务器  http://www.info110.com/Linux/2018-11/155507.htm

赞(0)
分享到: 更多 (0)