Setting Up a Node Website on Apache with AWS Lightsail

Nicholas Hrboka
5 min readFeb 10, 2020

My first time navigating AWS Lightsail was… confusing. I have never configured an apache server or worked with a linux command line before, so naturally I spent a lot of time searching through Stackoverflow via Google and Youtube videos.

So this is a guide on how to set up a web server pointing to the correct port on AWS Lightsail, for people that aren’t as familiar with the tech.

My example will Use a run of the mill express node site running on port 3000.

Step 1

Setting up Lightsail (aka Apache with Bitnami)

First thing is first.

This AWS mastermind has a great guide to getting it set up. If you’re like most devs, you already have the site running locally, with a database connected. I would ignore the port about switching the port from 80 to 8080 in the httpd.conf file. Not needed, and I’ll explain why below.

The basic step is to SSH into the server, and then simply clone your repo from github.

git clone “https://github.com/profile/repo.git

If you don’t have your code on github or any version control site, you need to get on that.

Then simply go to the directly and install any dependencies.

cd repo/

npm install -s

Great! You can simply run your server with whatever start command you use!

eg:

npm start

or

node app.js

Whatever method you set up and prefer.

Now congrats!!! You can visit your site!!! You may notice a few issues at this point. You can visit your site through the IP address assigned, but it won’t work. You can even add the port :3000 at the end, and for some reason, no dice.

I’ll explain.

You need to set up a static IP address, and add the port to your firewall.

Thankfully, this is easy. Simply click on the Networks tab, and add your port to the firewall. See a completed example below. Here you can also create a static IP.

Once set up, you visit the static IP at the exposed port to visit your site! One issue though, if you close the SSH connection, you will notice that it doesn’t work, because you closed the session running the server.

Don’t worry, we have a solution for that.

Step 2

pm2

pm2 is an npm install that allows you to continue running the server even after the close the SSH session. See the quick start docs below for help.

https://pm2.keymetrics.io/docs/usage/quick-start/

Essentially, just run these commands:

npm install -g pm2

pm2 start server.js (< — your server file)

Easy! Done! pm2 is an awesome tool that will handle restarts for you as well.

If you want to check the status, simply run:

pm2 ls

If you want to terminate the servers, run:

pm2 kill

Now this is great! App is up and running, everything looks good. But what if you connect the the server to a domain name, say, “www.example.com”?

Connecting a Lightsail instance with Route 53 is easy thankfully, just use the static IP for the A name address. But you will notice that in order to access the site, you have to visit:

http://www.example.com:3000

Do you want to make a note to your client that people have to type in the port every time they visit the site?

Thankfully, there is a “simple” solution.

Step 3

Setting up a Proxy on Apache

(The not so “simple”, “simple” solution)

Ok ok, it might not seam so crazy, but this next part took me 5 hours to figure out. 5 head splitting hours of reading documentation on bitnami, apache, and so many stackoverflow posts about almost similar problems that I could almost use. I should note, 2 of these hours were trying to set a port forward with s3 buckets, but we will ignore that ‘hack’.

So here it is!

Apache is the server that you are using to serve your app. Yes, it’s similar to the movie Inception.

But it is not as complicated as the movie, thankfully.

To set it up so that we only have to go to http://www.example.com, we need to edit only 1 file in Apache. This involves using vim. Vim is a built in command line text editor, and every programmers favorite tool to harass new developers.

First thing is first, run this command:

vim /opt/bitnami/apache2/conf/bitnami/bitnami.conf

This is a hidden file, that is not easy to find without looking it up before hand.

You will see something like this:

Don’t be intimidated, it’s quite easy. Just add this where the blue arrow points, under VirtualHost

ProxyPass / http://127.0.0.1:3000/

ProxyPassReverse / http://127.0.0.1:3000/

To do this, press “I” to insert, then use the arrow keys to got to the write location and type.

Next, press “Esc” and then type “:wq”. Basically that writes the new changes to the document and then quits the editor. These are common vim commands, but if you’re new, you usually have no idea how to write or exit this.

The ProxyPass methods above basically route the server to serve the specified location, in this case localhost:3000, at the root of the static IP address.

Next, restart the server with this command:

sudo /opt/bitnami/ctlscript.sh restart

Then don’t forget to check the status of your server with:

pm2 ls

If you access the at example.com, you will see the server up and running! Easy right?

That’s it! Congrats on setting up a site with AWS Lightsail. It’s a pretty awesome tool once you get the hang of it. Now give yourself a pat on the back!

Don’t feel bad about having to reference this often, I wrote the article because I have to reference this often enough.

Cheers!

--

--