Lets Generate a free SSL certificate and run an HTTPS server in 5 minutes or less (Node + Express + LetsEncrypt)

 

What you actually came for

First, you need those few things:

  • A server running on a linux distribution with root access (via SSH)
  • NodeJS: https://nodejs.org/en/
  • Express : npm install express
  • Certbot

To install certbot, copy-paste those lines in a terminal :

sudo add-apt-repository ppa:certbot/certbot

$ sudo apt-get update

$ sudo apt-get install certbot

Second, you will generate an SSL certificate with certbot :

$ certbot certonly --manual


Type your domain name(s) without the protocol part. For instance: yourdomain.com or even muchdomain.verysite.

Type then ENTER.

Note two things :

  • a-string : The name of the file you have to create, right now. Just create it, we’ll take care of the directories later.
  • a-challenge: Open the file you just created and put this challenge string into it. Nothing else, just this challenge string.

Now, don’t continue. You need to run a web server with Node & Express.

Keep your terminal opened somewhere

  • Create a directory with the name you want, e.g : server
  • In this directory, create a JS file which will run your server. Keep it empty for the moment as I’ll provide you with a ready-to-copy/paste source code.
  • In this directory, create two directories : .well-known , and inside this one, create : acme-challenge .
  • In the directory : acme-challenge place the file you created before : a-string

This is what you should have :

\server
----\.well-known
--------\acme-challenge
------------a-string
----server.js

Important: Actually the filename isn’t a-string, it is a long alphanumeric string. For security purposes I can’t show you mine. Same goes for a-challenge

You’re almost done !

Use your favorite code editor and copy-paste this code :


    
// Dependencies
    const express = require('express');

    // Configure & Run the http server
    const app = express();

    app.use(express.static(__dirname, { dotfiles: 'allow' } ));

    app.listen(80, () => {
      console.log('HTTP server running on port 80');
    });

To verify that everything is fine, open up your browser and navigate to : http://yourdomain.com/.well-known/acme-challenge/a-string

Your browser should download your challenge file. If it’s not the case, take everything back from the start. Don’t touch your shell, restart from directory & file creation.

If everything is okay, go back to your shell and type ENTER.


Copy-paste the following code and you’ll have a fresh HTTPS server running.

    
// Dependencies
    const fs = require('fs');
    const http = require('http');
    const https = require('https');
    const express = require('express');

    const app = express();

    // Certificate
    const privateKey = fs.readFileSync(
                '/etc/letsencrypt/live/yourdomain.com/privkey.pem'
                'utf8');
    const certificate = fs.readFileSync(
                '/etc/letsencrypt/live/yourdomain.com/cert.pem'
                'utf8');
    const ca = fs.readFileSync(
                '/etc/letsencrypt/live/yourdomain.com/chain.pem'
                'utf8');

    const credentials = {
      key: privateKey,
      cert: certificate,
      ca: ca
    };

    app.use((reqres=> {
      res.send('Hello there !');
    });

    // Starting both http & https servers
    const httpServer = http.createServer(app);
    const httpsServer = https.createServer(credentialsapp);

    httpServer.listen(80, () => {
      console.log('HTTP Server running on port 80');
    });

    httpsServer.listen(443, () => {
      console.log('HTTPS Server running on port 443');
    });

Navigate to : https://yourdomain.com, you should see “Hello there !”.

Well done, you’ve reached the end of this tutorial.

Last words:

  • You might encounter errors, restart the tutorial from the beginning and overall don’t forget to modify yourdomain.com with your actual domain name. Same goes for a-string and a-challenge.
  • If nothing works, let me apologize. StackOverflow will be your best friend.
  • This tutorial aims to make you use the manual method so that you have control over almost everything. In my case, this is the only solution that has worked.

Comments

Popular posts from this blog

How to download a file using command prompt (cmd) Windows?

The future of Artificial Intelligence: 6 ways it will impact everyday life

How to Include ThreeJs in Your Projects