7.

Apache HTTP Server コマンド一覧完全リファレンス

編集
この記事の要点
  • apachectl は Apache 制御の中核。start / stop / restart / reload / graceful / configtest / status
  • systemctl 経由なら systemctl reload httpd / systemctl status apache2
  • バージョンとビルド: httpd -V(詳細)/ httpd -v(簡易)
  • モジュール: httpd -l(静的)/ httpd -M(動的含む全て)
  • 構文: httpd -t / apachectl configtestreload 前に必ず実行
  • 仮想ホスト一覧: httpd -S。フォアグラウンド起動: apachectl -X(デバッグ)
  • 認証: htpasswd -c .htpasswd user で BASIC 認証ファイル作成

apachectl: 中核コマンド

apachectl は Apache HTTP Server を制御する公式ラッパー。OS のサービス管理コマンド(systemctl)と併用しますが、Apache 固有のサブコマンドが充実しています。

# 起動・停止・再起動
sudo apachectl start
sudo apachectl stop
sudo apachectl restart        # 即時再起動(接続切断あり)

# Graceful: 既存接続を維持したまま設定再読み込み(推奨)
sudo apachectl graceful

# Graceful stop: 既存接続が完了したら停止
sudo apachectl graceful-stop

# 設定構文チェック(reload 前必須)
sudo apachectl configtest
# → Syntax OK が出れば安全

# 簡易ステータス(mod_status 必要)
apachectl status
apachectl fullstatus

systemctl 経由

RHEL / Ubuntu の現代的なディストロでは systemctl 経由が標準。サービス名は OS で異なります:

# RHEL / CentOS / Rocky / AlmaLinux
sudo systemctl start   httpd
sudo systemctl stop    httpd
sudo systemctl reload  httpd
sudo systemctl restart httpd
sudo systemctl status  httpd
sudo systemctl enable  httpd   # 自動起動 ON
sudo systemctl disable httpd   # 自動起動 OFF

# Debian / Ubuntu
sudo systemctl reload apache2
sudo systemctl status apache2

# サービスのログ
sudo journalctl -u httpd -f
sudo journalctl -u apache2 --since "10 min ago"

httpd 本体のオプション

オプション用途
-vバージョン簡易表示
-Vバージョン + ビルド情報 + MPM + コンパイル設定
-l静的リンクされた組み込みモジュール一覧
-M動的ロード分も含む全モジュール一覧
-t構文チェックのみ(起動しない)
-S仮想ホストの設定状況をダンプ
-D NAMEconf 内の を有効化
-f /path/to/httpd.conf別の設定ファイルを指定
-Xフォアグラウンド + シングルプロセス(デバッグ用)
-k start|stop|restart|gracefulWindows 用の制御サブコマンド

具体例: 設定確認系

# 現在のバージョンと MPM を確認
httpd -V
# Server version: Apache/2.4.62 (Red Hat Enterprise Linux)
# Server MPM:     event
# Architecture:   64-bit
# Server compiled with....

# 全モジュール一覧
httpd -M
# core_module (static)
# so_module (static)
# rewrite_module (shared)
# ssl_module (shared)
# ...

# 仮想ホスト一覧(どのドメインがどのファイルから読まれているか)
httpd -S
# VirtualHost configuration:
# *:443         example.com (/etc/httpd/conf.d/example.conf:1)
# *:80          example.com (/etc/httpd/conf.d/example.conf:30)

# 構文チェック
sudo httpd -t
# Syntax OK

# デバッグ: フォアグラウンドで起動
sudo apachectl -X
# Ctrl+C で停止

Debian 系特有のスクリプト

Debian / Ubuntu には Apache モジュール・サイトを有効化するヘルパーがあります:

# モジュール
sudo a2enmod  rewrite ssl headers proxy proxy_http
sudo a2dismod cgi
ls /etc/apache2/mods-enabled/

# 仮想ホスト
sudo a2ensite  example.com.conf
sudo a2dissite 000-default.conf
ls /etc/apache2/sites-enabled/

# conf-available の有効化(例: security.conf, charset.conf)
sudo a2enconf  security
sudo a2disconf serve-cgi-bin

# 設定反映
sudo systemctl reload apache2
# または
sudo apache2ctl graceful

htpasswd: BASIC 認証

BASIC 認証用のパスワードファイルを作成・更新:

# 初回作成(-c で新規)
sudo htpasswd -c /etc/httpd/.htpasswd admin
# New password: ********

# 2 人目以降は -c を付けない(付けるとファイルを上書きしてしまう)
sudo htpasswd /etc/httpd/.htpasswd alice

# 非対話で(スクリプト用)
sudo htpasswd -b /etc/httpd/.htpasswd bob secretpass

# ユーザー削除
sudo htpasswd -D /etc/httpd/.htpasswd alice

# bcrypt 形式(より強力)
sudo htpasswd -B /etc/httpd/.htpasswd admin

使い方は .htaccess または vhost 設定で:


    AuthType Basic
    AuthName "Admin Area"
    AuthUserFile /etc/httpd/.htpasswd
    Require valid-user

運用フローの典型例

# 1. 設定変更
sudo vi /etc/httpd/conf.d/example.conf

# 2. 構文チェック(必ず!)
sudo apachectl configtest
# Syntax OK

# 3. graceful reload で無停止反映
sudo systemctl reload httpd
# または
sudo apachectl graceful

# 4. 状態確認
sudo systemctl status httpd
sudo journalctl -u httpd -n 50

# 5. アクセスログ確認
sudo tail -f /var/log/httpd/access_log
sudo tail -f /var/log/httpd/error_log

FAQ

Q: restart と graceful の違いは?
A: restart既存接続を切断して即時再起動。graceful既存接続は完了させ、新規接続用のプロセスだけ新設定で起動。本番は基本 graceful。

Q: configtest がパスしたのに起動失敗
A: ポート競合、SSL 鍵の権限、SELinux 等が原因。journalctl -xetail /var/log/httpd/error_log で確認。

Q: Windows での制御コマンドは?
A: httpd -k start | stop | restart | install | uninstall。サービス化されていれば net start Apache2.4 も使えます。

編集
Post Share
子ページ
  1. 起動・停止(Linux)
  2. 起動・停止(macOS)
同階層のページ
  1. インストール方法(Ubuntu)
  2. インストール方法(CentOS)
  3. ディレクトリ構造
  4. httpd.conf(設定ファイル)
  5. ドキュメントルートの変更方法
  6. .htaccess
  7. コマンド一覧
  8. エラー一覧
  9. VPSへの導入例(CentOS編)
  10. SSLの設定
  11. httpd.conf系のバーチャルホストの設定エラー有無確認方法
  12. .htaccessでベーシック認証
  13. configの文法チェック