LoginSignup
6
6

More than 5 years have passed since last update.

virtualbox(centos)でnginxをwebsocketを使用出来る状態でインストールする方法メモ

Posted at

node.jsでwebsocketを使用する場合nginx_tcp_proxy_moduleというモジュールが必要らしいが入れてなかった。
しかもモジュールの追加の場合は再コンパイルをしなくてはならない。
ガーーーン。
ということで一旦yumで入れたnginxを一旦アンインストールしてコンパイルして入れ直すことにした。

注意事項

  • ここから先は全てroo(権限)ユーザーで実行しています
  • 要git
  • 開発環境構築の話です。本番環境などはもっと詳しいブログをみつけてください。

参考にしたサイト

nginxのソースを取ってくる

取ってくる

/usr/local
cd /usr/local/
wget http://nginx.org/download/nginx-1.3.13.tar.gz
tar zxvf nginx-1.3.13.tar.gz
cd /usr/local/nginx-1.3.13

nginxのモジュールを追加する

便利なものが色々あるので追加する

/usr/local/nginx-1.3.13
cd /usr/local/nginx-1.3.13
git clone https://github.com/yaoweibin/nginx_tcp_proxy_module.git
git clone https://github.com/agentzh/headers-more-nginx-module.git
git clone https://github.com/FRiCKLE/ngx_cache_purge.git

nginx_tcp_proxy_moduleのパッチを当てる

このモジュールがないとwebsocket使えないです。
パッチ当てるって初めてやったのですがコマンド打つだけでした。

/usr/local/nginx-1.3.13
patch -p1 < nginx_tcp_proxy_module/tcp.patch 

コンパイルする際に必要なモジュールの追加

足らなければ入れましょう。

yum install -y patch
yum install -y pcre-devel
yum install -y zlib-devel openssl-devel

configureからのmake,make install

いつもどおりで

/usr/local/nginx-1.3.13
./configure --prefix='/usr/local/nginx' --with-http_ssl_module --with-http_stub_status_module --with-http_xslt_module --with-http_realip_module --with-http_gzip_static_module --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 --with-debug --add-module=./headers-more-nginx-module --add-module=./ngx_cache_purge --add-module=./nginx_tcp_proxy_module
/usr/local/nginx-1.3.13
make
make install

起動コマンドの作成

nginx 1.0.10をCentOSでインストール(WebSocketのリバースプロキシ付き)を参照しました

/etc/init.d/nginx
vim /etc/init.d/nginx


#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /var/run/nginx.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

#nginx="/usr/local/sbin/nginx"
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

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

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

confの設定を変更する

コメントアウト色々消去しちゃいました。ローカル環境なので最低限の構成で

/usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

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

    keepalive_timeout  65;

    #ここからの設定がapacheでいうvirtualhostの設定の項目になります。
    #rootがapacheでいうドキュメントルート
    #server_nameに/etc/hostsで書いたvirtulahostと解釈して変更してください。

    #lo.test.org server directive
    server {
        listen       80;
        server_name  lo.test.org;
        location / {
            root   /home/web/lo.test.org/www;
            index  index.html index.htm;
        }

        location = /50x.html {
            root   /usr/share/nginx/html;
        }


        #nginxでphpを使いたい場合の設定です。fastcgi_passはphp-fpmを起動しているポート番号を指定してください。(デフォルトは9000で起動している)
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/web/lo.test.org/www$fastcgi_script_name;
            include        fastcgi_params;
        }
    }


    #lo.sample.org server directive
    server {
        listen       80;
        server_name  lo.sample.org;
        root   /home/web/lo.sample.org/www;
        index  index.php index.html index.htm;
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/web/lo.sample.org/www$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

nginxの起動確認

書いた起動スクリプトを実行するだけ

/etc/init.d/nginx start
nginx を起動中:                                            [  OK  ]

起動時に自動でnginxが起動するようにする

chkconfigに設定を追加しましょう

chkconfig --add nginx
chkconfig nginx on

以上。パッチとかはじめ面倒そうってイメージでしたが以外とすんなりいけたのでよかった。

6
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6