How to Generate an SSL Certificate with OpenSSL
In this tutorial, you'll learn about How to Generate an SSL Certificate with OpenSSL. We cover key concepts, practical examples, and best practices.
The Problem
You need an SSL/TLS certificate for a web server, but the openssl req command fails or produces a certificate that browsers reject. Without a properly generated certificate, HTTPS connections fail with security warnings.
Quick Fix
Step 1: Generate a Private Key
openssl genrsa -out key.pem 2048
# ...+++
# ...+++
ls -la key.pem
# -rw------- 1 user user 1679 Jun 24 12:00 key.pem
Step 2: Create a Certificate Signing Request
WRONG — missing required fields:
openssl req -new -key key.pem -out csr.pem
# You are about to be asked to enter information...
# Country Name (2 letter code) []:
# This produces a CSR with empty fields, rejected by most CAs.
RIGHT — supply all fields non-interactively:
openssl req -new -key key.pem -out csr.pem \
-subj "/C=US/ST=California/L=San Francisco/O=DodaTech/CN=example.com"
ls -la csr.pem
# -rw-r--r-- 1 user user 1704 Jun 24 12:01 csr.pem
Step 3: View the CSR Contents
openssl req -in csr.pem -text -noout | grep -E "Subject:|Public Key"
# Subject: C = US, ST = California, L = San Francisco, O = DodaTech, CN = example.com
# Public Key Algorithm: rsaEncryption
Step 4: Self-Sign for Local Testing (Skip CA)
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out cert.pem
# Certificate request self-signature ok
# wrote certificate to 'cert.pem'
Step 5: Generate with SAN Extensions (Required by Browsers)
WRONG — no Subject Alternative Names:
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out cert.pem
# Chrome shows: NET::ERR_CERT_COMMON_NAME_INVALID
RIGHT — include SANs via config file:
cat > san.cnf <<EOF
[v3_req]
subjectAltName = DNS:example.com,DNS:www.example.com,DNS:api.example.com
EOF
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out cert.pem -extensions v3_req -extfile san.cnf
# Certificate request self-signature ok
openssl x509 -in cert.pem -text -noout | grep -A1 "Subject Alternative Name"
# X509v3 Subject Alternative Name:
# DNS:example.com, DNS:www.example.com, DNS:api.example.com
Step 6: Verify the Certificate
openssl verify -CAfile cert.pem cert.pem
# cert.pem: C = US, ST = California, O = DodaTech, CN = example.com
# error 18 at 0 depth lookup: self-signed certificate
# (This is expected for self-signed certs)
Submit the CSR to a Certificate Authority like Let's Encrypt for a browser-trusted certificate. DodaTech's SSL Checker tool validates certificate chains and highlights missing SAN entries before deployment.
Prevention
- Always include SAN entries for browser compatibility.
- Use at least 2048-bit RSA or EC keys for production.
- Keep the private key file permission-restricted (
chmod 600). - Set an appropriate validity period (90 days for LE, 365 for self-signed).
- Back up the private key securely — it cannot be recovered if lost.
Common Mistakes with cert generate
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world OPENSSL code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro