Now that I've finally got ubuntu set up on my desktop again, I needed to get a LAMP setup going and start developing again. (I found the eeepc, my current dev system, to be a bit small - perfect for travelling though) Installing it all was easy enough, just run sudo apt-get install apache2 php5 mysql-server-5.0 phpmyadmin
Once it was downloaded and installed, the only remaining thing to do was to set up some virtual hosts. I spent a while looking at them on the eee, so I knew how to do it, but it was going to take me a while to get all my sites set up. I decided to use that time a bit more efficiently and write a script that set things up for me. It took about half an hour, and was way more fun than making all the configs by hand.
I'm far from an expert with vhosts, but this works well enough for me. It'll be a huge timesaver whenever I need to set up a new vhost. It creates a folder named sub.domain.tld in the folder you defined as your webroot at the top of the script. The username variable should be the username of the user who needs write access to the folder.
If you've got any corrections/suggestions let me know in the comments so I can fix it up a bit better
Usage: sudo ./addhost sub.domain.tld
#!/bin/bash
WEBROOT='/home/sites/'
USERNAME='michael'
#########################################################
##
#########################################################
NEW_HOSTNAME=$1
# Make sure we have permission to edit all the files we need to
if [ $UID != 0 ]; then
echo "Please run this file as root"
exit
fi
# Work out how many lines are in /etc/hosts
# Default 10
LINES_IN_HOSTS=$(wc -l /etc/hosts | sed s:\ /etc/hosts::)
# If we have 10, it's our first run, so add a heading
if [ $LINES_IN_HOSTS = 10 ]; then
echo "# Automatically Added Hosts" >> /etc/hosts
LINES_IN_HOSTS=$(($LINES_IN_HOSTS + 1))
fi
# There are 8 padding lines, then we need to increment the IP by one
NEXT_HOST_IP_SEGMENT=$(($LINES_IN_HOSTS-9))
NEXT_IP="127.0.$NEXT_HOST_IP_SEGMENT.1"
########################################################
### Now we have everything we need, just start adding
########################################################
# Add our new host to the hosts file
echo "$NEXT_IP $NEW_HOSTNAME" >> /etc/hosts
# Write our new sites-available file
AVAILABLE_LOCATION="/etc/apache2/sites-available/$NEW_HOSTNAME"
echo " <VirtualHost $NEXT_IP>" >> $AVAILABLE_LOCATION
echo " ServerName $NEW_HOSTNAME" >> $AVAILABLE_LOCATION
echo " ServerAdmin $USERNAME@$NEW_HOSTNAME" >> $AVAILABLE_LOCATION
echo " DocumentRoot /home/sites/$NEW_HOSTNAME" >> $AVAILABLE_LOCATION
echo " <Directory /home/sites/$NEW_HOSTNAME>" >> $AVAILABLE_LOCATION
echo " Options -Indexes" >> $AVAILABLE_LOCATION
echo " AllowOverride All" >> $AVAILABLE_LOCATION
echo " Order Allow,Deny" >> $AVAILABLE_LOCATION
echo " Allow From All" >> $AVAILABLE_LOCATION
echo " </Directory>" >> $AVAILABLE_LOCATION
echo " </VirtualHost>" >> $AVAILABLE_LOCATION
# Link it to sites-enabled
ln -s /etc/apache2/sites-available/$NEW_HOSTNAME /etc/apache2/sites-enabled/$NEW_HOSTNAME
# Create our dev folder in /home/sites
mkdir $WEBROOT$NEW_HOSTNAME
chown $USERNAME:$USERNAME $WEBROOT$NEW_HOSTNAME
# Restart apache2
/etc/init.d/apache2 restart