Nginxインストール

さくらのVPS(Debian 6.0)にNginxをインストール。

ユーザ作成

groupadd -g 2001 nginx
useradd -u 2001 -g nginx -s `which nologin` -d /usr/local/nginx nginx

必要なパッケージのインストール

CentOSの場合はgcc,make,pcre,pcre-devel,zlib,zlib-devel,openssl-develが必要。

apt-get update
apt-get -y install gcc make libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev

ソースダウンロード

cd /usr/local/src
wget -c http://nginx.org/download/nginx-1.0.12.tar.gz

インストール

prefixはデフォルトと同じだけど指定しないと気持ち悪いので。

tar zxvf nginx-1.0.12.tar.gz
cd nginx-1.0.12
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make && make install

ディレクトリ作成

mkdir /usr/local/nginx/conf/conf.d
mkdir /usr/local/nginx/vhosts

設定ファイル

vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 3;
worker_priority 0;
worker_rlimit_nofile 8192;

error_log  logs/error.log  notice;
pid logs/nginx.pid;

events {
    multi_accept off;
    worker_connections  1024;
    use epoll;
}

http {
    include mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile on;
    server_tokens off;
    keepalive_timeout 10;
    index index.py index.wsgi index.php index.pl index.cgi index.html;
    error_page 500 502 503 504 /50x.html;

    server {
        listen 80 default;
        server_name _;
        root html;
        access_log logs/access.log main;

        location = /nginx_status {
                stub_status on;
                access_log off;
                allow 127.0.0.1;
                deny all;
        }
    }

    include conf.d/*.conf;
    #include proxy.conf;
}

起動スクリプト

touch /etc/init.d/nginx
chmod 755 /etc/init.d/nginx
vi /etc/init.d/nginx
#!/bin/bash
 
# Nginx start stop script
#
# Debian
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start and stop Nginx daemon
# Description:       start and stop Nginx daemon
### END INIT INFO
#
# CentOS
# chkconfig: 345 99 1
# description: Nginx start stop script
# processname: nginx
 
SERVER_ROOT=/usr/local/nginx
NGINX=$SERVER_ROOT/sbin/nginx
PIDFILE=$SERVER_ROOT/logs/nginx.pid
 
[ -x $NGINX ] || exit 1
 
if ! $NGINX -t > /dev/null 2>&1 ; then
    echo "Syntax error! Please confirm the config file."
    exit 1
fi
 
do_start() {
    if [ -f $PIDFILE ] ; then
        if ps -p `cat $PIDFILE` > /dev/null 2>&1 ; then
            echo "Nginx is already running..."
            exit 1
        fi
    fi
    $NGINX || echo "Failed to start Nginx."
}
 
do_stop() {
    if [ ! -f $PIDFILE ] ; then
        echo "Nginx is not running."
        exit 1
    fi
    if ps -p `cat $PIDFILE` > /dev/null 2>&1 ; then
        kill -QUIT `cat $PIDFILE` || echo "Failed to stop Nginx."
    else
        echo "Nginx is not running."
        exit 1
    fi
}
 
do_graceful() {
    if [ ! -f $PIDFILE ] ; then
        echo "Nginx is not running."
        exit 1
    fi
    if ps -p `cat $PIDFILE` > /dev/null 2>&1 ; then
        kill -HUP `cat $PIDFILE` || echo "Failed to graceful Nginx."
    else
        echo "Nginx is not running."
        exit 1
    fi
}
 
case $1 in
    start)
        do_start;;
    stop)
        do_stop;;
    restart)
        do_stop
        sleep 2
        do_start;;
    graceful)
        do_graceful;;
    *)
        echo "Usage: nginx [start|stop|restart|graceful]"
        exit 1;;
esac
 
exit 0

Nginx起動と自動起動設定

/usr/local/nginx/sbin/nginx -t
/etc/init.d/nginx start
update-rc.d nginx defaults