Skip to content

Instantly share code, notes, and snippets.

@jagamts1
Created May 2, 2021 10:22
Show Gist options
  • Save jagamts1/a9eecb5d6c1e07141a5aa04ff73a4a11 to your computer and use it in GitHub Desktop.
Save jagamts1/a9eecb5d6c1e07141a5aa04ff73a4a11 to your computer and use it in GitHub Desktop.
Bash shell script for generating self-signed certs.
#!/bin/bash
# Bash shell script for generating self-signed certs.
# usage: ./gen_ssl_cert.sh superset.com
# Ref: https://gist.githubusercontent.com/adamrunner/285746ca0f22b0f2e10192427e0b703c/raw/23ec7544d0377aea3df06e4e9a684935c68bd397/gen_cert.sh
# Script accepts a single argument, the fqdn for the cert
DOMAIN="$1"
if [ -z "$DOMAIN" ]; then
echo "Usage: $(basename $0) <domain>"
exit 11
fi
fail_if_error() {
[ $1 != 0 ] && {
unset PASSPHRASE
exit 10
}
}
# Generate a passphrase
export PASSPHRASE=$(head -c 500 /dev/urandom | tr -dc a-z0-9A-Z | head -c 128; echo)
# Certificate details; replace items in angle brackets with your own info
subj="
C=US
ST=OR
O=superset
localityName=superset
commonName=$DOMAIN
organizationalUnitName=superset
[email protected]
"
# Generate the server private key
openssl genrsa -des3 -out $DOMAIN.key -passout env:PASSPHRASE 2048
fail_if_error $?
# Generate the CSR
openssl req \
-new \
-batch \
-subj "$(echo -n "$subj" | tr "\n" "/")" \
-key $DOMAIN.key \
-out $DOMAIN.csr \
-passin env:PASSPHRASE
fail_if_error $?
cp $DOMAIN.key $DOMAIN.key.org
fail_if_error $?
# Strip the password so we don't have to type it every time we restart Apache
openssl rsa -in $DOMAIN.key.org -out $DOMAIN.key -passin env:PASSPHRASE
fail_if_error $?
# Generate the cert (good for 10 years)
openssl x509 -req -days 3650 -in $DOMAIN.csr -signkey $DOMAIN.key -out $DOMAIN.crt
fail_if_error $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment