hnakamur’s blog

ものすごい勢いで忘れる私のために未整理でもいいからとりあえずメモ

2010-09-17

CentOS5.5でnginx

参考:http://serverfault.com/questions/114895/why-is-nginx-more-popular-than-lighttpd
nginxは軽量級のウェブサーバーで、ロードバランサーとしてもリバースプロキシーとしても使えるそうです。


http://articles.slicehost.com/2008/12/17/centos-installing-nginx-via-yum
によるとEPELにもパッケージがあるそうなのですが、
http://download.fedora.redhat.com/pub/epel/5/x86_64/repoview/nginx.html
バージョン0.6.39と古いのでnginxホームページからソースを落としてインストールすることにします。

NginxHttpSslModuleによるとSSLを使うにはconfigureで--with-http_ssl_moduleオプションを指定しておく必要があります。

$ wget http://sysoev.ru/nginx/nginx-0.8.50.tar.gz
$ sudo yum install openssl-devel pcre-devel
$ tar zxf nginx-0.8.50.tar.gz
$ cd nginx-0.8.50
$ ./configure --with-http_ssl_module
$ make
$ sudo /usr/local/sbin/checkinstall
Requiresにopenssl, pcreを追加
$ sudo rpm -i /usr/src/redhat/RPMS/x86_64/nginx-0.8.50-1.x86_64.rpm

http://articles.slicehost.com/2009/2/20/centos-nginx-configuration
http://503.mydns.jp/2010/06/nginx/
を参考にしつつ設定。

iptables設定

# /sbin/iptables -A RH-Firewall-1-INPUT -p tcp -m tcp --dport 80 -j ACCEPT
# /sbin/iptables -A RH-Firewall-1-INPUT -p tcp -m tcp --dport 443 -j ACCEPT
# /etc/init.d/iptables save
# /etc/init.d/iptables restart

グループとユーザ作成

# /usr/sbin/groupadd -g 201 nginx
# /usr/sbin/useradd -u 201 -g nginx -d / -s /sbin/nologin nginx

# mkdir /usr/local/nginx/conf.d
# mkdir /var/log/nginx
# vi /usr/local/nginx/conf/nginx.conf

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /usr/local/nginx/conf/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    include /usr/local/nginx/conf.d/*.conf;

    server {
        listen 80;
        server_name sunshine12;
        location / {
            root /usr/local/nginx/html;
            index index.html index.htm;
        }
    }
    server {
        listen 443 ssl;
        ssl_certificate /usr/local/nginx/conf/server.crt;
        ssl_certificate_key /usr/local/nginx/conf/server.key;
        server_name sunshine12;
        location / {
            root /usr/local/nginx/html;
            index index.html index.htm;
        }
    }
}

# vi /etc/init.d/nginx

#!/bin/bash
#
# nginx        Startup script for the nginx Web Server
#
# chkconfig: - 85 15
# description: nginx is a World Wide Web server.  It is used to serve \
#              HTML files and CGI.
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Path
nginx='/usr/local/nginx/sbin/nginx'
prog=nginx
RETVAL=0

start() {
        echo -n $"Starting $prog: "
        daemon $nginx
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc $nginx
        RETVAL=$?
        echo
        return $RETVAL
}

reload() {
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  reload)
        reload
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|reload}"
        exit 1
        ;;
esac

exit $RETVAL


# chmod +x /etc/init.d/nginx

CentOSでcheckinstallを使う

$ sudo yum install gcc make gettext rpm-build
$ wget http://asic-linux.com.mx/~izto/checkinstall/files/source/checkinstall-1.6.2.tar.gz

$ tar zxf checkinstall-1.6.2.tar.gz
$ cd checkinstall-1.6.2
$ make
$ sudo make install
$ sudo ln -s /usr/local/lib/installwatch.so /usr/local/lib64/installwatch.so
$ sudo /usr/local/sbin/checkinstall

checkinstall 1.6.2, Copyright 2009 Felipe Eduardo Sanchez Diaz Duran
           This software is released under the GNU GPL.

The checkinstallrc file was not found at:
/usr/local/sbin/../checkinstallrc

Assuming default values.

The package documentation directory ./doc-pak does not exist.
Should I create a default set of package docs?  [y]: y

Preparing package documentation...OK

Please choose the packaging method you want to use.
Slackware [S], RPM [R] or Debian [D]? R
...
$ sudo rpm -i /usr/src/redhat/RPMS/x86_64/checkinstall-1.6.2-1.x86_64.rpm

パッケージとしてインストールされたことを確認

$ rpm -qi checkinstall

参考:http://d.hatena.ne.jp/massat/20100817/1282044839

CentOSでselinuxをオフにする

# sed -i -e '/^SELINUX=/s/=.*/=disabled/' /etc/selinux/config
# reboot

ブログ アーカイブ