You are here

Apache SSL VirtualHost configurator

Apache's configuration has always irked me slightly...to be more specific, the configuration of SSL on VirtualHosts. Sure, if you want only one VirtualHost to have an equivalent SSL section, you just copy and paste, change the port number, and add a few lines. However, what if you have more? What if you have, say, 40 virtualhosts spread across several files? It could quite easily become tedious and time consuming. And what if you change the original specification of the VirtualHost? You'd need to keep the configuration in sync, or it could easily lead to some annoying errors. This is where this script comes in.

What it does
This script takes a normal Apache configuration file, creates a copy of it, and makes some changes to this new file to enable SSL on the virtualhosts. First, it encapsulates the entire file in a <IfModule mod_ssl.c>. This means that the VirtualHosts will only be active if mod_ssl.c, the module responsible for handling SSL connections, is enabled. This prevents, for example, Apache to generate errors about virtualhosts that are for ports that it hasn't been told to listen on.
It then takes the VirtualHost statements that listen on a particular port (the default is 80) and replaces it with the port for SSL connections (the default is 443). This means that any connection attempts on the SSL port will hit that VirtualHost. It also adds some SSL-related commands to the VirtualHost, so that it is treated as SSL. These specify which certificate file to use for that virtualhost.
Net, it changes the logging path. This means that there is a separate directory for all logging related to SSL connections, errors and so on. This means that the /var/log/ directory stays clean, and easy to navigate.

Source code
If you wish to download the source code, the file is attached.

#! /bin/bash

# Put here the files containing the configuration you want to SSLify
FILES=$(ls)

# Port that the VirtualHosts are currently listening on
defaultport=80

# Port that the SSL connections will be on
sslport=443

# Path to Apache's log directory. If blank, the script will attempt to guess it from /etc/apache2/envvars. If this fails, it is set to "/var/log/apache2"
# logpath=/var/log/apache2

sslopts="\n\tSSLEngine\t\tOn\n\tSSLCertificateFile\t/etc/apache2/ssl/server.crt\n\tSSLCertificateKeyFile\t/etc/apache2/ssl/server.key\n"

# -------- DO NOT MODIFY BELOW HERE -------- #

if [ -z "$logpath" ]; then
. /etc/apache2/envvars
if [ -z "$APACHE_LOG_DIR" ]; then
logpath="$APACHE_LOG_DIR"
else
echo "Unable to determin apache log directory, falling back to /var/log/apache2"
logpath="/var/log/apache2"
fi
fi

portreplace="-e s|\*:$defaultport|\*:$sslport|g"
logreplace="-e s|$logpath|$logpath/ssl|g"
for file in $FILES; do
out=$file-ssl
echo "" > $out
sed $portreplace $logreplace -e "s||$sslopts|g" $file >> $out
echo "" >> $out
done

AttachmentSize
makessl.sh1.09 KB