LoginSignup
34
34

More than 5 years have passed since last update.

Mac nginx をPort:80で起動させる

Last updated at Posted at 2013-01-19

brew install nginx の一部ログ:

In the interest of allowing you to run `nginx` without `sudo`, the default
port is set to localhost:8080.

If you want to host pages on your local machine to the public, you should
change that to localhost:80, and run `sudo nginx`. You'll need to turn off
any other web servers running port 80, of course.

概要

Home brew からインストールしたNginx の待ち受けポートを、デフォルトの8080番から80番に変更、システム起動時に待ち受けさせる。

おおまかな手順としては
1. nginx.conf の編集
2. nginx をsudo で起動するシェルスクリプトを書く
3. nginx plist の編集
4. 自動起動できるように、sudo パスワード入力をスキップ

注意事項として、上記手順4にてroot権限が必要になるため、rootとして作業できることが必要。あと、Mac標準のhttpdさんは眠っている前提です。


80番ポートで待たせるためにはsudo が必要らしい。

8080番はJenkins やその他アプリケーションが利用するかもしれない。sudoer 権限で実行できるように設定する。

nginx.conf の編集

/usr/local/etc/nginx/nginx.conf
http { …
    server {
-       listen  8080;
+       listen 80;
        server_name

        #charset koi8-r;
        …
}

シェルスクリプトの作成

/usr/local/bin/nginx_with_sudo
#!/bin/bash

NGINX_BIN="/usr/local/sbin/nginx"

function on_die() {
    sudo $NGINX_BIN -s stop
    exit 0
}

trap on_die TERM

sudo $NGINX_BIN $@ &

wait

chmod +x /usr/local/bin/nginx_with_sudo で実行権限を付与しておきましょう。

plist の編集

~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
<?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>KeepAlive</key>
        <false/>
+       <key>UserName</key>
+       <string>USER_NAME</string>
        <key>ProgramArguments</key>
-       <array>
-           <string>/usr/local/opt/nginx/sbin/nginx/
-           <string>-g</string>
-           <string>daemon off;</string>
-       </array>
+       <array>
+           <string>/usr/local/bin/nginx_with_sudo</string>
+       </array>
        …
    </dict>
</plist>

sudoersファイルの編集

root としてログインsudo suする。
visudoコマンドを叩いてsudoersファイルの編集を開始。

Nginxとスクリプトをsudoで実行した時のパスワード入力をスキップさせます。

Cmnd_Alias NGINX=/usr/local/sbin/nginx
Cmnd_Alias NGINX_SCRIPT=/usr/local/bin/nginx_with_sudo

Your_User_Name  ALL=NOPASSWD: NGINX, NGINX_SCRIPT

NOPASSWD: ALL なんかも指定出来てお手軽な感じがしますが、それは穴が大きすぎるとおもいますので、個別に指定して行きましょう。

最後に、作成したスクリプトを起動するか、再起動すればnginx が80番ポートで起動してくれるはずです。

http://localhost:80/ で確認してみましょう。

リンク


34
34
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
34
34