LoginSignup
18
20

More than 5 years have passed since last update.

CentOS7.1にNginx+NodeJS(nvm)環境の導入(Vagrant環境下)

Last updated at Posted at 2015-09-05

昨日の記事の環境を元に書きます。
昨日の記事は→ Windows10にVagrant入れて、CentOS7.1入れた話

Nginx入れる

公式リポジトリを追加してインストール。

$ sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
$ sudo yum install -y nginx

NodeJSを入れる

やることはこの自分が書いたリンクのやつです。
NodeJSの導入(CentOS7 - Qiita

このリンクの内容を行うために幾つかパッケージを入れなきゃいけないので入れます。

$ yum install -y git

nvmインストール(詳しい流れはさっきのリンク見てね)

$ git clone git://github.com/creationix/nvm.git ~/.nvm
$ echo . ~/.nvm/nvm.sh >> ~/.bashrc
$ . ~/.bashrc
$ nvm --version
0.26.1

NodeJSのインストール(詳しい流れh(ry

$ nvm install stable
Now using node v0.12.7 (npm v2.11.3)

今回は再起動しても問題ないようにエイリアスを付けておく

$ nvm alias default 0.12.7
$ nvm list
->      v0.12.7
default -> 0.12.7 (-> v0.12.7)
node -> stable (-> v0.12.7) (default)
stable -> 0.12 (-> v0.12.7) (default)
iojs -> iojs- (-> N/A) (default)

Nginxの設定

nginxの設定ですが、socket.ioのサイトの設定方法を参考にします。
http://socket.io/docs/using-multiple-nodes/#nginx-configuration

sudo vi /etc/nginx/conf.d/node.conf
/etc/nginx/conf.d/node.conf
upstream io_nodes {
  ip_hash; # クライアントは同じサーバーに接続されることを保証
  server 127.0.0.1:6001;
}

server {
  listen 3000;
  server_name 192.168.33.10;
  location / {
    # websocketを使えるようにする設定
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    # ロードバランサ用の設定
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;

    proxy_http_version 1.1;
    proxy_pass http://io_nodes;
  }
}

confが間違えてないかシンタックスチェック

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

後は起動

$ sudo nginx

自動起動設定もやっておくか。

$ sudo systemctl enable nginx.service
ln -s '/usr/lib/systemd/system/nginx.service' '/etc/systemd/system/multi-user.target.wants/nginx.service'

起動させるNodeアプリケーションを用意する

とりあえず今回はSocket.ioまでは確認せずに、一旦表示されるまで見ます。
雛形があるexpressを使っちゃいましょう。

$ npm install -g express-generator
$ cd ~
$ express -e ejs hoge
$ cd hoge
$ npm install

起動

$ PORT=6001 NODE_DEBUG=http,net node bin/www

起動したものの、対象のアドレスにブラウザしても動かない。
そうだ、Vagrantfileでip指定してなかった。

$ vim Vagrantfile
# config.vm.network "private_network", ip: "192.168.33.10"
↓
config.vm.network "private_network", ip: "192.168.33.10"

これでreloadし直す。

vagrant reload

そして再度アプリケーションを起動します。

$ PORT=6001 NODE_DEBUG=http,net node bin/www

これで http://192.168.33.10:3000 にアクセスすればExpressの初期表示が出ましたー。

ではではー。

次にやったこと書きましたー
Nginx+pm2(fork)をやってみる。(CentOS7.1+express4)

18
20
0

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
18
20