DEV Community

Murtaja Ziad
Murtaja Ziad

Posted on • Originally published at blog.murtajaziad.xyz on

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

Top comments (1)

Collapse
 
bharukarupesh11 profile image
Rupesh Bharuka

It would also be helpful if you provide the commands/steps for windows user.