Virtual Hosting


Last month I got a chance to play with an Ubuntu Linux server to set up Virtual Hosting for couple of sites in the office. Most of the time this is achieved through cPanel interface, this server was not supported with GUI tools but I got the root access🙂.

Unfortunately I had only heard and read little about Virtual Hosting. So this was sort of a challenge for me. So first of all I did a bit of searching to get to know the basics of Virtual Hosting. With couple of hours effort I managed to figure out what should be done.

Following is the folder structure I created in the server to hold the site related resources. Sometimes I might have missed some of the important aspect, so strongly recommends you to refer here for a complete explanation.

/home/websites/yoursite.com/
                                                |
                                                |—- html
                                                |—- logs
                                                |—- etc

  • html – place all the resources required for the site.
  • logs – place the access log and error log files.
  • etc – place other resources that are not directly required for the site but you feel like useful for future activities.

Once above is completed next step is to update the apache server about the new Virtual Host. Following block illustrates the necessary configuration for the new Virtual Host. Opens up your favorite text editor and place the below content block and replace the ‘yoursite.com‘ with the sites actual name. Save the file as yoursite.com in the following path: /etc/apache2/sites-available/ and create a symbolic link to /etc/apache2/sites-enabled/.

How to

  • Opens the terminal window
  • Creates a new file having a name like ‘yoursite.com‘ (replace this with your actual site details) using vim editor: sudo vim /etc/apache2/sites-available/yoursite.com
  • Enter into insert mode by pressing i key on your keyboard and place the below code block and replace respective location to reflect to your site’s settings.
  • Next click Esc key on your keyboard and type :wq
  • Finally create a symbolic link to /etc/apache2/sites-enabled/ using following command: ln -s /etc/apache2/sites-available/yoursite.com /etc/apache2/sites-enabled/yoursite.com

<VirtualHost *:80>

ServerAdmin webmaster@localhost

DocumentRoot /home/websites/yoursite.com/html/
ServerName http://www.yoursite.com
ServerAlias yoursite.com

Options Indexes FollowSymLinks
DirectoryIndex index.html, index.php

ErrorLog /home/websites/yoursite.com/logs/error.log

# Possible values include: debug, info, notice, warn, error, crit,alert, emerg.

LogLevel warn

CustomLog /home/websites/yoursite.com/logs/access.log combined
ServerSignature On

</VirtualHost>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: