2.

macOS Apache起動・停止・再起動|apachectlと標準同梱・Homebrew版

編集
この記事の要点
  • macOS で Apache(httpd)の起動・停止・再起動sudo apachectl コマンドで操作する
  • 起動: sudo apachectl start / 停止: sudo apachectl stop / 再起動: sudo apachectl restart
  • 設定再読み込み(無停止)は sudo apachectl graceful、設定ファイルの構文チェックは sudo apachectl configtest
  • macOS 標準同梱の Apache とは別に、Homebrew で入れた httpd もある。両者はパスと設定が別物
  • 再起動時の自動起動には launchctl または Homebrew の brew services を使う

macOS の Apache

macOS には標準で Apache HTTP Server(httpd)が同梱されており、開発用 Web サーバとして使えます。本ページでは macOS で Apache を起動・停止・再起動する基本コマンドと、よく使う運用コマンドをまとめます。

基本コマンド一覧

操作コマンド
起動sudo apachectl start
停止sudo apachectl stop
再起動(プロセス停止 → 起動)sudo apachectl restart
設定再読み込み(無停止 graceful)sudo apachectl graceful
graceful 停止(処理中リクエスト完了後)sudo apachectl graceful-stop
設定ファイル構文チェックsudo apachectl configtest / apachectl -t
状態確認sudo apachectl status
バージョン確認httpd -v
ロードされているモジュールapachectl -M

起動

sudo apachectl start

起動後、ブラウザで http://localhost/ にアクセスすると Apache のデフォルトページ(または /Library/WebServer/Documents/index.html の内容)が表示されます。

停止

sudo apachectl stop

再起動

sudo apachectl restart

restartプロセスを停止してから起動します。本番運用や開発中の接続を切りたくない場合は graceful を使います。

graceful(無停止リロード)

sudo apachectl graceful          # 設定を再読み込み(既存接続は維持)
sudo apachectl graceful-stop     # 既存接続完了後に停止

設定ファイル(httpd.conf)を編集した後、接続を切らずに反映したい場合は graceful が安全です。

configtest(構文チェック)

設定変更後は必ず構文チェックしてから restart / graceful するのが鉄則です。誤った設定で再起動すると、Apache が立ち上がらなくなることがあります。

sudo apachectl configtest
# Syntax OK   ← この表示が出れば OK

# 同じ意味
sudo apachectl -t

標準同梱 Apache と Homebrew 版の違い

macOS には標準同梱の Apacheと、別途 Homebrew でインストールできる httpd があります。両者は完全に別物で、パスと設定ファイルが異なります。混在すると混乱の元なので、用途を決めて使い分けます。

項目標準同梱Homebrew 版
実行ファイル/usr/sbin/httpd/opt/homebrew/bin/httpd(Apple Silicon)
/usr/local/bin/httpd(Intel)
apachectl/usr/sbin/apachectl/opt/homebrew/bin/apachectl
設定ファイル/etc/apache2/httpd.conf/opt/homebrew/etc/httpd/httpd.conf
ドキュメントルート/Library/WebServer/Documents/opt/homebrew/var/www
ログ/private/var/log/apache2//opt/homebrew/var/log/httpd/
デフォルトポート808080(インストール直後)
起動方法sudo apachectl startbrew services start httpd

Homebrew 版の起動・停止

brew install httpd

brew services start  httpd     # 起動 + 自動起動登録
brew services stop   httpd     # 停止 + 自動起動解除
brew services restart httpd    # 再起動
brew services list             # 状態確認

自動起動の制御(launchctl)

macOS は launchd でサービスを管理しています。OS 再起動時に自動で Apache を立ち上げたい場合は launchctl を使います。

# 自動起動を有効化(macOS の plist は既に同梱)
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

# 自動起動を無効化
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

macOS Catalina 以降は /System/Library が読み取り専用となり、上記コマンドが動作しないバージョンもあります。その場合は Homebrew 版 + brew services の方が確実です。

状態確認とトラブルシュート

プロセス確認

ps aux | grep httpd
# httpd プロセスが複数立ち上がっていれば動作中

sudo lsof -i :80
# 80 番ポートを listen しているプロセスを確認

ログ確認

# 標準同梱版
sudo tail -f /private/var/log/apache2/error_log
sudo tail -f /private/var/log/apache2/access_log

# Homebrew 版
tail -f /opt/homebrew/var/log/httpd/error_log
tail -f /opt/homebrew/var/log/httpd/access_log

よくあるエラー

症状原因対処
Address already in use: AH0007280 番が他プロセス(nginx 等)に占有sudo lsof -i :80 で犯人特定、停止
(13)Permission denied1024 未満のポートに非 root で bind しようとしたsudo を付ける、または 8080 等を使う
Syntax error on line N of /etc/apache2/httpd.conf設定ファイル誤りsudo apachectl configtest でエラー位置を確認
403 Forbiddenドキュメントルートのパーミッションchmod -R 755 で読み取り権付与
localhost につながらないApache が止まっている / ファイアウォールsudo apachectl start、システム設定 → ファイアウォール

FAQ

Q: macOS のどのバージョンから Apache の標準同梱は無くなった?
A: macOS Monterey (12)・Ventura (13)・Sonoma (14) まで /usr/sbin/httpd は同梱されています。将来的には削除される可能性があり、Apple は公式に「開発用」と位置付けています。本格運用は Homebrew 版か Docker 推奨。

Q: 80 番ポートを使うと毎回 sudo が必要
A: macOS では 1024 未満のポートに bind するには root 権限が必要です。開発用に 8080 / 8000 などを使えば sudo 不要にできます。

Q: graceful と restart の違いは?
A: restart は親プロセスごと再起動し既存接続も切れます。graceful は子プロセスを順次入れ替え、既存接続は完了まで維持します。本番では graceful が推奨。

関連

  • コマンド一覧 — 親カテゴリ
  • macOS — 上位カテゴリ
  • Apache(httpd) — サーバ本体
  • httpd.conf / launchctl / brew services
編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. 起動・停止(Linux)
  2. 起動・停止

最近更新/作成されたページ