Run NodeJS in Ubuntu 11.04 EC2 With User Permission and Run as Service


In this post, i show you how to install & run NodeJS in Production Mode without ROOT permission. I make this runing and tested on Amazon EC2 on Ubuntu Natty 11.04. I user user permission NodeJS installation on [my NodeJS how to installation] (http://yoodey.com/how-install-nodejs-ubuntu-natty-1104-and-start-learning)
Set environment

Our node is installed on ~/local/node. We will make this also can be accessed in GLOBAL environment. My home path is /home/ubuntu.

Editing /etc/environment :

NODE=”/home/ubuntu/local/node”
PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$NODE/bin:$NODE/lib/node_modules”

And restart your server to load NODE in ENV.
Install Runit

We will using Runit for running NodeJS application.

sudo apt-get install runit
cd /etc/sv && sudo mkdir userservice
sudo touch run && sudo chmod a+x run
sudo chown ubuntu:ubuntu -R /etc/sv/userservice

We will make service running by users, not by root access.

Edit run files :

#!/bin/sh
exec 2>&1
exec chpst -U ubuntu:ubuntu runsvdir /home/ubuntu/service

Registering User service

Now we creating service in user home. All application that in this service folder, will running by Runit with user permission.

cd ~/
mkdir service && cd service
mkdir nodejs && cd nodejs
touch run && chmod a+x run

Edit run files :

#!/bin/sh
exec 2>&1
exec sudo -i -u ubuntu node /var/yodi/obrool.com/chat-server.js >> /var/yodi/obrool.com/node.log 2>&1

In this example, my nodejs application located in /var/yodi/obrool.com/chat-server.js.
Start service

Register userservice into /etc/services by symbolic links : sudo ln -s /etc/sv/userservice /etc/service/

Now you can start nodejs by sv start userservice.

Do ps aux | grep node and you will get this results :

root      7728  0.0  0.0    160     4 ?        S    07:13   0:00 runsv nodejs
root      7729  0.0  0.0   2400  1160 ?        S    07:13   0:00 sudo -i -u ubuntu node /var/yodi/obrool.com/chat-server.js
ubuntu    7730  0.5  0.8  58536 14216 ?        Sl   07:13   0:27 node /var/yodi/obrool.com/chat-server.js
ubuntu    8482  0.0  0.0   3612   860 pts/5    S+   08:33   0:00 grep –color=auto node

Mission complete!

Copy from http://www.yodi.me/blog/2011/09/05/run-nodejs-in-ubuntu-11-dot-04-ec2-with-user-permission-and-run-as-service/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s