How to generate a local SSL certificate?

November 13, 2020#security

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

Then run:

openssl rsa -in keytmp.pem -out key.pem

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..");
  });

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",

or using Gatsby:

gatsby develop --https --key-file ./key.pem --cert-file ./cert.pem

Share the post: