Apache の対抗馬としてメキメキ名を上げている nginx。パッケージ・インストールすれば簡単だけど、あえてソースからコンパイルして、インストールしてみた。
インストール
最新 1.3.14 版をダウンロードして configure && make && make install。nginx の難しい所は、モジュールのインストールや設定を configure でやってしまう点。必要なモジュールがあれば再インストールが必要になる。まあ、代わりに nginx 自体はシンプルに保てるわけだけど...
デフォールトの設定ファイルが /etc/nginx/ 以下に入らないとか、独特な仕様なので Ubuntu Linux のパッケージから nginx の configure オプション (nginx -V で見られる) を調べて、適当に削って configure を実行した。
$ wget http://nginx.org/download/nginx-1.3.14.tar.gz $ tar xzvf nginx-1.3.14.tar.gz $ cd nginx-1.3.14 $ ./configure --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-client-body-temp-path=/var/lib/nginx/body \ --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \ --http-log-path=/var/log/nginx/access.log \ --http-proxy-temp-path=/var/lib/nginx/proxy \ --http-scgi-temp-path=/var/lib/nginx/scgi \ --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \ --lock-path=/var/lock/nginx.lock \ --pid-path=/var/run/nginx.pid \ --with-pcre-jit \ --with-debug \ --with-http_addition_module \ --with-http_dav_module \ --with-http_geoip_module \ --with-http_gzip_static_module \ --with-http_image_filter_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_sub_module \ --with-http_xslt_module \ --with-ipv6 \ --with-sha1=/usr/include/openssl \ --with-md5=/usr/include/openssl \ --with-mail \ --with-mail_ssl_module $ make $ make install
途中、足りないライブラリー (libgd2-xpm-dev や libgeoip-dev など) を apt-get でインストール。
設定
/etc/nginx/nginx.conf を編集。サンプル:
user www-user;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
一応、ログをインストールしたり後で使う設定ファイルのためにディレクトリーを作成。
$ sudo mkdir -p /var/lib/nginx/body $ sudo mkdir -p /etc/nginx/conf.d/ $ sudo mkdir -p /etc/nginx/sites-enabled $ sudo mkdir -p /etc/nginx/sites-available
myserver.com で開くようにしてみる。/etc/nginx/sites-available 以下に myserver.com というファイルを作る:
server {
listen 8080;
server_name myserver.com;
location / {
root /home/you/www/myserver.com/;
index index.html index.htm;
}
}
Apache が 80 番ポートを使っているので、8080 番ポートでアクセスする様にしてみた。location の中身は ~/www/myserver.com/ 以下に置いた HTML ファイルが myserver.com からアクセスする設定。
続いて sites-enabled にリンクを張る。
$ cd /etc/nginx/sites-enabled $ ln -s ../sites-available/myserver.com ./
myserver.com にアクセスできる様 /etc/hosts もいじっておく (テストの時は意外とハマり所)。下記一行を追記。
127.0.0.1 myserver.com
/etc/init.d/nginx の実行
さあ、nginx を実行してみたい。apache と同じ様に /etc/init.d/nginx を start... と思ったらファイルがない。nginx。このファイルから作らなきゃなのか...
大変なので Ubuntu Linux のパッケージ・インストールしたスクリプトをコピー。ファイルは下記の通り:
/etc/init.d/nginx
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
. /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
test_nginx_config() {
if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
return 0
else
$DAEMON -t $DAEMON_OPTS
return $?
fi
}
case "$1" in
start)
echo -n "Starting $DESC: "
test_nginx_config
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n "$ULIMIT" ]; then
# Set the ulimits
ulimit $ULIMIT
fi
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON || true
sleep 1
test_nginx_config
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n "$ULIMIT" ]; then
# Set the ulimits
ulimit $ULIMIT
fi
start-stop-daemon --start --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
test_nginx_config
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;
configtest|testconfig)
echo -n "Testing $DESC configuration: "
if test_nginx_config; then
echo "$NAME."
else
exit $?
fi
;;
status)
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac
exit 0
実行
これで準備は整った。
$ sudo /etc/init.d/nginx start
ブラウザーで http://myserver.com にアクセスして Nginx のページが出たら成功。
あとがき
Nginx。最初はパッケージ・インストールしたので楽なツールと侮っていたけど、自分で make するとかなり大変。でも、モジュールをインストールする時は再コンパイルが必要というので挑戦してみた。あー、疲れた。
パッケージをアンインストールしてソースからインストールしようとしましたが、うまくいかず困っているところでした。助かりました!
返信削除hirahara さん、はじめまして。
削除Nginx は compile して初めて真価を発揮するところがありますよね。
記事が参考になって良かったです。