Lately, I’ve frequently needed to create self-signed certificates. So, to avoid having to look up the process every single time, I’m posting the commands I use.

Generate RSA certificate Link to heading

openssl genrsa -out example.com.rsa.key 2048
openssl req -x509 -new -nodes -key example.com.rsa.key -sha256 -days 365 \
  -out example.com.rsa.crt \
  -subj "/CN=example.com" \
  -addext "subjectAltName=DNS:example.com,DNS:*.example.com"

or with a single command

openssl req -x509 -newkey rsa:2048 -sha256 -days 365 \
  -nodes -keyout example.com.rsa.key -out example.com.rsa.crt \
  -subj "/CN=example.com" \
  -addext "subjectAltName=DNS:example.com,DNS:*.example.com"

Generate ECDSA certificate Link to heading

openssl ecparam -name prime256v1 -genkey -noout -out example.com.ec.key
openssl req -x509 -key example.com.ec.key -sha256 -days 365 \
  -nodes -keyout example.com.ec.key -out example.com.ec.crt \
  -subj "/CN=example.com" \
  -addext "subjectAltName=DNS:example.com,DNS:*.example.com"

or with a single command

openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -sha256 -days 365 \
  -nodes -keyout example.com.ec.key -out example.com.ec.crt \
  -subj "/CN=example.com" \
  -addext "subjectAltName=DNS:example.com,DNS:*.example.com"

View certificate information Link to heading

openssl x509 -in example.com.ec.crt -noout -text
openssl x509 -in example.com.rsa.crt -noout -text

openssl x509 -in example.com.rsa.crt -noout -dates -issuer -subject -fingerprint -sha256 -sigopt md
openssl x509 -in example.com.ec.crt -noout -dates -issuer -subject -fingerprint -sha256 -sigopt md

or via an OpenSSL client

openssl s_client -showcerts -connect example.com:443 </dev/null | openssl x509 -text

Nginx config Link to heading

server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com;

    ssl_certificate /etc/nginx/ssl/example.com.ec.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.ec.key;

    # ...
}