ウェブサイト検索

Linux で Apache Web サーバーを管理するための便利なコマンド


このチュートリアルでは、開発者またはシステム管理者が知っておくべき最も一般的に使用されるApache (HTTPD) サービス管理コマンドのいくつかについて説明します。これらのコマンドはすぐに使えるようにしておいてください。ここではSystemdSysVinit の両方のコマンドを示します。

こちらもお読みください: すべての Linux ユーザーが知っておくべき、最もよく使用される 10 の Nginx コマンド

次のコマンドは root または sudo ユーザーとして実行する必要があり、CentOSRHEL などの Linux ディストリビューションで動作することを確認してください。 FedoraDebian、およびUbuntu

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)

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 の起動、有効化、再起動、停止など、知っておくべき最も一般的に使用される Apache/HTTPD サービス管理コマンドについて説明しました。ご質問やご意見がございましたら、いつでも以下のフィードバック フォームからお問い合わせいただけます。