How to generate a local SSL certificate?

murtajaziad

Murtaja Ziad

Posted on November 22, 2020

How to generate a local SSL certificate?

These commands has been tested in linux.

In the project root folder, run

openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365
Enter fullscreen mode Exit fullscreen mode

Then run:

openssl rsa -in keytmp.pem -out key.pem
Enter fullscreen mode Exit fullscreen mode

Now, you should have cert.pem and ket.pem in the project root folder.


If you’re using Node.JS/Express, you can load the certificate and key using:

const express = require("express");
const https = require("https");

const app = express();

app.get("/", (req, res) => {
  res.json({
    message: "Hello",
  });
});

https
  .createServer(
    {
      key: fs.readFileSync("key.pem"),
      cert: fs.readFileSync("cert.pem"),
    },
    app
  )
  .listen(3000, () => {
    console.log("Listening..");
  });
Enter fullscreen mode Exit fullscreen mode

or if you are using create-react-app modify the start script in package.json:

"start": "export HTTPS=true&&SSL_CRT_FILE=cert.pem&&SSL_KEY_FILE=key.pem react-scripts start",
Enter fullscreen mode Exit fullscreen mode

or using Gatsby:

gatsby develop --https --key-file ./key.pem --cert-file ./cert.pem
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
murtajaziad
Murtaja Ziad

Posted on November 22, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related