Skip to content

Instantly share code, notes, and snippets.

@simplyadrian
Last active August 29, 2015 14:21
Show Gist options
  • Save simplyadrian/4c7dae71c697316e3044 to your computer and use it in GitHub Desktop.
Save simplyadrian/4c7dae71c697316e3044 to your computer and use it in GitHub Desktop.
Bash script to force https in apache
#!/bin/bash -x
#
# Linux detection
#
if [ -e /usr/bin/lsb_release ]; then
case `lsb_release -si` in
Ubuntu*) export RS_DISTRO=ubuntu
export RS_BASE_OS=debian
echo 'Ubuntu detected.'
;;
Debian*) export RS_DISTRO=debian
export RS_BASE_OS=debian
echo 'Debian detected.'
;;
CentOS*) export RS_DISTRO=centos
export RS_BASE_OS=redhat
echo 'CentOS detected.'
;;
Fedora*) export RS_DISTRO=redhat
export RS_BASE_OS=redhat
echo 'Fedora detected.'
;;
*) export RS_DISTRO=unknown
export RS_BASE_OS=unknown
;;
esac
fi
echo
#
# Test for a reboot, if this is a reboot just skip this script.
#
if test "$RS_REBOOT" = "true" ; then
echo "Skip Install Postfix on reboot."
logger -t RightScale "Skip Install Postfix on reboot."
exit 0 # Leave with a smile ...
fi
#
# Assign default inputs values if not set/empty
#
: ${APACHE_FRONTEND_PORT:=8000}
#
# Assign local/private variables specific as per distro used
#
if [[ "$RS_DIST" = 'debian' || "$RS_DIST" = 'ubuntu' ]]; then
apache_config_dir=/etc/apache2
apache_user="www-data"
apache_group="www-data"
collectd_config_include_dir=/etc/collectd/conf
else
apache_config_dir=/etc/httpd
apache_user="apache"
apache_group="apache"
collectd_config_include_dir=/etc/collectd.d
fi
# Force HTTPS on the Apache frontend vhost
sed -i 's/RewriteEngine On/RewriteEngine On\n RewriteCond %{SERVER_PORT} !^443$\n RewriteRule ^\/(.*) https:\/\/%{HTTP_HOST}\/$1 [NC,R,L]/' "$apache_config_dir"/sites-enabled/"$APPLICATION_NAME".frontend.http.conf
# Turn off verify host with collectd plugin
sed -i '/<\Plugin apache>/a\
VerifyHost false' "$collectd_config_include_dir"/apache.conf
# Done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment