Skip to content

Instantly share code, notes, and snippets.

@devoto13
Last active December 20, 2015 17:19
Show Gist options
  • Save devoto13/6168177 to your computer and use it in GitHub Desktop.
Save devoto13/6168177 to your computer and use it in GitHub Desktop.
Shell-script for creating virtual hosts (with directory structure) for LAMP on Ubuntu.Place somewhere on the PATH, for example at /usr/local/bin.Usage: sudo addvhost <hostname> <branch>
#!/bin/bash
if [ $# -lt 2 ] ; then
echo "Usage: sudo addvhost <hostname> <branch>";
exit 0;
fi
email="admin@example.com"
domain="$1"
branch="$2"
echo "Creating directories..."
mkdir -p /var/www/$domain/$branch
mkdir -p /var/www/$domain/log
echo "Creating vhost..."
echo "127.0.0.1 $domain.$branch www.$domain.$branch" >> /etc/hosts
touch /etc/apache2/sites-available/$domain.$branch
(cat <<VDOC
<VirtualHost *:80>
ServerName $domain.$branch
ServerAlias www.$domain.$branch
ServerAdmin $email
DocumentRoot /var/www/$domain/$branch
ErrorLog /var/www/$domain/log/$branch-error.log
CustomLog /var/www/$domain/log/$branch-access.log combined
</VirtualHost>
VDOC
) > /etc/apache2/sites-available/$domain.$branch
echo "Enabling vhost..."
a2ensite $domain.$branch
echo "Restarting Apache..."
service apache2 restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment