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

在本教程中,我們將描述一些最常用的Apache(HTTPD)服務管理命令,作為開發人員或系統管理員,您應該了解這些命令並將其牢記在心。我們將展示SystemdSysVinit的命令。

閱讀更多每個Linux用戶都應該知道的10個最常用的Nginx命令

請確保以下命令必須以rootsudo用戶身份執行,並且應該適用於任何Linux發行版,如CentOSRHELFedoraDebianUbuntu

安裝Apache伺服器

要安裝Apache網頁伺服器,請使用您的默認發行版套件管理器,如下所示。

$ 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網頁伺服器版本,運行以下命令。

$ 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 tecmint.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 命令幫助

最後,您可以通過執行以下命令獲取有關 Apache 服務命令在 systemd 下的幫助。

$ 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 相關的文章。

  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。如有任何问题或意见,您可以随时通过下方的反馈表联系我们。

Source:
https://www.tecmint.com/manage-apache-web-server-in-linux/