/Lightsail-Setup-Custom-Domain-SSL

Deploy a Node / Express app on a Lightsail instance (VPS) on AWS, add a custom domain + SSL in 10 min!

Primary LanguageJavaScript

Deploy a Node / Express app on AWS Lightsail with a Custom Domain + SSL

Video Tutorial (10 min)


Steps below if you prefer images

Create a Lightsail instance from the AWS dashboard

Dashboard Image

Choose your instance type

Linux & Node

Instance type

Wait for the instance to be created

Wait

In the meantime, go to the Networking tab and click on Create static IP

Static

Attach the static IP you just created to your instance

attachIP

Go back home and click on networking so that you arrive at this tab

networktab

Click on Create DNS zone and enter your domain here

domain

I personally use Google Domains to buy my domains but if you prefer to stay in the ecosystem, Amazon has their own solution called Route53

I will be using opensourceme.app for the rest of this tutorial as I have this one for testing

Click create and add two A records

The first one should be @ in the Subdomain and Resolves to should be the static IP address we created a few steps ago

rs

Your second subdomain should be www in the first box and once more, select your static IP

added

Add the name servers that Amazon provides you to your domain in your domain DNS settings. The following is for Google Domains:

ns gd

Go back to your instance networking settings and create a firewall rule so that we can do a quick connection test in a few steps

int

custom TCP on port 3000 - You can delete this later after your site is up

poot

Go back to the homepage and click the terminal icon to SSH into your instance. Your instance should be up and running by now.

ssh

Go into htdocs

cd htdocs

Delete everything inside

rm -rf *

dlt

Clone this very repository!

git clone https://github.com/joswayski/Lightsail-Setup-Custom-Domain-SSL.git

Go into the repository folder

cd Lightsail-Setup-Custom-Domain-SSL

Install the dependencies

npm install

installed

Run the app to test it!

node index.js

installed

You should see: Example app listening at http://localhost:3000

If you go to your static IP from earlier and add :3000 at the end, you should be able to see something like this:

up

Now we have to route HTTP traffic on port 80 and all HTTPS traffic on port 443 to our server's port, 3000

Here is the documentation from Bitnami directly, or you can follow my steps below

Stop your Node app in the SSH terminal Control + C on Mac, I believe it is the same for Windows.

In the terminal, type in vi /opt/bitnami/apache/conf/vhosts/myapp-http-vhost.conf and press enter

Type i for INSERT mode

Visit this link and copy this code for port 80

NOTE: If you would like to use another port in your app, make sure that you change the http://localhost:3000 line to http://localhost:YOUR_PORT_NUMBER as well, in this section and the one coming up

bitnami1

IMPORTANT:

  • Replace both instances of /opt/bitnami/projects/myapp/public with your app directory which, if you followed my tutorial, is in /home/bitnami/htdocs/Lightsail-Setup-Custom-Domain-SSL

rme1

Press your ESCAPE key

esc

And then force save

:w!

Quit the editor

:q

Now do the same thing for the other port:

vi /opt/bitnami/apache/conf/vhosts/myapp-https-vhost.conf

ENTER

i to enter INSERT mode

Copy and paste the second part for port 443

bitnami2

Again, be sure to replace the /opt/bitnami/projects/myapp with your app directory, or if you are following this tutorial: /home/bitnami/htdocs/Lightsail-Setup-Custom-Domain-SSL

rm443

Escape, force save, and quit the editor.

esc

:w!

:q

Restart the Apache server

sudo /opt/bitnami/ctlscript.sh restart apache

If you go back to your directory and run your app:

cd /home/bitnami/htdocs/Lightsail-Setup-Custom-Domain-SSL

node index.js

You will be able to visit your site without putitng the port number at the end!

noport

To get your SSL certificate (fix that Not Secure in your browser), run:

sudo /opt/bitnami/bncert-tool

You will be prompted to enter this again, paste it in the terminal and run it again

twicerun

You will then be prompted to enter the domain that you chose, enter www.yourdomain.com and yourdomain.com

dom

  • Enable HTTP to HTTPS redirection [Y/n]: Y

  • Enable non-www to www redirection [Y/n]:

Up to you, I usually say Y because it looks more "professional"

  • Enable www to non-www redirection [Y/n]: N

Again, personal preference. I say N to this

  • Do you agree to these changes? [Y/n]: Y

Enter your email and hit Y on the following prompts. This will take a minute.

donezo

Go back to our app and run the server forever!

cd /home/bitnami/htdocs/Lightsail-Setup-Custom-Domain-SSL

forever start index.js

Your server is up and running on your custom domain with SSL! Congrats!

fin


Feel free to delete the firewall rule on port 3000 we created earlier. Do not hesitate for any PR's or issues. Happy hosting!