7
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Apache2 と node を連携する。

Last updated at Posted at 2019-01-18

AWSLinux, CentOS が対象。(Ubuntuは設定の方法が違います。)
設定ファイルを作成する。

/etc/httpd/conf.d/node.conf
Alias /node /var/www/node

<Location /node/>
  ProxyPass http://localhost:3000/
  ProxyPassReverse http://localhost:3000/
  Order deny,allow
  Deny from all
  Allow from all
</Location>

node を起動する。

$ node app.js

作成したらnodeサーバを再起動。

$ service httpd restart

これで サイトのURL/node/ で nodeサーバーが呼ばれるようになる。


でも、実際はあまりよろしくない?

Apache と node 使ったサービスを一つのサーバーに無理やり押し込めるにはありだけど、
イベントループ方式の node と、スレッド方式の Apache ではあまり相性が良くない。
上の方式だと、来たら直接 node のビルトインサーバーに転送するだけなので、まあ良いけど、
できれば Docker などを使って、単体で起動しておいた方がいいかも。

nodeを永続的に実行する forever。

上記の方法で node を起動した場合、nodeサーバはフォアグラウンドで実行される。
nohup を使うと、正しく動かなかったりするので、forever を使ったりする。
foreverのインストールは、一旦ルートになって、以下のコマンドを実行する。

$ npm install -g forever

次にデーモンとしてスクリプトをバックグラウンドで実行する。

$ forever start app.js

foreverの詳しい使い方はヘルプを参照。

$ forever --help
7
12
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
7
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?