yamamotototo のプロフィール画像@yamamotototo

投稿日
2022/12/16

HomebrewでApacheインストールしたら全く起動しなかった話

はじめに

以前、M1 MacbookにてHomebrewを使い、apacheをインストールしたのですが、起動してもapacheの初期画面が表示できませんでした。

環境・バージョン等

macOS Monetery 12.2.1
HomeBrew 3.6.11
Apache 2.4.54
PHP 7.3.33

ここまでやったこと

コマンドだけ見ると起動しているように見える

#問題なく起動した?
% brew services start httpd
==> Successfully started `httpd` (label: homebrew.mxcl.httpd)

でも、apacheの初期画面は出なかったので実行しているはずのプロセスを確認してみる

#プロセスを確認
% ps -aux | grep httpd  #プロセスを確認
501 57466 35275   0  4:47PM ttys002    0:00.00 grep httpd

動いてないじゃん!笑

homebrewで設定したhttpd(apache)を一覧で出してみる

% brew services list
Name  Status   User File
httpd error    256 root ~/Library/LaunchAgenbash/homebrew.mxcl.httpd.plist

statusがエラーになってることが発覚
ということは実行時にエラーログが出ているかもしれない!

#エラーログを確認してみる
% vi /opt/homebrew/var/log/httpd/error_log

(エラーログは何も出ていない。。。。)

もう一度apacheを起動してみるとさらにエラー発生(汗)

% brew services start httpd
Boobashtrap failed: 5: Input/output error
Try re-running the command as root for richer errors.
Error: Failure while executing; `/bin/launchctl boobashtrap gui/501 /Users/<user-name>/Library/LaunchAgenbash/homebrew.mxcl.httpd.plist` exited with 5.

解決策

どうやらmacOS12 MontereyではPHPが標準バンドルから除外されたみたいです。
HomebrewのPHPが標準バンドルApacheと相性が悪いらしい!
Twitter_ICHIKEN@🇺🇸→🇯🇵エンジニアキャンパー🏕
やることは2つ

・mac標準Apacheを消してHomebrew PHPとHomebrew Apacheにする

・Homebrewで入れたApacheのパスがネットに多くある情報と異なるので設定変更
(どうやらApple SiliconとIntelでHomebrewのパスが違う)

①一度apacheをstop

% brew services stop httpd

②「homebrew.mxcl.httpd.plist」ファイルのPATHをviで修正する

#httpdのバージョンは人による
% cd /opt/homebrew//Cellar/httpd/2.4.54_1
% ls
CHANGES                         README                          include
INSTALL_RECEIPT.json            bin                             lib
LICENSE                         homebrew.httpd.service          share
NOTICE                          homebrew.mxcl.httpd.plist
% vi homebrew.mxcl.httpd.plist

homebrew.mxcl.httpd.plistファイルのPATH内を変更する
※それ以外はデフォルトで問題ない

<!-- 変更前 -->
<dict>
  <key>PATH</key>
  <string>/opt/homebrew/bin:/opt/homebrew/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>

<!-- 変更後 -->
<dict>
  <key>PATH</key>
  <string>/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin</string>
</dict>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>EnvironmentVariables</key>
	<dict>
		<key>PATH</key>
		<string>/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin</string> #ここの一箇所のみ
	</dict>
	...............
  ........
  .....
  ...
  ..
  .
</dict>
</plist>

③停止したhttpdを起動

% brew service start httpd

④brew servise listでhomebrewで設定したhttpdを再確認

% brew services list
Name  Status  User File
httpd started root ~/Library/LaunchAgenbash/homebrew.mxcl.httpd.plist

Status:startedに!
念のためプロセスも確認

% ps -aux | grep httpd
  501 63049     1   0  5:28PM ??         0:00.06 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 63056 63049   0  5:28PM ??         0:00.00 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 63057 63049   0  5:28PM ??         0:00.00 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 63058 63049   0  5:28PM ??         0:00.00 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 63059 63049   0  5:28PM ??         0:00.00 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 63060 63049   0  5:28PM ??         0:00.00 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 61500 35275   0  5:21PM ttys002    0:00.02 vi homebrew.mxcl.httpd.plist
  501 61695 35275   0  5:21PM ttys002    0:00.06 vi homebrew.mxcl.httpd.plist
  501 63327 35275   0  5:31PM ttys002    0:00.00 grep httpd

動いたぁ!

おわりに

調べていて感じたのは、新しいmacバージョンでのエラーのため日本語での説明が少なく、解決までかなり時間を使いました。
新しいエラーについてはTwitterで発信しているエンジニアさんの情報から調べるきっかけを見つけるのも大事だなと思いました!
それでは、また( ̄^ ̄)ゞ

0