DEV Community

Cover image for Enable HTTPS on Localhost in Under 2 Minutes
ℵi✗✗
ℵi✗✗

Posted on • Edited on

4 1 1 1 1

Enable HTTPS on Localhost in Under 2 Minutes

Introduction

Have you ever needed to enable HTTPS on your localhost quickly and effortlessly? This concise guide will walk you through a straightforward method that takes less than two minutes to set up.

Why Enable HTTPS on Localhost?

Enabling HTTPS on localhost is essential in scenarios such as:

  • Testing APIs that only function over HTTPS.
  • Avoiding the hassle of deploying code to external servers like DigitalOcean or Heroku for non-production work.

With this guide, you’ll achieve HTTPS locally without unnecessary complexity.


Solution: mkcert – Your Zero-Configuration HTTPS Enabler

Meet mkcert, a user-friendly tool for creating locally-trusted development certificates. Access it on its GitHub page and follow installation instructions for your operating system.

For Mac users with Homebrew, simply run the following commands in your terminal:

brew install mkcert
mkcert -install
mkcert -key-file ~/localhost-key.pem -cert-file ~/localhost-cert.pem localhost
Enter fullscreen mode Exit fullscreen mode

Deciphering the Command:

  • mkcert: The tool that simplifies creating certificates.
  • -key-file ~/localhost-key.pem: Path and filename for the private key file.
  • -cert-file ~/localhost-cert.pem: Path and filename for the certificate file.
  • localhost: The domain for which the certificate is generated.

After execution, two files will be created in your home directory:

  • localhost-key.pem (private key)
  • localhost-cert.pem (certificate)

Step 1: Create a Certificate Bundle

To simplify future use, concatenate the private key and certificate into a single file:

cat ~/localhost-key.pem ~/localhost-cert.pem > ~/localhost-bundle.pem
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up a Secure Tunnel with stunnel

To enable SSL/TLS on localhost, we’ll use stunnel. Install it with:

brew install stunnel
Enter fullscreen mode Exit fullscreen mode

Initiate SSL for Your Local Server

Start your local server and execute the following command:

sudo stunnel3 -f -d 443 -r 8000 -p ~/localhost-bundle.pem
Enter fullscreen mode Exit fullscreen mode

Breaking Down the Command:

  • sudo: Runs the command with elevated privileges.
  • stunnel3: Tool for creating encrypted tunnels.
  • -f: Runs stunnel in the foreground for real-time feedback.
  • -d 443: Specifies the local port for encrypted traffic (default HTTPS port).
  • -r 8000: The destination address and port of the local server.
  • -p ~/localhost-bundle.pem: Path to the concatenated certificate and key file.

Step 3: Verify Your Setup

If all steps are executed correctly, visit https://localhost in your browser to confirm your secure local server is running.


Conclusion

By following this guide, you can enable HTTPS on your localhost effortlessly. Whether you’re testing APIs or ensuring a secure development environment, this quick setup saves time and eliminates the need for external servers.


Follow Me for More!
Stay connected for more tips, tutorials, and resources:

GitHub
YouTube
LinkedIn

Happy coding!✌️❤️

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more