Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Caffe1neAdd1ct/0a708c171b5ad67a83f6 to your computer and use it in GitHub Desktop.
Save Caffe1neAdd1ct/0a708c171b5ad67a83f6 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ ! "$#" -eq 1 ]; then
echo "Usage: sudo $0 VIRTUAL_HOST_NAME"
exit
fi
if [ ! $(whoami) = "root" ]; then
echo "You require root privilages for server maintanence, use sudo?"
exit
fi
APACHE_SITES_AVAILABLE_PATH="/etc/apache2/sites-available/"
APACHE_LOG_PATH="/var/log/apache2/"
APACHE_DOCUMENT_ROOT_BASE="/var/www/"
VIRTUAL_HOST_NAME=$1
HOSTNAME=$(hostname)
APACHE_USER="www-data"
DOCUMENT_ROOT_OWNER="dev"
if [[ -d $APACHE_SITES_AVAILABLE_PATH$VIRTUAL_HOST_NAME ]] || [[ -f $APACHE_SITES_AVAILABLE_PATH$VIRTUAL_HOST_NAME ]]; then
read -p "$APACHE_SITES_AVAILABLE_PATH$VIRTUAL_HOST_NAME already exists. Would you like to overwrite it? [Y/n] " yn
case $yn in
[Nn]* ) echo "Cancelled."; exit;;
esac
fi
VHOST_CONFIG=$( cat <<VHOST
<VirtualHost *:80>
ServerName $VIRTUAL_HOST_NAME.$HOSTNAME
ServerAdmin webmaster@localhost
DocumentRoot /var/www/$VIRTUAL_HOST_NAME
<Directory /var/www/$VIRTUAL_HOST_NAME>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog $APACHE_LOG_PATH$VIRTUAL_HOST_NAME.error.log
CustomLog $APACHE_LOG_PATH$VIRTUAL_HOST_NAME.access.log combined
</VirtualHost>
VHOST
)
printf "$VHOST_CONFIG" > "$APACHE_SITES_AVAILABLE_PATH$VIRTUAL_HOST_NAME"
touch "$APACHE_LOG_PATH$VIRTUAL_HOST_NAME.error.log"
touch "$APACHE_LOG_PATH$VIRTUAL_HOST_NAME.access.log"
chown -R $APACHE_USER:$APACHE_USER $APACHE_LOG_PATH
mkdir -p "$APACHE_DOCUMENT_ROOT_BASE$VIRTUAL_HOST_NAME"
chown -R $DOCUMENT_ROOT_OWNER:$DOCUMENT_ROOT_OWNER $APACHE_DOCUMENT_ROOT_BASE$VIRTUAL_HOST_NAME
read -p "Virtual Host $VIRTUAL_HOST_NAME created. Would you like to enable it? [Y/n] " yn
case $yn in
[Nn]* ) exit;;
esac
a2ensite $VIRTUAL_HOST_NAME > /dev/null
service apache2 reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment