Running a NodeJS Application on Ubuntu

Hello World!
The first post of the blog is a quick start-up guide on running any Node Application on Amazon AWS, and this is exactly how this blog made with ghost is running on an AWS EC2 Ubuntu instance.

In order to do so, we need to install Nginx, a light weighted web server that handles proxying the request to another port of the localhost, which we will have our NodeJS application listening to. And we will use a NodeJS package called PM2 to permanently run our app in the background.

First of all, you need to install Nginx, NodeJS and PM2 on your Ubuntu if you have not got them already.

#install Nginx
sudo apt-get update
sudo apt-get install nginx
#install NodeJS
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
#install PM2
sudo npm install -g pm2

Next step is to run the node application with PM2, take ghost for example, running it on production environment can be done by doing

NODE_ENV=production pm2 start index.js --name \"ghost\"

The <code>--name</code> parameter lets you set an app name in pm2 so you don't confuse yourself when you see a list of apps all named \"index\" in PM2.

You can check the status of your app with pm2 list, if you see the app is crashed or stopped, check the logs with pm2 logs to find out why.

Default ghost is running on port 2368 with production mode, all we need to do now is just to fire the request to this port for any incoming traffic to our site. To do this we will need to setup a new server in Nginx. By convention, a new file should be added to the

server {
  server_name [ENTER YOUR URL HERE];
  listen 80;
  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://127.0.0.1:[ENTER NODEJS APP PORT];
    proxy_redirect off;
  }
}

Restart Nginx and you should now be able to access your NodeJS app.

sudo service nginx restart

To sum up, this is a step by step guide with minimal explanation. If you want to find out more, ask Google.