Skip to content

Commit a94de34

Browse files
committed
Use mkcert if available
1 parent 30eb99c commit a94de34

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

make-localhost-cert.sh

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,52 @@
33
set -e
44
cd "$(dirname "$0")"
55

6+
# Ensure certs directory exists
7+
mkdir -p certs
8+
69
if [[ -f "certs/key.pem" ]] || [[ -f "certs/cert.pem" ]]; then
710
echo "Certificate already exists."
811
exit 0
912
fi
1013

11-
if ! type 2>&1 >/dev/null openssl ; then
12-
echo >&2 "Error: openssl not found!"
14+
echo "Creating certificates..."
15+
16+
# Check if mkcert is available
17+
if type mkcert >/dev/null 2>&1; then
18+
echo "Using mkcert to generate trusted certificates..."
19+
20+
# Install local CA if not already installed
21+
mkcert -install >/dev/null 2>&1 || true
22+
23+
# Generate certificate for localhost and common local addresses
24+
mkcert -cert-file certs/cert.pem -key-file certs/key.pem \
25+
localhost 127.0.0.1 ::1 \
26+
"*.localhost" \
27+
"localhost.localdomain" \
28+
"*.localhost.localdomain"
29+
30+
echo "Certificates created with mkcert (automatically trusted in browsers)"
31+
32+
elif type openssl >/dev/null 2>&1; then
33+
echo "mkcert not found, falling back to openssl..."
34+
echo "You will need to accept a security exception for the"
35+
echo "generated certificate in your browser manually."
36+
37+
openssl req -x509 -newkey rsa:4096 -nodes -days 3650 \
38+
-subj "/C=DE/O=Selfsigned Test/CN=localhost" \
39+
-keyout certs/key.pem -out certs/cert.pem
40+
41+
echo "Self-signed certificate created with openssl"
42+
43+
else
44+
echo >&2 "Error: Neither mkcert nor openssl found!"
45+
echo >&2 "Please install either mkcert (recommended) or openssl."
46+
echo >&2 ""
47+
echo >&2 "To install mkcert:"
48+
echo >&2 " - macOS: brew install mkcert"
49+
echo >&2 " - Linux: Check https://github.com/FiloSottile/mkcert#installation"
50+
echo >&2 " - Windows: choco install mkcert or scoop install mkcert"
1351
exit 1
1452
fi
1553

16-
echo "Creating certificates..."
17-
echo "You will need to accept an security exception for the"
18-
echo "generated certificate in your browser manually."
19-
openssl req -x509 -newkey rsa:4096 -nodes -days 3650 \
20-
-subj "/C=DE/O=Selfsigned Test/CN=localhost" \
21-
-keyout certs/key.pem -out certs/cert.pem
2254
echo "done"

0 commit comments

Comments
 (0)