Bash Script to Setup New Site on Ubuntu/Debian

October 16th, 2013 - Posted by Steve Marks to Server Management, Web Development.

We’ve got a development server setup in the office running Ubuntu where we build all new sites before putting them into the public domain. This allows us to a) work quickly and b) have multiple people working on the same site with no danger of files becoming out-of-date.

The only annoying thing we found was having to setup a new site in Apache each time we wanted to begin a new project. It required manually performing about ten steps. This not only took time and required each member in the office to learn how to do it, but also left room for errors to occur. It was all too easy for someone to mess up the process.

As a result I took 10 minutes out this morning and wrote a bash script to perform these steps meaning we can now setup a new site in just a few seconds. Like I say, we use Ubuntu but I believe the same steps could also be performed on Debian, and potentially other platforms.

I created a file called ‘‘ and entered the following: (replace the red parts where necessary)

echo $sitename
sudo mkdir -p /var/www/vhosts/$sitename/httpdocs
echo "Created folder"
sudo chmod -R 755 /var/www/vhosts/$sitename
echo "Performed chmod"
sudo chown -R user:group /var/www/vhosts/$sitename
echo "Performed chown"
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/$sitename
echo "Created hosts file"
sudo sed -i "s/default site name/$sitename/g" /etc/apache2/sites-available/$sitename
echo "Replaced occurrences in hosts file"
sudo a2ensite $sitename
echo "Enabled site"
sudo service apache2 reload
echo "Reloaded Apache"
echo "DONE"

Before running the bash script I needed to update the file permissions like so:

sudo chmod +x

From now on we can create sites instantly by executing the bash script and passing the site name as an argument like so:

sudo ./ newsitename

This entry was posted on Wednesday, October 16th, 2013 at 11:06 am by +Steve Marks and is filed under Server Management, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first