Wesley Todd

Code, Clay & Pixels
I am an artist and these are my media.
Some of the things I make are useful {Websites, Mugs}.
Others are beautiful {Vases, Pictures, Sculptures}.
Hopefully a few fit into both categories.

Setting up a Virtual Host on Ubuntu

I recently have been working on learning more about server administration and optimization. As a part of this I have been tearing apart install after install of Ubuntu Server. One of the most time consuming thing about this is getting my test sites back up and running. So, as any good hacker does, I got lazy and automated it. I found a nice little starter script from Command Line Idiot and then took some liberties in modifying it.

GitHub Repo

Setting Up the Script

To use this script you can just grab it from the GitHub page:

# wget https://raw.github.com/wesleytodd/Ubuntu-vHost-Setup-Script/master/newsite

The first thing you should do is edit the settings at the top of the file. The options in this file are for global settings that will be a foundation for all the sites on your server. The domain specific settings (aka. domain name) will be set each time you run the script. This is the setting section:

# =======================
#	Settings
# =======================

sysadmin="wes@wesleytodd.com"
droot="/var/www/vhosts/"
webroot="httpdocs"
permissions="755"

Be sure to change the sysadmin to be your email.

The droot variable will be added as a prefix to the domain name provided upon running the script and then added on the end of that will be the webroot variable. This concatenation will then be given as the document root. Example:

droot="/var/www/vhosts/"
webroot="httpdocs"

Will turn into:

DocumentRoot /var/www/vhosts/{DOMAIN NAME}/httpdocs/

The script will set the file ownership of the DocumentRoot recursively to the Ubuntu standard of www-data:www-data and you can also define a default file permissions setting (permissions="755").

# ======================
# Set Permissions
# ======================
chown -R www-data:www-data $droot$dname/$webroot
chmod $permissions -R $droot$dname/$webroot

Running The Script

The script is very simple to run, just remember you have to run this as root. Just type:

# sh newsite

You will then receive a prompt for the domain name (ex: wesleytodd.com) and press enter. After the script runs you will see some output like this:

***  Ubutnu Virtual Host Setup  ***
==> Enter new domain name (domain.com): wesleytodd.com            
Setting up files for wesleytodd.com
mkdir: created directory `/var/www/vhosts/wesleytodd.com'
mkdir: created directory `/var/www/vhosts/wesleytodd.com/httpdocs'
mkdir: created directory `/var/www/vhosts/wesleytodd.com/cgi-bin'
mkdir: created directory `/var/www/vhosts/wesleytodd.com/logs/'
created /var/www/vhosts/wesleytodd.com/logs/access.log
created /var/www/vhosts/wesleytodd.com/logs/error.log
created /var/www/vhosts/wesleytodd.com/httpdocs/index.html
created /etc/apache2/sites-available/wesleytodd.com
Enabling site wesleytodd.com.
To activate the new configuration, you need to run:
  service apache2 reload
 * Reloading web server config apache2                                                                                                                 [ OK ] 
***  Finished setting up files for wesleytodd.com.  ***

Now when you open up your browser and visit the URL, you should see a single line: welcome to {YOUR DOMAIN}. And that is all there is to it.

Adding a BASH Alias

One thing you might also want to do is add an alias for this script to your ~/.bachrc so that you can easily run this at any time without typing the full path to the file. To do this type:

# vi ~/.bashrc

And then somewhere in the file add the line:

alias newsite="sh ~/scripts/newsite"

Once you have saved this file and logged out and back in, you will just have to type newsite for the script to run.

What People Are Saying...

  1. Arun says:

    The httpdocs cdreitory for my virtual host was working fine, but subdirectories were not. I tried adding separate Directory blocks in the vhosts.conf, but that didn’t work. I finally changed the include_path line to:php_admin_value include_path .:/usr/share/pear/ I then added:php_admin_value safe_mode_include_dir /usr/share/pear/ After that, it began to work in subdirectories. Try just adding the extra line first. The other change may not be necessary.

Join The Discussion...