Introduction to mkcert
mkcert
is a simple, zero-config tool to make locally-trusted development certificates. It requires no configuration, making it perfect for local development and testing environments. In this guide, we will explore the basics of mkcert
and delve into numerous API examples that demonstrate its capabilities.
Installing mkcert
To get started with mkcert
, you first need to install it. Follow the installation instructions for your operating system:
For macOS:
brew install mkcert brew install nss # For Firefox support
For Windows:
choco install mkcert
For Linux:
sudo apt install libnss3-tools wget https://dl.filippo.io/mkcert/latest?for=linux/amd64 -O mkcert chmod +x mkcert sudo mv mkcert /usr/local/bin/
Using mkcert
To start using mkcert
, follow these commands:
Creating a local CA:
mkcert -install
Generating Certificates:
mkcert example.com "*.example.com" example.test localhost 127.0.0.1 ::1
Dozens of Useful API Examples
Basic Commands
Generate a Certificate for Local Development
mkcert my-domain.test
Generate a Wildcard Certificate
mkcert "*.my-domain.test"
Advanced Commands
Generate a Certificate with IP Addresses
mkcert my-domain.test 127.0.0.1 ::1
Generate a Certificate for Multiple Hosts
mkcert my-domain.test example.com '*.example.org'
Working with Custom Key and Certificate File Paths
mkcert -key-file key.pem -cert-file cert.pem my-domain.test
App Example Using mkcert APIs
Consider an example where you have a Node.js application, and you want to use mkcert
to create and use certificates:
Step-by-Step Guide
1. Install mkcert and generate certificates:
mkcert -install mkcert myapp.test
2. Install dependencies for your Node.js application:
npm install express https fs
3. Create an Express server with HTTPS:
const fs = require('fs'); const https = require('https'); const express = require('express'); const app = express(); const options = { key: fs.readFileSync('myapp.test-key.pem'), cert: fs.readFileSync('myapp.test.pem') }; app.get('/', (req, res) => { res.send('Hello, mkcert!'); }); https.createServer(options, app).listen(3000, () => { console.log('Server is running on https://myapp.test:3000'); });
With the above example, your Node.js application is now set up to use HTTPS with the certificates generated by mkcert
.
Summary
mkcert is an invaluable tool for developers who need to set up HTTPS for local development quickly. We covered installation, basic, and advanced API usage with code snippets. We also provided a practical example using a Node.js application. Armed with this knowledge, you can confidently use mkcert in your projects.
Hash: 087138ed6d1b4952037e29ffcc77a2991786c6cd881694b36ef0ed94bf8a9757