Skip to content

Instantly share code, notes, and snippets.

@AmmarCodes
Created October 7, 2013 10:26
Show Gist options
  • Save AmmarCodes/6865708 to your computer and use it in GitHub Desktop.
Save AmmarCodes/6865708 to your computer and use it in GitHub Desktop.
Help create Apache virtual host on Ubuntu
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if [ $# -lt 2 ]; then
echo "No arguments supplied!"
echo "Usage: sudo scriptname directory_name server_name"
exit 1
fi
cat > /etc/apache2/sites-enabled/$1 <<EOF
<VirtualHost *:80>
DocumentRoot /var/www/$1
ServerName $2
ServerAlias *.$2
</VirtualHost>
EOF
if [ ! -d "$1" ]; then
mkdir /var/www/$1
fi
cat >> /etc/hosts <<EOF
127.0.0.1 $2
127.0.0.1 www.$2
EOF
service apache2 restart
sql="CREATE DATABASE ${1}"
mysql -h localhost -u root -Bse "${sql};"
#xdg-open http://$2/
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment