Skip to content

Instantly share code, notes, and snippets.

@mtrl
Created August 26, 2011 11:29
Show Gist options
  • Save mtrl/1173229 to your computer and use it in GitHub Desktop.
Save mtrl/1173229 to your computer and use it in GitHub Desktop.
Create apache virtual host on Ubuntu and create entry in hosts file
#! /bin/sh
if [ "$(id -u)" -ne "0" ] ; then
echo "This script must be run as root"
else
if [ -z "$2" ] ; then
echo Usage ./create-vhost.sh [full path to site root directory] [url of vhost without http://]
exit 1;
else
DIRECTORY=$1;
DOMAINNAME=$2;
# Create directory
mkdir -p $DIRECTORY
# Set permissions
chmod 0755 $DIRECTORY -R
chown www-data:www-data $DIRECTORY -R
# Create vhosts file
echo "NameVirtualHost *
<VirtualHost *>
ServerName $DOMAINNAME
DocumentRoot $DIRECTORY
</VirtualHost>" > /etc/apache2/sites-available/$DOMAINNAME
# Enable site
a2ensite $DOMAINNAME
# Restart apache
apache2ctl restart
# Add to hosts file
echo "# $DOMAINNAME" >> /etc/hosts
echo "127.0.0.1 $DOMAINNAME" >> /etc/hosts
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment