Serve.sh – A shell script for serving sites
I am experimenting with using a Linux virtual machine as my web development environment of choice. I store the vm on a removable drive so that I can develop from any location, without having to setup a working environment. Previously I had to check out the repositories, setup a local webserver and I had trouble keeping things working, because every configuration change had to be applied in every location. Now it is all centralized and my life is simpler.
The aim is to make working on projects as easy as possible. I have all projects checked out in a folder called /var/sites. They are mostly PHP projects and because of my shared hosting environment they share a single apache configuration. How can I easily serve them? Having seperate virtualhosts for each project would result in me having to make manual changes on every location again, so this was not the way to go.
Instead I created a simple shell script that creates a symbolic link from the Apache’s webroot to the project I am working on:
#!/bin/bash echo "Available sites:" ls /var/sites echo echo -n "Type site to serve: " read site if [ -z "$site" ] then exit fi sudo rm /var/www sudo ln -s /var/sites/$site /var/www
Serve.sh updated for terminal use
After publishing the previous article about serve.sh – the shell script I created to easily serve websites in development – I made some changes to make it easier to run it from the terminal, for example over ssh. I thought I’d share it with you.
One of the problems with the original script was that it was optimized for a GUI setting – ie. start the script by clicking on it from the desktop. However usually I want to run it from the terminal. I don’t like typing so I added code to make the script available from any directory to the ~/.bashrc:
export PATH=$PATH:~/bin
Then moved it to that path and renamed it to shave another 3 characters off the syntax:
mv ~/Desktop/serve.sh ~/bin/serve
In addition I noticed that prompting which site should be served was just a workaround for commandline arguments, which are now added. It doesn’t check that you entered an argument though. So the new script now requires you to specify which directory you want to serve: to serve the current directory simply run:
serve .
Latest script contents:
#!/bin/bash my_path=`readlink -f $1` sudo rm /var/www sudo ln -s $my_path /var/www echo Now serving $my_path...

