LoginSignup
17
17

More than 5 years have passed since last update.

osx の nginx を 80 番ポートで launchctl から起動する

Last updated at Posted at 2012-07-04

前提条件

  • homebrew

インストール

% brew install nginx

設定ファイル

nginx を sudo つけて起動するシェルスクリプトを作成する。
(実行権限つけるのを忘れないこと)

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

BIN="/usr/local/sbin/nginx"

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

trap on_die TERM

sudo $BIN $@ &

wait

nginxはdaemon offで起動するので nginx.conf に記述する。
(ここはplistで指定したかったけどうまくいかなかった)
ポートは 80 に設定する

/usr/local/etc/nginx/nginx.conf
(…snip…)
daemon off;
(…snip…)
http {
(…snip…)
  server {
    listen 80;
(…snip…)

plist ファイルは{USER_NAME}を適宜置き換えること

~/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>Label</key>
    <string>homebrew.mxcl.nginx</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>UserName</key>
    <string>{USER_NAME}</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/sbin/nginx_with_sudo</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/usr/local</string>
  </dict>
</plist>

起動確認

% launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

http://localhost:80 にアクセスしてみると nginx が起動していることが確認できる

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