Skip to content

Instantly share code, notes, and snippets.

@athanasius-kircher
Last active September 7, 2017 09:18
Show Gist options
  • Save athanasius-kircher/d9c736699a5c58a140ce1ef086ec7ce3 to your computer and use it in GitHub Desktop.
Save athanasius-kircher/d9c736699a5c58a140ce1ef086ec7ce3 to your computer and use it in GitHub Desktop.
Shell script to add a vhost entry to your apache pointing to current folder, for quick local development setup. Some basic things have to be setuped, as local main domain, with a local certificate. Bases on a brewer apache installation.
#!/bin/bash
#Config this according to your needs, all variables are currently mandatory
#-------------------------------------------------------------------------------
#Your local main dev domain
MAIN_DOMAIN_NAME=".ls-boellmann.local"
#Path to cert apache uses
CERT_PATH="/usr/local/etc/apache2/2.4/crt/boellmann_dev_local.crt"
#Path to key apache uses
KEY_PATH="/usr/local/etc/apache2/2.4/crt/Julius_dev_local_neu.pem"
#Path to the vhost file that the config is appended to
VHOST_CONFIG_PATH="/usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf"
#no comment about this
FAVORITE_BROWSER="Google Chrome"
#Program starts here
#-------------------------------------------------------------------------------
CURRENT_PATH="$(pwd)"
read -p "Please give the name of the subdomain:" SUB_DOMAIN_NAME
read -r -d '' CONFIG<<EOL
<VirtualHost *:80>
ServerName ${SUB_DOMAIN_NAME}${MAIN_DOMAIN_NAME}
Redirect permanent / https://${SUB_DOMAIN_NAME}${MAIN_DOMAIN_NAME}
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "${CURRENT_PATH}"
ServerName ${SUB_DOMAIN_NAME}${MAIN_DOMAIN_NAME}
SSLEngine on
SSLCertificateFile "${CERT_PATH}"
SSLCertificateKeyFile "${KEY_PATH}"
</VirtualHost>
EOL
echo "${CONFIG}"
while true; do
read -p "Do you wish to add this vhost entry?" yn
case $yn in
[Yy]* )
cp -f ${VHOST_CONFIG_PATH} ${VHOST_CONFIG_PATH}.bk
echo "${CONFIG}" >> "${VHOST_CONFIG_PATH}"
sudo apachectl restart
echo "New vhost set and server restarted"
open -a "${FAVORITE_BROWSER}" "https://${SUB_DOMAIN_NAME}${MAIN_DOMAIN_NAME}"
break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment