Setting up an apache vhost is a boring task. This is why most of the times, I use the h*tp://localhost/project subdirectory for my development work.
This is perfectly ok for most cases, however in some occasions you may wish to create a virtual host so that your project is accessible like: h*tp://project
To do this, you need to create a file in the the sites-available directory of your apache instance, set up a link in the sites-enabled directory, add an entry in the /etc/hosts file and finally restart apache.
Too much work. Fortunately we can automate it:
Create a template file (you can copy from an old project) in the sites-available directory. Replace every reference of your old project name and directory name with 'TEMPLATE'. Name this template file also 'TEMPLATE'
Then, create a shell script and add the following lines:
#!/bin/sh
sed -u s/TEMPLATE/"$1"/g /etc/apache2/sites-available/TEMPLATE > /etc/apache2/sites-available/$1
ln -s /etc/apache2/sites-available/$1 /etc/apache2/sites-enabled/$1
echo "127.0.0.1 $1" >> /etc/hosts
service apache2 restart
I named my script 'create_vhost.sh'.
Make the script executable and run it with the name of your project as an argument:
sudo ./create_vhost.sh new_project
and your project will be magically accessible from http://new_project !
Did you like this post? Drop me a line in the comments below