Plate Up! Bash Script for Apache VHost Setup

In the sisyphian marathon of blowing up and rebuilding my server, I’ve had to go through the process of reconfiguring my virtual hosts for Apache2 several times. Well, that gets a bit tedious. How can my hands do the very important jobs of holding a beer up to my lips whilst scratching my man candy if they have to keep typing “sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/newdomain.com” over and over again?

bash it!

Fear not, my young linux adventurers. There IS a better way! Pop that cold beer, pull up some sweats, and feast your eyes on my hand dandy little bash script for automatically setting up yon website. Here’s what it does, in a nutshell:

  1. Asks for the new domain name
  2. Creates a new folder in the user folder with the domain name (/home/user/DOMAIN.COM)
  3. Creates a basic index.html file in the new domain folder that displays the name of the domain when accessed.
  4. Rolls 2d6 for Hacker Shield, to prevent magic-based attacks against your website. (planned for version 0.2)
  5. Generates a vanilla Apache vhost config file spelling out how Apache should handle requests for that domain.
  6. Generates an access log and an error log
  7. Generates a cgi-bin folder within the DOMAIN.COM folder

The script assumes you have Apache 2.2+ installed.

The non-compliance disclosure

Keep in mind that this script was written by me, for me, to help save me some effort. Because of that, it uses some non-standard locations for things. Why? Because I likes my shit where I likes my shit, that’s why!

I like having my apache vhost config files in my user folder, so that I don’t have to hassle with permissions. You can do the same thing with these two commands. First, make yourself a few new directories:

mkdir -p ~/www-config/sites-available
Then, link the apache site config folder to your new folder. Make sure this all goes on one line:

sudo ln -s /home/USERNAME/www-config/sites-available /etc/apache2/sites-available/

So where are things placed with this script?

config:
/home/USERNAME/www-config/sites-available/DOMAIN.COM

website documents:
/home/USERNAME/DOMAINNAME.COM

index page:
/home/USERNAME/DOMAIN.COM/index.html

access log:
/home/USERNAME/logs/DOMAINNAME.COM/access.log

error log:
/home/USERNAME/logs/DOMAINNAME.COM/error.log

cgi-bin:
/home/USERNAME/DOMAIN.COM/cgi-bin

The Script

Here ’tis. Click here to view or download a .txt file with the script.

http://commandlineidiot.com/scripts/siteup.txt

To use it, do the following:

mkdir ~/bin
cd ~/bin
wget http://commandlineidiot.com/scripts/siteup.txt
mv siteup.txt siteup
chmod 755 siteup

Now, to invoke the script, type the following:
. ~/bin/siteup

If you want to find out more about bash scripts, including how to handle permissions and setting paths so that your scripts can be called by name, I highly recommend the following tutorial by William Shotts Jr. at LinuxCommands.org: Writing Shell Scripts

Questions? Comments? Feck Off! I’m too busy drinking this cold beer and fiddling my diddly.

9 thoughts on “Plate Up! Bash Script for Apache VHost Setup

  1. just a quick note that you can use mkdir’s -p option and it will create any of the parent directories needed in order to complete your request:
    mkdir -p ~/www-config/sites-available
    Thanks for writing about your experiences with the command line.

  2. Also if you run chmod +x siteup then edit your ~/.bash_profile to include export PATH="$PATH:~/bin" it saves you having to use ~/bin/siteup to run the script, a simple siteup will suffice.

    Even if you don’t do that, then a simple ~/bin/siteup will run the script, no need for the leading .

  3. Thanks for saving my time by writing it up.
    Btw, I would like to ask, why not placing the DocumentRoot in /var/www/*** and leave only the logs in home directory and a2ensite files?

  4. Stas, it’s purely preference on my part. I connect via ssh directly to my user folder, and I don’t like to go searching around ot other folders to find my data. Since web server is the only thing my machine does, and since I am the only user, it’s mentally more convenient for me this way.

  5. nog, thanks. I stole this and am strutting around town acting like it’s mine. All the ladies are REALLY into it.
    Thanks for doing the work for me.

  6. Hi,

    Is your ln command logic backwards.

    sudo ln -s /etc/apache2/sites-available /home/$USER/www-config

    I used the following command to execute the necessary link to the apache2 sites-available directory.

    You can also ln your site configuration file that you hold in ~/www-config/${SITENAME}.conf then link it to the sites-available folder.

Leave a Reply