チャットを作りたい② nginxとNode.jsの共存

Node.js

まずNode.jsを使えるようにしてこー。

Node.jsのインストール

https://github.com/nodesource/distributions/blob/master/README.md

$ curl -sL https://rpm.nodesource.com/setup_14.x | bash -

$ sudo yum install -y nodejs

された。

$ node -v
v14.15.1

現時点のLTS、LTSって何?Long Term Supportの略で長期サポート版ってことらしい。が、インストールされた。

$ npm -v
6.14.8

npmも同時にインストールされる。

Hello World

https://nodejs.org/ja/about/

にあるサンプルをそのままやってみる。いつも思うんですけど、こういうのってどのフォルダに置くのが一番いいんですかね。まぁなんでもいいかと

$ mkdir /home/dalomo/nodejs

した。そんでこん中に

$ vi helloworld.js

そんで

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

まるぱくる。とりあえず起動

$ node helloworld.js

そんで!http://dalomo.net:3000/にアクセスしても、反応なし。hostnameとportがちがうんかなーというのと、まずはnginxとhttpdを止める。

$ systemctl stop httpd 
$ systemctl stop nginx
  • hostname = ‘127.0.0.1’ port = 3000 アクセス先 = http://dalomo.net:3000
    • 動かない
  • hostname = ‘サーバーのIP’ port = 3000 アクセス先 = http://dalomo.net:3000
    • 動かない
  • hostname = ‘サーバーのIP’ port = 80 アクセス先 = http://サーバーのIP:80
    • 動く!
  • hostname = ‘dalomo.net’ port = 80 アクセス先 = http://dalomo.net:80
    • 動かない

結局動いたのこれだけだ。知識が足りなすぎて、どういう背景でこうなってるのか予想もつかん。多分だけどポートが開いてないとかなのかな…。試すのめんどくさいので取り合えず動いたやつに合わせてやってこう。

Node.js + forever

現状ctrl+Cしないと何もできなくなるし、その状態だとnginxと共存っつっても何もできんので、foreverでデーモン化してみる。って思ったんだけど、なんかCentOS7以降だとOSの機能としてデーモン化・永続化の機能があるみたい。そっちにしよう!

systemdでNode.jsアプリをデーモン化

デーモン化・永続化・バックグラウンドプロセス・常時起動とか色んな呼び方あるみたいだけど、何が正しいのか分からない!色んなサイトを参考にやってみよう。

https://qiita.com/you21979@github/items/588bddb59378ce7303a2

https://blog.kazu69.net/2016/06/06/start-node-server-with-systemd/

http://var.blog.jp/archives/83319334.html

https://nodesource.com/blog/running-your-node-js-app-with-systemd-part-1

https://qiita.com/ukiuni@github/items/400e4fdaae14b9bc0fcf

https://baykara.medium.com/how-to-daemonize-a-process-or-service-with-systemd-c34501e646c9

https://www.linode.com/docs/guides/start-service-at-boot/

/etc/systemd/system下にhelloworld.serviceという名前で

[Unit]
Description=helloworld
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/node /home/dalomo/nodejs/helloworld.js
Restart=always
WorkingDirectory=/home/dalomo/nodejs

[Install]
WantedBy=multi-user.target

こんな内容のファイルを作った。もうこれで使えるようになってるみたい。nginxとApacheを止めて

$ systemctl daemon-reload

で再読み込み。そんで

$ systemctl start helloworld

スタート!どきどき。確認してみる。

$ systemctl status helloworld
● helloworld.service - helloworld
Loaded: loaded (/etc/systemd/system/helloworld.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2020-12-03 22:32:45 JST; 1min 1s ago
Main PID: 13582 (node)
CGroup: /system.slice/helloworld.service
mq13582 /usr/bin/node /home/dalomo/nodejs/helloworld.js

Dec 03 22:32:45 1scp2bi4 systemd[1]: Started helloworld.
Dec 03 22:32:46 1scp2bi4 node[13582]: Server running at http://140.227.173....0/
Hint: Some lines were ellipsized, use -l to show in full.

おお…!できてるっぽい気がする。サーバーのIPアドレスにアクセスすると、上と同じように表示されてるー!すごい!めちゃめちゃ簡単なんだねぇ。そしたら

$ systemctl stop helloworld

で一回止めて、Node.jsアプリのポートを3000に変更して、nginxからリバースプロキシでアクセスできるようにしてきたい。

nginx + Node.js

http://www2.matsue-ct.ac.jp/home/kanayama/text/nginx/node16.html

http://www2.matsue-ct.ac.jp/home/kanayama/text/nginx/node36.html

https://heartbeats.jp/hbblog/2012/04/nginx05.html

サブディレクトリごとにどこの何を返すか、みたいなのを決めれるんと思う。現状は

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://127.0.0.1:8080;
        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-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

server {
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/dalomo.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dalomo.net/privkey.pem;
    server_name localhost;

    location / {
        proxy_pass http://127.0.0.1:8080;
        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-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

こんな感じになってる。なんとなく、Locationのパスを追加して、proxy_passの所をポート3000に飛ばせばいいような気がする。でもそれ以下の記述は全部一緒で、それが複数回繰り返されるからなんか冗長な気がするんですけどどうなんでしょう。まぁとりあえず愚直に

location /nodejs/ {
    proxy_pass http://127.0.0.1:3000;
    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-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

を、80と443をListenしてるserverの{…}内に追記してみる。そんでnginxとhelloworldのサービス起動すると、502 Bad Gateway…うーん?なんとなく、helloworld.jsのhostnameがよくなさそうな気がしたので、サーバーのIPアドレスから127.0.0.1に変えてみた。もっかい起動すると

できたー!!!すごい!動いてる!これでnginxとNode.jsとApacheが共存できた!いよいよチャットを作っていこう!

カテゴリー: したい タグ: , , , パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です