Skip to content

Instantly share code, notes, and snippets.

@tomcarlson
Last active December 23, 2015 05:19
Show Gist options
  • Select an option

  • Save tomcarlson/6585973 to your computer and use it in GitHub Desktop.

Select an option

Save tomcarlson/6585973 to your computer and use it in GitHub Desktop.
Bash script to create self-signed SSL Key to allow secure comms to website
# Create self-signed SSL Key to allow secure comms to website
# http://www.thegeekstuff.com/2009/07/linux-apache-mod-ssl-generate-key-csr-crt-file/
CERTIFICATE_DAYS=2000
PASSPHRASE="NO-PASS_TODAY@!"
#Certificate Parameters
SSL_DOMAIN=$1
C="" #Country
ST="" #State
L="" #Location
O="" #Organization
E="security@${SSL_DOMAIN}" #Email Address (this is actually attached to the certificate as part of the CN)
OU="Security Dept"
CN=${SSL_DOMAIN}/emailAddress=${E} #Common Name
KEYDIR_BASE=`pwd`
EXPECTED_ARGS=1
E_BADARGS=65
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: `basename $0` {subdomain.example.com}"
exit $E_BADARGS
fi
mkdir ${SSL_DOMAIN}
cd ${SSL_DOMAIN}
# Generate Private Key
openssl genrsa -des3 -out ${SSL_DOMAIN}.key 4096
# Generate a Certificate Signing Request (CSR)
openssl req -new -subj "/C=$C/ST=$ST/L=$L/CN=$CN/O=$O/OU=$OU" -key ${SSL_DOMAIN}.key -out ${SSL_DOMAIN}.csr
# Generate a Self-Signed SSL Certificate
openssl x509 -req -days ${CERTIFICATE_DAYS} -in ${SSL_DOMAIN}.csr -signkey ${SSL_DOMAIN}.key -out ${SSL_DOMAIN}.crt
# Remove Passphrase
cp ${SSL_DOMAIN}.key ${SSL_DOMAIN}.key.org
openssl rsa -in ${SSL_DOMAIN}.key.org -out ${SSL_DOMAIN}.key
echo
echo
echo Add the following to /etc/apache2/sites-available/${SSL_DOMAIN}
echo
echo "<VirtualHost *:443>"
echo
echo SSLEngine on
echo SSLCertificateFile ${KEYDIR_BASE}/${SSL_DOMAIN}/${SSL_DOMAIN}.crt
echo SSLCertificateKeyFile ${KEYDIR_BASE}/${SSL_DOMAIN}/${SSL_DOMAIN}.key
echo
echo "</VirtualHost>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment