0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【macOS】Homebrewで入れたNginxが"error 1 root"になるときの対処法

Posted at

Nginxでローカルサーバーを立ち上げようとした時に詰まったので備忘録

結論、sudoでNginxをスタートさせると、少し手を入れないと一般ユーザーでうまく立ち上がらなくなるということがわかりました。

理由としては、brew services は LaunchAgent をユーザーレベルで管理する前提ですが、sudo で起動してしまうと LaunchDaemon(システムレベル)に登録されてしまい、一般ユーザーでは解除・制御できなくなるためです。

今回の問題の根源は最初に管理者権限でNginxをスタートさせたことでした。

sudo brew services start

以降、一般ユーザーでスタートさせても、

brew services list

で確認すると

nginx   error  1 root ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

となっていました。

試したこと

brew services stop nginx
rm -f ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

試していませんが、以下のように権限を変更しても戻せたかもしれません。

sudo chown -R $(whoami):admin /opt/homebrew/opt/nginx
sudo chown -R $(whoami):admin /opt/homebrew/Cellar/nginx
sudo chown -R $(whoami):admin /opt/homebrew/var/homebrew/linked/nginx

なお、whoamiは自身のユーザー名

さらに、こんな警告が出てくるので、macOSバックグラウンド項目からも削除しておきます。
「システム設定」→「一般」→「ログイン項目」

image.png

手動でrootが所有しているサービス定義ファイルを削除する(LaunchDaemonからrootサービスを削除する)

sudo rm /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

これで、一般ユーザーで立ち上げることができました。

すると、次の問題としてログファイルにアクセス権限がないと言われました。

open() "/opt/homebrew/var/log/nginx/access.log" failed (13: Permission denied)
sudo chown -R $(whoami):admin /opt/homebrew/var/log/nginx

無事起動

nginx started user1 ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

一般ユーザーで作業したい場合になんとなくsudo使うと、Deamon消したりする作業が発生するので気をつけたい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?