2011年8月23日、加筆・訂正しました。
概要
2011年8月23日現在、Nginxの安定版は1.0.5(2011年7月19日公開)、開発版は1.1.0(2011年8月1日公開)、旧安定版は0.8.55となっています。
開発版とは文字通り現在開発中のバージョンです。新機能やバグフィックスがすぐに適用される一方、新しいバグが増えることもあります。開発版のバグフィックスが安定すると、安定版にバックポートされます。しかし新機能が必ずしもバックポートされるとは限りません。どうしても使いたい新機能がある場合を除いて、通常は安定版を利用します。
依存パッケージのインストール
インストール済みのパッケージをアップデートします
# yum check-update
# yum update
GCCをインストールします
# yum install gcc.x86_64
PCREライブラリ(Perlの正規表現ライブラリ)をインストールします
# yum install pcre.x86_64 pcre-devel.x86_64
zlibライブラリをインストールします
# yum install zlib.x86_64 zlib-devel.x86_64
OpenSSLをインストールします
# yum install openssl.x86_64 openssl-devel.x86_64
最新のソースコードはInstallJa - Nginx Communityからダウンロードできます。
# cd /usr/local/src
# wget http://sysoev.ru/nginx/nginx-1.0.5.tar.gz
2011年12月26日 追記:ダウンロードサイトのURLが変わったようです。
# wget http://nginx.org/download/nginx-1.0.10.tar.gz
Nginxのインストール
ソースコードからconfigure & makeします。デフォルトのprefixは/usr/local/nginxというバージョン番号を含まないパスですが、これだとバージョンアップ時に旧バージョンを上書いてしまうため、/usr/local/nginx-1.0.5というバージョン番号を含むパスに変更しています。
# tar zxvf nginx-1.0.5.tar.gz
# mkdir nginx-1.0.5/modules
# git clone git://github.com/gnosek/nginx-upstream-fair.git nginx-1.0.5/modules/nginx-upstream-fair
# cd nginx-1.0.5
# ./configure --prefix=/usr/local/nginx-1.0.5 \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--with-http_stub_status_module \
--add-module=/usr/local/src/nginx-1.0.5/modules/nginx-upstream-fair
...
configuring additional modules
adding module in /usr/local/src/nginx-1.0.5/modules/nginx-upstream-fair
+ ngx_http_upstream_fair_module was configured
...
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/usr/local/nginx-1.0.5"
nginx binary file: "/usr/local/nginx-1.0.5/sbin/nginx"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/var/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/lib/nginx/body"
nginx http proxy temporary files: "/var/lib/nginx/proxy"
nginx http fastcgi temporary files: "/var/lib/nginx/fastcgi"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
# make
# make install
バイナリファイルは/usr/local/nginx-1.0.5/sbin/nginxに、nginx.confは/usr/local/nginx-1.0.5/conf/nginx.confに格納されます。
/usr/local/nginxディレクトリにカレントバージョンのNginxを格納したディレクトリへのシンボリックリンクを作成します。また、バイナリファイルへのシンボリックリンクをパスが通っているディレクトリに、設定ファイルへのシンボリックリンクを/etcディレクトリに作成します。
# ln -s /usr/local/nginx-1.0.5 /usr/local/nginx
# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
# ln -s /usr/local/nginx/conf /etc/nginx
# nginx -v
nginx: nginx version: nginx/1.0.5
起動ユーザーとconfigureコマンドで指定したディレクトリを作成します。
# useradd -s /sbin/nologin nginx
# mkdir /usr/local/nginx-1.0.5/conf/conf.d
# mkdir -p /var/log/nginx; chown -R nginx:nginx /var/log/nginx
# mkdir -p /var/lib/nginx/body; chown -R nginx:nginx /var/lib/nginx/body
# mkdir -p /var/lib/nginx/proxy; chown -R nginx:nginx /var/lib/nginx/proxy
# mkdir -p /var/lib/nginx/fastcgi; chown -R nginx:nginx /var/lib/nginx/fastcgi
起動スクリプトを作成します。ここではバイナリファイルの場所を/usr/local/nginx/sbin/nginx、設定ファイルの場所を/etc/nginx/nginx.conf、PIDファイルの場所を/var/run/nginx.pidとしています。
FILE: /etc/rc.d/init.d/nginx
. /etc/rc.d/init.d/functions
start(){
/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
}
stop(){
kill `cat /var/run/nginx.pid`
}
restart(){
stop
sleep 1
start
}
reload(){
kill -HUP `cat /var/run/nginx.pid`
}
test(){
/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
test)
test
;;
*)
echo "Usage: $0 {start|stop|restart|reload|test}"
esac
exit 0
# chmod 755 /etc/rc.d/init.d/nginx
ログファイルのローテーション
1日1回ローテートし、60日経過したら削除するように設定します。
FILE: /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
dateext
rotate 60
compress
missingok
ifempty
sharedscripts
postrotate
test ! -f /var/run/nginx.pid || kill -USR1 `cat /var/run/nginx.pid`
endscript
}
ここでは、Nginxが実行中なら、USR1のシグナルをプロセスに通知しています。NginxはUSR1のシグナルを受け取ると、ログファイルの再オープン処理を実施します。
なお、NginxはUSR1のシグナルを受信してもプロセスの終了や再起動を行いません。ログファイルをローテートするには非常に効率的です。Apacheにおけるservice httpd gracefulのように、ローテーションするタイミングで必ず再起動したい場合は、USR1の代わりにHUPを用います。
備考
unknown directive "stub_status"
設定ファイルの中でstub_statusディレクティブを使う場合、configureコマンドに--with-http_stub_status_moduleを渡す必要があります。これを忘れると、unknown directive "stub_status"と怒られます。初めてインストールしたとき、私もこのメッセージに半日ほど悩みました。
# /usr/local/nginx/sbin/nginx -t
[emerg]: unknown directive "stub_status" in /usr/local/nginx/conf/nginx.conf:91
configuration file /usr/local/nginx/conf/nginx.conf test failed
yumを使ったインストール
configureコマンドのオプションに悩みたくなければ、yumからインストールするといいかもしれません。この場合、最初からstub_statusディレクティブも有効になっています。
# yum info nginx
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
Available Packages
Name : nginx
Arch : x86_64
Version : 0.7.67
Release : 1.0.amzn1
Size : 354 k
Repo : amzn
Summary : Robust, small and high performance HTTP and reverse proxy server
URL : http://nginx.net/
License : BSD
Description: Nginx [engine x] is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3
: proxy server written by Igor Sysoev.
# yum install nginx
バイナリファイルは/usr/sbin/nginxディレクトリに、設定ファイルは/etc/nginx/nginx.confディレクトリに格納されます。