LoginSignup
44
54

More than 5 years have passed since last update.

CentOSのサーバー上でApacheでNode.jsアプリを動かす

Posted at

apacheでアプリを動かしている環境でnode.jsも動かしたいときは、apacheからnode.jsにproxyしてやるといい。
ちょっとapacheのproxy設定にはまったのでメモ。

環境

# cat /etc/redhat-release 
CentOS release 6.6 (Final)

# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Oct 16 2014 14:48:21

$ node -v
v0.12.0

$ mongo --version
MongoDB shell version: 2.6.8

mongodbのインストール

次のサイトのとおりに実行する。

yumの設定

/etc/yum.repos.d/mongodb.repo
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

yumの実行

sudo yum install -y mongodb-org

起動

sudo service mongod start
mongo
exit

nodeのインストール

nodebrewのインストール

nodebrewをインストールすると、nodeのバージョンを切りかえられる。
バージョンによって変化が激しいので、入れておくと良い。

curl -L git.io/nodebrew | perl - setup
export PATH=$HOME/.nodebrew/current/bin:$PATH
source ~/.bashrc

nodeのインストール

nodebrew install stable
nodebrew use stable
node -v

apacheの設定

apacheのインストールや基本的な設定は省略。

conf.d

/etc/httpd/conf.dにNode.jsアプリ専用の設定を追加する。
末尾の'/'に注意する。
これがないと、cssやjsなどの相対パスがうまくいかない。

/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>

サービス起動

service httpd restart
cd YOUR_NODE_APP_PATH
node app.js &
44
54
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
44
54