Deploy node.js apps

31 July 2013


This is a simple and basic guide on how to deploy your super cool node.js apps. This assumes basic stuff like node and npm are already installed.

1. Install forever

[sudo] npm install forever -g

Here are a few basics you might need :

Command Action
forever start <script> start script as daemon
forever stop <script> stop script
forever list List running daemons
forever restartall Restart all daemons

For more info please refer to the forever Github page here.

3. Configure Nginx

Install Nginx

[sudo] pkgin in nginx
nginx -v

Configure Nginx

  1. cd /opt/local/etc/nginx/
  2. Open nginx.conf and add the following line :

     include /opt/local/etc/nginx/sites-enabled/*;

    right after the http { line.

  3. mkdir sites-enabled
  4. Now for all apps you want to run on that server create a file called <yourapp>.conf inside that folder. Copy/paste the following template :

     server {
         listen 80;
            server_name expaflyr *;
         access_log /var/log/nginx/expaflyr.access.log;
         location / {

    This file waits on port 80 for a request from and then loads the app running on port 8888. Simply change the server_name and the proxy_pass line.

Start Nginx

Now all there is to do is start our reverse proxy server. Do so by calling

[sudo] nginx

Again here a few basic commands :

Command Action
nginx -s quit graceful shutdown
nginx -s reload reloading the configuration file

More info our

4. Deploy your Code with git

Upload your app

Here is the basic idea : we have our local git repository in which resides our app and then on our instance we have two git repos :

| -- git/
|    | -- <your_app1>.git/
|    | -- <your_app2>.git/
|    | -- ...
| -- www/
|    | -- <your_app1>/
|    | -- <your_app2>/
|    | -- ...

The idea is we push our changes to the /git/<your_app>.git which will then checkout (or copy) the files into our other repo /www/<your_app> using a git hook.

  1. First, log into your instance as admin.
  2. Create both the git folder and the www folder.
  3. Then initialize the first repo :

     cd ~/www
     mkdir <your_app>
     cd <your_app>
     git init
  4. Finally we do the same on the other repo, but this one has to be bare.

     cd ~/git
     mkdir <your_app>.git
     cd <your_app>.git
     git init --bare

Now we have to create a hook which will copy all pushed files from our git repo to the 'production' repo.

  1. Create a file in ~/git/<your_app>.git/hooks/ called post-receive with the following content:

     GIT_WORK_TREE=/home/admin/www/<your_app> git checkout -f
     forever restartall
  1. Make it executable :

     $ chmod +x ~/git/<your_app>.git/hooks/post-receive

Finally we have to initialize our app as a git repo.

  1. Exit the instance and navigate to your application.
  2. Initialize it :

     git init
     git add .
     git commit -m "Hello out there!"
  3. Now add the remote git repo :

     git remote add origin admin@<ip>:git/<your_app>.git
  4. And then simply push :

     git push origin master

Now all your files will be in /home/admin/www/<your_app>/. And form now on just call git push Joyent master to deploy. It's that easy.

Run your app

cd ~/www/<your_app>
forever start <mainscript.js>

Be carefull, always run forever without sudo, so we can restart them through our hook.

Change your app

git add .
git commit -m "Added cool stuff"
git push origin master

5. Point domains to your app

Create an A-Record:

Field Value
Domain <>
Host Record @
Type A
Points To <ip>