How to set up an EC2 instance with Github, Node.js and PostgreSQL

For one of our Node.js web development projects at DigitalCrafts, we needed to host our website on an AWS EC2 instance. If you don’t know already, EC2 stands for “Elastic Compute Cloud,” which means it’s a little virtual computer in the cloud (aka on Amazon’s servers somewhere) that you can use to host/run web applications among other things. Hosting your website on EC2 intuitively sounds straightforward — like you should be able to just drop your code in there and it should appear on the web. But if you don’t know what you’re looking for, the documentation out there can make things more complicated than it should be. After digging through the internet for hours and writing some mystical command line incantations, here is what I discovered you need to do to set up and EC2 instance, connect it to Github, and install Node.js and PostrgreSQL.

Set up an EC2 instance

Sign up for an account at You will need to enter your billing and credit card information, sorry. However, you shouldn’t be charged too much for just one EC2 instance. You can always delete instances you no longer need.

Select Amazon EC2 from their product options.

Click the button that says “Launch Instance.”

You should now be in a configuration section that will take you through steps labeled 1–7.

Step 1 Choose an Amazon Machine Image (AMI): This is the operating system your virtual machine in the cloud will be. Select “Ubuntu Server 16.04 LTS (HVM), SSD Volume Type” or another Ubuntu version but make sure it’s labeled “free tier eligible.”

Step 2 Choose an Instance Type: Leave it at “General purpose t2.micro free tier eligible”. Click “Next: Configure Instance Details.”

Step 3 Configure Instance Details: Leave the default settings. Click “Next: Add Storage.”

Step 4 Add Storage: Leave the default settings. Click “Next: Add Tags.”

Step 5 Add Tags: Create a key and value. I named mine “tutorial-key”: “tutorial-value.”

Step 6 Configure Security Group: Check the radio button labeled “create a new security group” and give it a name and description. Make sure there is an SSH type listed and add HTTP and HTTPS. Click “Review and Launch.”

Step 7 Review Instance Launch: Click “Launch.” The following window should appear. Select “Create a new key pair” and type in a name. Click “Download.” Make sure you save this file!!! Click “launch instance.” You should be redirected to a page where you can click “view instances” to see all your instances. Your new EC2 instance should appear and may take a few moments to initialize. Tada! You have a new EC2 instance!

Set up ssh connection

On your local machine, go to your root directory in your terminal. Locate and move your .pem file (tutorial.pem in my case) into your .ssh directory. You can move it via finder, or with the following command: mv path-to-your-pem-file .ssh

Add the following statement to your .bashrc file (located in your root directory):alias youraliasname='ssh -i "~/.ssh/yourfile.pem" ubuntu@your-ec2-public-dns' This allows you to run your alias name to ssh into your ec2 instance instead of the the long string above.

In .bashrc I added the following: alias tutoralec2='ssh -i "~/.ssh/tutorial.pem"'

Save your .bashrc file and run .~/.bashrc

Run chmod -R 400 ~/.ssh/yourfile.pem

Now you can run your alias name to ssh into your ec2 instance.

Here is what the commands look like for my files.

Connect to Github

Here is a useful way to push your codebase up to your ec2 instance from Github so you can do most of your work on your local machine.

Once you’ve ssh-ed inside your ec2 instance, run the following command to create an ssh key, using the email you registered with Github.

$ ssh-keygen -t rsa -b 4096 -C ""

The terminal may prompt you to enter a file name in which to save the key and some passphrases. You can continue to hit enter to save the key to the default file, and continue without a passphrase.

Get your ssh key code under your .ssh directory in the file titled Copy the string within. It will start with something like ‘ssh-rsa’ and end with your email address. You can use vi, vim or another text editor to access the key.

Go to and make a new ssh key. You can give it whatever title you want. Paste the key you just copied from within the ‘key’ text field and click ‘add ssh key.’ You are now connected to Github and won’t have to sign in every time you want to push to your ec2 instance.

Next, return to your ec2 root directory and make a new directory in which you want your project located. I’ll just call mine ‘projects.’ Navigate into this new directory and clone your Github repository. You now have access to your codebase within ec2!

Install Node.js and node modules

If you are still in your project directory and need to install Node.js and npm, follow these commands.

Now you can initialize npm and install whichever node modules you need for your project. If your codebase already contains your dependencies in your package.json file, you can just run npm install. If you are starting a new Node project, run the command npm init, and proceed to install your modules as usual.

Install and configure PostgreSQL

Follow these instructions to install and configure your PostgreSQL database. Once you’ve restarted postgres, your ubuntu database should be ready to go. You can proceed as usual with database creation, or upload your existing schema with the command psql -f yourschema.sql yourdbname

Install and configure Nginx

Next, we will need to install nginx as our web server. Follow these commands, add this new location to /etc/nginx/sites-available/default, and comment out the existing locations.

location / {proxy_pass http://localhost:3000;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection 'upgrade';proxy_set_header Host $host;proxy_cache_bypass $http_upgrade;}

You can then open your ec2 public dns in your browser and should see a ‘Welcome to nginx!’ page.

Run your live app

Finally we need to install and configure the pm2 process manager to keep our application live and running. Run the following commands.

After a few moments, you should be able to refresh your site in the browser and see you web application up and running! Congrats you did it!!


Tip and blog posts from coding bootcamp students, graduates, teachers and staff

Jennifer Li Johnson

Written by


Tip and blog posts from coding bootcamp students, graduates, teachers and staff

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade