Set Up Name Based Virtual Hosting In Linux


NameVirtualHost
With the default configuration you are only serving up one site, and that site is based on your IP address. What I’m setting up is name-based virtual hosting, meaning the Apache server will serve specific content based on the domain name requested. In this way a single server can host multiple sites, and serve up unique content based on the domain requested.

My preferred method of using name based virtual hosting is creating a seperate file for each domain. These can all be done within one file, but I’ll be creating a new file for each site.

First 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

The above should be your public facing IP address (assuming you’re creating a public site), and port is generally port 80 by default. After this we’ll create the base configuration for your virtual hosts. Debian and Ubuntu use /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ directories for defining virtual hosting. One nice thing about this is that you can have more sites “available” than you have “enabled”, meaning not everything configured is actually live and listening. This is nice to quickly disable a site for whatever reason.

I like to create unique files for each of my domains within the /etc/apache2/sites-available/ folder. For example I have a file called “ubuntu-tutorials.com” in that directory, with the following contents:

For creating new file /etc/apache2/sites-available/ here

paste this command: touch /path/filename
ServerName ubuntu-tutorials.com
ServerAlias http://www.ubuntu-tutorials.com
ServerAdmin christer.edwards@ubuntu.com
DocumentRoot /var/www/ubuntu-tutorials.com/html

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 this command

sudo a2ensite alpha.opencart.com

OR

creating a symbolic link from one folder to the next.

cd /etc/apache2/sites-enabled/
ln -s ../sites-available/ubuntu-tutorials.com .

This site is now available (as in configured) and enabled (as in listening) once we restart the apache service:

sudo /etc/init.d/apache2 restart

Testing

To test your configuration you can, temporarily, configure your /etc/hosts file to point the domain to your IP address and see if your server loads up the correct site. This is only needed if the hostname or domain name does not already resolve to your IP address. Editing the /etc/hosts by adding the following line:

ip.address domain.tld

Open your browser, try to access domain.tld and see if it loads the contents from your local DocumentRoot (from the configuration above). You might want to drop a file in the DocumentRoot to verify its pulling your local content.

cd /var/www/ubuntu-tutorials.com/html
echo “Hello World” > index.html

Conclusion

I hope I didn’t miss anything here. One of the main purposes of this writeup is to document what I did to setup my server. I do it so rarely I don’t always remember all the steps when I need to. If this helps you setup name based virtual hosting, great. Leave a comment and let me know. If I forgot anything critical please also let me know so I can update the contents.

Tagged with:
Posted in Linux

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

Follow me on Twitter

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 442 other followers

%d bloggers like this: