MacPortsを使ってnginxをインストールする

Linodeでは、メモリ消費量を極力抑えたいので、Apacheではなく、nginx(エンジンエックス)を使ってみようと思って、手元のMacにインストールしてみました。

nginxについては、公式サイトを参照して下さい。

上記サイトからのnginxについての説明を抜粋しておきます。

nginxは無料で利用できるオープンソースのハイパフォーマンスHTTPサーバ且つリバースプロキシで、 
IMAP/POP3のプロキシサーバとしても動作します。

ってことで、早速インストール。

Macでは、MacPortsを使うことで簡単にインストールすることができます。

$ sudo port install nginx

インストール完了時に自動起動の方法を教えてくれます。具体的には以下のコマンドを実行します。

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist

また各種設定ファイルは、/opt/local/etc/nginx/以下にあるので、それをベースに編集した作成します。

$ ls -alF /opt/local/etc/nginx/
total 48
drwxr-xr-x   8 root  admin   272  3 27 22:08 ./
drwxr-xr-x  14 root  admin   476  3 27 22:08 ../
-rw-r--r--   2 root  admin   909  3 27 22:08 fastcgi_params.example
-rw-r--r--   2 root  admin  2837  3 27 22:08 koi-utf
-rw-r--r--   2 root  admin  2223  3 27 22:08 koi-win
-rw-r--r--   2 root  admin  3128  3 27 22:08 mime.types.example
-rw-r--r--   2 root  admin  2786  3 27 22:08 nginx.conf.example
-rw-r--r--   2 root  admin  3610  3 27 22:08 win-utf

nginxのオプションを確認しておきます。

$ nginx -h
nginx version: nginx/0.7.64
Usage: nginx [-?hvVt] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /opt/local/)
  -c filename   : set configuration file (default: /opt/local/etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

基本的な起動と停止方法はこんな感じみたいです。

$ sudo nginx
$ sudo nginx -s stop

実際の起動/停止は、以下のように設定ファイルを指定することになります。パスは、絶対パスで指定する必要があります。指定しないと、prefixパスからの相対パスになってしまうので注意が必要です。

$ sudo nginx -c /path/to/nginx.conf
$ sudo nginx -c /path/to/nginx.conf -s stop

設定ファイル(nginx.conf)の記述方法は、Apacheとは全然異なるようなので、色々とドキュメントを確認しないとダメですね。

補足

設定ファイル(nginx.conf)をサンプルをコピーすると、 /opt/local/var/log/nginx/ 以下にログが出力されます。通常のアカウントで起動すると、パーミッションエラーが発生するため、rootアカウントで起動しています(手抜き)。

実際には、こんな感じの構成でアプリケーションを作ろうと思っているので、連携が必要です。

nginx(80,443) -> thin(3000-300?) -> rails