19 July, 2024
When developing a Single Page Application (SPA) such as a React app that involves authentication or secure communication, it often becomes necessary to run the development environment over HTTPS. This requirement can stem from using an authentication provider that mandates HTTPS for security reasons. Setting up SSL certificates for local hosts is crucial for mirroring the production environment as closely as possible and avoiding security warnings during development. This guide provides step-by-step instructions on how to set up SSL certificates locally using the `mkcert` tool.
Using SSL (HTTPS) on localhost ensures that:
[`mkcert`](https://www.npmjs.com/package/mkcert) is a simple tool for making locally trusted development certificates. It requires zero configuration and works seamlessly out of the box. Unlike self-signed certificates, certificates created by `mkcert` are automatically trusted by your system’s certificate store, meaning you don’t get those annoying browser warnings.
To begin, you need to install `mkcert` globally via npm:
```bash
npm install -g mkcert
```
First, decide on where you would like to save your certs. It can be your project folder or a common folder accessible to all your projects on your system. This could be something like `c:/dev/.cert` on Windows or `~/dev/.cert` on macOS/Linux. I chose a common folder, so I didn’t have to create a CA for every project.
Create the directory if it does not exist, then navigate to it.
```bash
mkdir <full-path-to-directory>/<directory-name>
cd <full-path-to-directory>/<directory-name>
```
```bash
mkdir c:/dev/.cert
cd c:/dev/.cert
```
A Certificate Authority (CA) acts as a trusted entity that issues digital certificates. By creating your own CA with `mkcert`, you can sign your local certificates such that they are trusted by your operating system and browsers.
Create your CA with the following command:
```bash
mkcert create-ca --validity 900 --key [name].key --cert [name].crt
```
```bash
mkcert create-ca --validity 900 --key ca.key --cert ca.crt
```
I chose to name my files `ca.key` and `ca.cert`.
Note: The `validity` parameter sets the duration that the certificate is considered valid. Here, `900` days is just an example; adjust it based on your preference.
With the CA created, you can now generate an SSL certificate for `localhost`:
```bash
mkcert create-cert --validity 900 --ca-key [ca-name].key --ca-cert [ca-name].crt--key [cert-name].key --cert [cert-name].crt --domain localhost
```
```bash
mkcert create-cert --validity 900 --ca-key ca.key --ca-cert ca.crt --key cert.key --cert cert.crt --domain localhost
```
Ensure that `[ca-name]` points to the `.key` and `.crt` files you generated in the previous step.
On Windows:
On macOS:
In your SPA project, such as a React app, configure the development server to use your SSL certificates. For instance, if you’re using `create-react-app`, you can set environment variables in your `.env` file:
```plaintext
SSL_CRT_FILE=<full-path-to-cert.crt>
SSL_KEY_FILE=<full-path-to-cert.key>
```
Replace `<full-path-to-cert.crt>` and `<full-path-to-cert.key>` with the absolute paths to your certificate files.
By following these steps, you’ve set up a secure, HTTPS-enabled development environment for your SPA. This setup not only improves security but also ensures compatibility with modern web APIs and authentication services that require HTTPS. Happy secure coding!
Read more on related topics:
Ready to secure your React app development environment with SSL certificates? Xorbix specializes in software development solutions tailored to enhance your project’s security and performance. Contact us today to learn more!
Discover how our expertise can drive innovation and efficiency in your projects. Whether you’re looking to harness the power of AI, streamline software development, or transform your data into actionable insights, our tailored demos will showcase the potential of our solutions and services to meet your unique needs.
Connect with our team today by filling out your project information.
802 N. Pinyon Ct,
Hartland, WI 53029
(866) 568-8615
info@xorbix.com