LoginSignup
5
4

More than 5 years have passed since last update.

UbuntuでNode.jsをポート80で動かす

Last updated at Posted at 2017-12-30

前提条件

  • Ubuntu16.04
  • Node.jsとnpmは既に入っている

本文

スーパーユーザーで起動させれば動きます。
が、気持ち悪いのでNginxでプロキシしませう。:v:

↓こちらのすばらしい記事を参考に最新版のNginxをインストールしてくださまし。
Ubuntuに最新のnginxをインストールする

そしたらば、Nginxのconfファイルをつくるーー
/etc/nginx/conf.dにあるconfファイルは/etc/nginxnginx.confの中でinclude /etc/nginx/conf.d/*.conf;みたいな感じでインクルードされるのです。詳しくはぐぐってください〜〜(https対応するときはちょっと弄らないとけないですーーー:innocent:

$ cd /etc/nginx/conf.d
$ sudo vi node-app.conf # 内容は下にあるよ!
$ sudo mv default.conf default.conf.origin

Node.jsのアプリへプロキシするための設定node-app.confファイルここんな感じでつくっって、最初からあるdefault.confdefault.conf.origin名前を変えて一応置いておきます。(念のためね!:yum:

node-app.conf
upstream node-app {
    server localhost:8080;
}

server {
    listen       80;
    server_name  example.com;
    proxy_redirect                          off;
    proxy_set_header Host                   $host;
    proxy_set_header X-Real-IP              $remote_addr;
    proxy_set_header X-Forwarded-Host       $host;
    proxy_set_header X-Forwarded-Server     $host;
    proxy_set_header X-Forwarded-For        $proxy_add_x_forwarded_for;
    location / {
        proxy_pass http://node-app/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   /usr/share/nginx/html;
    }
}

そしたらNginxを再起動しますーーーー

$ sudo nginx -s reload

はい、これでNode.jsで作ったアプリをポート8080で動かせば80で動きますーーーー!!!
完璧です!!:laughing:
ufwでポート制限している人は忘れず80だけ開けておきましょう!8080は開けなくても大丈夫です!:sparkles:

$ sudo ufw allow 80

おまけ

はい!でもNode.jsのアプリはプロセスなので死ぬかもしれないので一応デーモン化しておきましょーー。foreverを使います!インストールは以下!

$ sudo npm install -g forever

そうですNode.jsでNode.jsをデーモン化するツールです!なんか気持ち悪いですね!
でもそんなの関係ない!便利なので使っちゃえーーーっ!!:lollipop:

$ forever start app.js

こんな感じで起動させましょー!環境変数も問題なく起動時につっこめます!

$ HOGE=hage forever start app.js

以上ですおわりです!!!!!!!かんたんーーー:smile::beer::beer:
(備忘録です)

参考

NginxからNode.jsへリバースプロキシする

5
4
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
5
4