Saturday, August 24, 2013

Setting Up Name Based Virtual Hosting in Apache on Ubuntu

Apache is by far the most popluar web server out there. Installing  apache on a Ubuntu machine is straightfoward. However, whats the use of it if we cant make it run multiple sites.

Virtual Hosts are used to run more than one site off of a single IP address. If you want to be able to handle more than one domain with one web server, you'll need to set up a virtual host for each. Here I will show you how to work with Virtual Hosts by setting up 2 different sites on a single apache installation using virtual hosts.

As an example, let us assume we are setting up 2 sites www.testwadt.com and www.testwadt.net

Create 2 separate folders where we will place the files which will serve the 2 sites. Let do this in the /var/www folder

mkdir /var/www/testwadt_com
mkdir /var/www/testwadt_net

Now for each of the 2 domains, put the files in the respective folders created for the same

Also create a log folder in each of the 2 folder created above
mkdir /var/www/testwadt_com/log
mkdir /var/www/testwadt_net/log


Now we need to define to Apache that we’re using name based virtual hosting instead of IP based. You can append the following line to your /etc/apache2/apache2.conf to define this:
NameVirtualHost ip.address:port

typically this would be
NameVirtualHost :80

The next step is to set up Virtual Hosts. Each virtual host needs its own file in the /etc/apache2/sites-available/ directory.
This allows for a clear and specific per-site configuration.

So lets create files for the two sites we wish to host: testwadt.com and testwadt.net

First lets create a file testwadt.com in /etc/apache2/sites-available

vi /etc/apache2/sites-available/testwadt.com

In the file place the following:

# domain: testwadt.com
# public: /var/www/testwadt_com

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin youremail@youremailprovider
  ServerName  www.testwadt.com
  ServerAlias testwadt.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/testwadt_com

  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/testwadt_com/log/error.log
  CustomLog /var/www/testwadt_com/log/access.log combined
</VirtualHost>


Now lets create the 2nd file testwadt.net in /etc/apache2/sites-available

vi /etc/apache2/sites-available/testwadt.net

In the file place the following:

# domain: testwadt.net
# public: /var/www/testwadt_net

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin youremail@youremailprovider
  ServerName  www.testwadt.net
  ServerAlias testwadt.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/testwadt_net

  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/testwadt_net/log/error.log
  CustomLog /var/www/testwadt_net/log/access.log combined
</VirtualHost>

What these settings do is as follows:

ServerName listens for requests asking for a certain domain
ServerAlias defines any additional domains that should match
ServerAdmin is the contact for the site
DocumentRoot is the path to the content for that site


Now that this file is created in the /etc/apache2/sites-available/ folder we’re just about ready to start, but we need to enable it. We can do that by creating a symbolic link in /etc/apache2/sites-enabled/

This is done by calling the a2ensite command

a2ensite testwadt.com
a2ensite testwadt.net

Finally, restart the Apache server to initialize all the changes

/etc/init.d/apache2 restart


Files that configure virtual hosts should be located in the /etc/apache2/sites-available/ directory.

The files should be symbolically linked to sites-enabled/ with the a2ensite tool.

No comments:

Post a Comment