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:
- Asks for the new domain name
- Creates a new folder in the user folder with the domain name (/home/user/DOMAIN.COM)
- Creates a basic index.html file in the new domain folder that displays the name of the domain when accessed.
- Rolls 2d6 for Hacker Shield, to prevent magic-based attacks against your website. (planned for version 0.2)
- Generates a vanilla Apache vhost config file spelling out how Apache should handle requests for that domain.
- Generates an access log and an error log
- 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.
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.
Thanks, bradoaks. You’re absolutely right; I had forgotten about that options. I’ll amend the post to include that method.
Also if you run
chmod +x siteupthen edit your ~/.bash_profile to includeexport 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/siteupwill run the script, no need for the leading .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?
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.
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.
Maybe i am a little bit late, but what about a2ensite ….
-dieter
I prefer a /home/d/o/domain.com structure. Allows for massive growth.
Yea screw yourself.
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.