How to easily host your Express server on https without a domain name

Peter Huh
3 min readJul 13, 2021

--

For a simple prototype, you may want to host the server on https, but not go through the hassle of buying a domain name. This is a short tutorial on how you could host your server on https only with a public IP address.

Let’s assume that we have our server running on AWS EC2 instance. To make this server run on https we only need to follow three simple steps.

  1. Download Apache/Nginx on EC2 instance.
  2. Get free SSL certificate from Certbot.
  3. Run server on https with the above credentials.

In this tutorial, I decided to use Apache for our web server software. From the terminal, SSH into your EC2 instance.

sudo apt update
sudo apt install apache2

The above commands will install Apache on your instance. To check if Apache is successfully installed, type the instance’s public IP address in the browser. You will be able to see image below. Make sure to allow port 80 on EC2 security group if you are unable to access your public IP address.

Next, go to https://certbot.eff.org/.

Select Apache and your instance system version. I decided to use Ubuntu 20.04 instance for this tutorial. Scroll down a little bit and you will be able to see a set of instructions. Carefully follow the instructions step by step.

On this line:

sudo certbot --apache

The above command will prompt you to answer a couple questions.

  1. Enter Email Address
  2. Accept terms and conditions
  3. Enter a domain name

Wait, but I don’t have a domain name yet! Don’t worry, here is a simple hack.

https://nip.io/ provides wildcard DNS for any IP Address. All you need to do is convert “.” to “-” in your IP address, and append “.nip.io”. If your IP address is 10.0.0.1, your wildcard DNS is 10-0-0-1.nip.io. So simply use this address for the domain name.

Now if you type in your wildcard DNS with https in front on browser, you will be able to see the lock icon signaling that the site is secured. Make sure to allow port 443 on EC2 security group if you are unable to access this page.

If you followed through all the instructions above, your certificate will be stored in path: /etc/letsencrypt/live/{hostname}/

If you are using Express:

var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('/etc/letsencrypt/live/{hostname}/privkey.pem', 'utf8');var certificate = fs.readFileSync('/etc/letsencrypt/live/{hostname}/cert.pem', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(3000);
httpsServer.listen(3443);

The above configuration lines will run your Express https server on port 3443. Make sure to enable whichever port you are using on EC2 security group if you are unable to access your server.

That’s it! If you see “This site can’t be reached”, most of the times its the problem of ports being not allowed in ec2 security inbound rules or server firewalls.

You can check if your firewall is active with the command below.

sudo ufw status

If you want to allow ports through the firewall use the command below.

sudo ufw allow {port #}

--

--

No responses yet