Skip to content

Instantly share code, notes, and snippets.

@hcooper
Created February 7, 2011 11:03
Show Gist options
  • Save hcooper/814250 to your computer and use it in GitHub Desktop.
Save hcooper/814250 to your computer and use it in GitHub Desktop.
Script to automate creating new apache2 virtual hosts
#! /bin/bash
# Script to automate creating new apache2 virtual hosts.
# Logs go in /var/log/apache2/$SN
# DocRt is /var/www-vhosts/$SN
# SN = shortname -- used for directory names etc.
# DN = domainname -- used only in apache config file
#
# H Cooper - 18/10/06
#
echo -n "Enter full domain name (e.g. example.com): "
read -e DN
echo -n "Enter a unique 'nice' name for the site (e.g. example): "
read -e SN
if [ -d /etc/apache2/sites-available/$SN ]
then echo "Error: site already configure using the name $SN"; exit
fi
echo "Creating Apache Site Configuration File"
echo "<VirtualHost *:80>
ServerName $DN
ServerAlias www.$DN
DocumentRoot /var/www-vhosts/$SN
LogLevel warn
ErrorLog /var/log/apache2/$SN/error.log
CustomLog /var/log/apache2/$SN/access.log combined
#ServerAdmin youradmin@yourdomain
</VirtualHost>" > /etc/apache2/sites-available/$SN
echo "Creating Document Root"
mkdir /var/www-vhosts/$SN
echo "Creating Log File Directory"
mkdir /var/log/apache2/$SN
echo "Enabling Site & Reloading Apache (edit /etc/apache2/sites-available/$SN for more options)"
a2ensite $SN
/etc/init.d/apache2 reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment