How I set up this subdomain in Nginx on Digital Ocean

I've been scratching my head a lot lately. It's not because of issues with my scalp, but rather the setup on the sub domain of this very site. I guess this is the sort of stuff you either know or don't know. After hours of researching, I finally found a solution that work for me.

My setup

I have a portfolio site and a blogging platform. Both are node.js apps, running on Express and Ghost. I run them on two ports; 8080 and 2368. I run a VM with Ubuntu 14.2 on Digital Ocean (5$/20GB SSD).

DNS on Digital Ocean

Nothing too fancy there. Create a DNS record in Digital Ocean. Set up an A record with the IP address and a CNAME that allows the "blog" subdomain.

Nginx on Ubuntu

I installed Nginx on the server by flipping up the terminal and running sudo apt-get install nginx. Nginx should be located at "/etc/nginx" in our file system. I deleted all files in the "/etc/nginx/sites-available" directory and created a file named "blogs". This is the files contents:

Here we have a reverse proxy set up. In my solution, it's very important NOT to listen to port 80 on each server. Then it will resolve to the same port and it don't work. This is where I was stuck for a while.

After that, make a symbolic link to "sites-enabled" by running sudo ln -s /etc/nginx/sites-available/blogs /etc/nginx/sites-enabled/. Start the nginx server by running sudo service nginx start

Keeping node.js apps alive with PM2

I use the PM2 module, but you can also use forever. If you have node and npm installed, run npm i -g pm2 and find the directory of the app you want to run. Simply run NODE_ENV=production pm2 start 'serverfile'.js --name "YourName". I did this for both node.js apps.

Results

Well, you are looking at it! Yaay! Took a bit longer than expected. I hope this post aids you with the same issues and brings you great relief. Check out my sites: blog and portfolio!