Bash Script to Setup New Site on Ubuntu/Debian
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 ‘createsite.sh‘ and entered the following: (replace the red parts where necessary)
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"
Before running the bash script I needed to update the file permissions like so:
sudo chmod +x createsite.sh
From now on we can create sites instantly by executing the bash script and passing the site name as an argument like so:
sudo ./createsite.sh newsitename