53 notes &
Hosting NodeJS Apps on CentOS 5
I recently enjoyed a great article by Alex Young from DailyJS, called “Hosting Node Apps” (http://dailyjs.com/2010/03/15/hosting-nodejs-apps/). However, my server runs on CentOS so setting it up was a little bit different. Here’s what I did to setup, hopefully it helps you out as well.
Grab Core Dependencies
yum install sudo nginx unzip gcc-c++ screen git-core monit
Install NodeJS
Clone,
git clone git://github.com/joyent/node.git
then Build
./configure make make install
Configure nginx Web Server
Edit the virtual host configuration file at:
/etc/nginx/conf.d/virtual.conf
Add the following to the end of the file:
upstream app_cluster_1 {
server 127.0.0.1:8000;
}
server {
listen 0.0.0.0:80;
server_name nodetest.local nodetest;
access_log /var/log/nginx/nodetest.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_cluster_1/;
proxy_redirect off;
}
}
Save and reload nginx:
service nginx reload
Create a Node App
Make a quick sample app at:
/var/www/apps/hello_world/example.js
Putting in the following contents:
var sys = require("sys"),
http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World");
}).listen(8000);
sys.puts("Server running at 127.0.0.1:8000");
Set up Monit
Create a new configuration file for monit at:
/etc/monit.d/hello_world
Putting in the following contents:
check host hello_world with address 127.0.0.1
start program = "/usr/local/bin/node /var/www/apps/hello_world/example.js"
stop program = "/usr/bin/pkill -f 'node /var/www/apps/hello_world/example.js'"
if failed port 8000 protocol HTTP
request /
with timeout 10 seconds
then restart
Then restart monit for the configuration to take place:
monit start all && service monit restart
All set!
Go ahead and point your browser to http://nodetest.local to see a “Hello World” page (if everything worked!). For more details on the how and why and for additional resources, check out Alex’s article. This is just intended to give a quick start for CentOS 5 users since some of the commands and packages are different.