# How to generate a local SSL certificate

> Learn how to generate a local SSL certificate with openssl, creating cert.pem and key.pem, then load them in an Express HTTPS server or create-react-app.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-08-03 | Topics: [Networking](https://flaviocopes.com/tags/network/) | Canonical: https://flaviocopes.com/how-to-generate-local-ssl-cert/

> Note: I ran these commands on macOS. Linux should work in the same way. I don't guarantee for Windows.

In the project root folder, run:

```sh
openssl req -x509 -newkey rsa:2048 -keyout keytmp.pem -out cert.pem -days 365
```

Now run:

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

You should now have the files `cert.pem` and `key.pem` in the folder.

With Express/[Node.js](https://flaviocopes.com/nodejs/), you can load the certificate and key using this code:

```js
const fs = require('fs')
const https = require('https')
const app = express()

app.get('/', (req, res) => {
  res.send('Hello HTTPS!')
})

https.createServer({
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
}, app).listen(3000, () => {
  console.log('Listening...')
})
```

If you're using `create-react-app`, change the `start` script in the `package.json` file to:

```
"start": "export HTTPS=true&&SSL_CRT_FILE=cert.pem&&SSL_KEY_FILE=key.pem react-scripts start",
```

Look at your framework/library documentation on the instructions on how to pass the certificate and key to the app.
