Simple Is Good!

How To Web Dev

Ubuntu 12.04, Apache Virtual Sites Shell Script

The Problem (Besides being lazy)

Whoa, long title there. I find that I am creating virtual sites all the time on my local development machine, and this is not a particularly hard process, but kind of tedious when setting up multiple sites.  Well, since I moved over to running a linux dev machine I decided that I could probably write a shell script and have it do all the for me.  So what was my process before that was so hard?  Well I am lazy but here it is:

  1. Create the virtual host file in apache which means setting the document root, server name and server alias.
  2. Create the folder that the site is going to live at, /var/www/sitename/
  3. Restart Apache (sudo service restart apache)
  4. Create the database where the site data is going to exist (rarely do I have a site without a database.)

I know, not that much and not that hard, but it does get old, and don’t forget I am lazy!

The Solution – Shell Script

So I am sure that there is a way to do this same thing in windows, I just never took the time to learn.  I went ahead and decided to create a shell script that does all those steps above with one command from me via terminal.  I know you could do something very similar on OSx.  So here is my script and then I will walk you through it if you don’t just copy paste and leave my site!

The command:

sudo install.sh example_com client_12_sites example.local

Thats the command, here is what gets executed:

#!/bin/bash
cat > /etc/apache2/sites-enabled/$1 << EOF
<VirtualHost *:80>
     DocumentRoot /var/www/$2/$1
     ServerName $3
     ServerAlias*.$3
</VirtualHost>
EOF
 
mkdir /var/www/$2/$1
 
service apache2 restart
 
sql="CREATE DATABASE ${2}_${1}"
 
mysql -h localhost -u root -p***** -Bse "${sql};"

In Detail

So here is the breakdown for those who are interested. First the command

install.sh: This is just the name of the the shell script.

example_com: This is the name of the live site domain with the period replaced with an underscore.

client_12_sites: This is just for how I organize things, I have many clients who have more then one site, so they get put in a sub-directory.

example.local: This part is what I want my local domain name to be to access this site.

No for the actual script:

 
First we need to create the virtual host file with apache. $1 is the first argument that is being passed to the script which is the live sites domain name with the period replaced with and underscore.

#!/bin/bash
cat &gt; /etc/apache2/sites-enabled/$1 &lt;&lt; EOF

Next we need to write the content to the file, again $1 = example_com, $2 = client_12_sites and finally $3 = local domain example.local.

<VirtualHost *:80>
     DocumentRoot /var/www/$2/$1
     ServerName $3
     ServerAlias*.$3
</VirtualHost>
EOF

Now we need to make sure that the folder the site is going to live at is created, if we don’t when apache restarts it will not be happy. This is just simple directory creation.

mkdir /var/www/$2/$1

Now that we have the virual host and and our folder, lets restart apache to recognize them.

service apache2 restart

Finally we just need to create the database for the site. This was actually the part that was the most difficult, as I had never run mysql cli from a shell script. Fist thing that should be noted is the naming convention I use for databases, client_domain_com. This keeps everything easy to keep track of. So first I created mysql and then run it. Notice that the password starts immediatly aft -p if this is not done then it will think your password is the name of a database, nothing good will come of that I assure you.

sql="CREATE DATABASE ${2}_${1}"
 
mysql -h localhost -u root -p***** -Bse "${sql};"

And now if everything worked correctly you should have a functioning site up and running. This I think will become a bit more advanced as time goes on, git repos could be added, it should check to make sure files don’t already exist etc.

July 22, 2013 at 11:17 am | Apache, Linux, MySQL | No comment

Leave a Reply

Your email address will not be published. Required fields are marked *