Apache virtual hosts

When you first set up a server with multiple domains coming in, redirecting your incoming traffic to the correct location can be somewhat puzzling. Thankfully, Apache makes it rather simple to set up redirecting incoming requests based on the incoming URL.

For the purpose of this tutorial, we will assume that you are running Ubuntu and have already correctly pointed your DNS records to your server. You have created a new website called Inkhorn Tutorials under your web directory /var/www resulting in the following:

Path to new website: /var/www/inkhornTutorials

We will also assume that this website should be tied to the fictitious domain www.inkhorntutorials.com

Having established the fundamental assumptions, it’s time to get to the core of this tutorial, setting up a virtual host under Apache. To start off, navigate to your apache directory:

Path to Apache2: cd /etc/apache2/

The directory you are interested in is /sites-available and later we will have a look at /sites-enabled.

Navigate into the /sites-available directory so that we can create our new virtual host file.

Make a new virtual host: sudo nano ./inkhorntutorials.com

Now within that file we are going to place the following code:

File inkhorntutorials.com file contents:

<VirtualHost *:80>

ServerAdmin your@email.com

ServerName inkhorntutorials.com
DocumentRoot /var/www/inkhornTutorials/
ServerAlias www.inkhorntutorials.com

<Directory />

Options FollowSymLinks
AllowOverride All

</Directory>

<Directory /var/www/inkhornTutorials/>

Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Now that that’s done we need to save the file and enable the virtual host in Apache.

Save the file (in vi): :wq

The above snippet also closes vi so you should be back at the console depending on how you are running through this tutorial.

With the console open, type in the following:

Enable virtual host: a2enmod inkhorntutorials.com

Now for the sake of being thorough, we will restart Apache

Restart Apache: sudo service apache2 restart

You are good to go. Your site is now enabled and listed in your /etc/apache2/sites-enabled directory.

Comments are closed.