Get More Out of Apache2 and Linux

June 16th, 2008

If you’re like me, and use Apache 2 now, and you’ve noticed the new sites-available and sites-enabled folders in /etc/apache2 and it made you smile.

Also, I have a folder called Projects in my home folder where I work on a number of sites, and I wanted to have an easy way to edit/create files in my sites-available folder, and re-link them (even a new one) in my sites-enabled folder.

Also, I wanted it to edit /etc/hosts so I can use sub-domains like project.localhost.

Well, here’s a little script I use,

echo Edit and create Apache 2.2 Sites Script
echo Created by Aubrey Island - http://imaginecambio.com/
cd /etc/apache2/sites-available/
echo Creating backup of hosts
sudo cp /etc/hosts /etc/hosts.bak
echo Starting gEdit use the new document to save a new site to sites-available.
sudo gedit /etc/apache2/sites-available/* /etc/hosts | sudo gedit --new-document
echo Removing gEdit backup files, this may produce an error - ignore.
sudo rm /etc/apache2/sites-available/*~
echo Clearing sites-enabled, this may produce an error.
sudo rm /etc/apache2/sites-enabled/*
echo Creating links -> sites-enabled
sudo cp /etc/apache2/sites-available/* /etc/apache2/sites-enabled/ -s -u
echo Restarting Apache2
sudo apache2ctl restart

Here’s what I use to create a new VirtualHost in the new document, if you’re just editing, just ignore the new document.

<VirtualHost *>
ServerName gazouiller.localhost
DocumentRoot "/home/aubrey/Projects/gazouiller"
</VirtualHost>

You can add a localhost sub-domain by going to System -> Administration -> Network in Ubuntu 8.04, and add something like 127.0.0.1 subdomain.localhost. This script will manually edit /etc/hosts for you to edit.

Here’s some screen-shots of the process.

Running the script in terminal.
gEdit opens with all the previous avalable files (sites) and /etc/hosts.
A previously available site on localhost.
Creating a new site.
Saving a new site to sites-available.
Updated links in sites-enabled equal to sites-available.

You can save the file in your home folder, something like .editapachesites and do a sudo ln -s .editapachesites.sh /usr/bin/editapachesites create a launcher in your Menu. (Hasn’t happened successfully for me, yet!)

Leave a Reply