Configuring Virtual Host

Configure Virtual Hosting Link

There are different ways to set up Virtual Hosts, however we recommend the method below.

By default, Apache listens on all IP addresses available to it. We must configure it to listen only on addresses we specify. Even if you only have one IP, it is still a good idea to tell Apache what IP address to listen on in case you decide to add more.

Begin by modifying the NameVirtualHost entry in /etc/apache2/ports.conf so that the line reads:

File excerpt: /etc/apache2/ports.conf

NameVirtualHost 12.34.56.78:80

Be sure to replace “12.34.56.78” with your Linode’s public IP address.

Now, modify the default site’s VirtualHost in the /etc/apache2/sites-available/default file so that the entry reads:

File excerpt: /etc/apache2/sites-available/default

Configure Virtual Hosts Link

You can create as many virtual hosting files as you need to support the domains that you want to host with your Linode. First, create a file in the /etc/apache2/sites-available/ directory for each virtual host that you want to set up. Name each file with the domain for which you want to provide virtual hosting. See the following example configurations for the hypothetical “ducklington.org” and “bucknell.net” domains.

File: /etc/apache2/sites-available/duckington.org

ServerAdmin squire@ducklington.org
ServerName ducklington.org
ServerAlias http://www.ducklington.org
DocumentRoot /srv/www/ducklington.org/public_html/
ErrorLog /srv/www/ducklington.org/logs/error.log
CustomLog /srv/www/ducklington.org/logs/access.log combined

File: /etc/apache2/sites-available/bucknell.net

ServerAdmin squire@bucknell.net
ServerName bucknell.net
ServerAlias http://www.bucknell.net
DocumentRoot /srv/www/bucknell.net/public_html/
ErrorLog /srv/www/bucknell.net/logs/error.log
CustomLog /srv/www/bucknell.net/logs/access.log combined

Notes regarding this example configuration:

* All of the files for the sites that you host will be located in directories that exist underneath /srv/www You can symbolically link these directories into other locations if you need them to exist in other places.
* ErrorLog and CustomLog entries are suggested for more fine-grained logging, but are not required. If they are defined (as shown above), the logs directories must be created before you restart Apache.

Before you can use the above configuration you’ll need to create the specified directories. For the above configuration, you can do this with the following commands:

mkdir -p /srv/www/ducklington.org/public_html
mkdir -p /srv/www/ducklington.org/logs
mkdir -p /srv/www/bucknell.net/public_html
mkdir -p /srv/www/bucknell.net/logs

After you’ve set up your virtual hosts, issue the following commands:

a2ensite ducklington.org
a2ensite bucknell.net

This command symbolically links your virtual host file from sites-available to the sites-enabled directory. Finally, before you can access your sites you must reload Apache with the following command:

/etc/init.d/apache2 reload

Should you ever need to disable a site, you can use the a2dissite command. For example, if you wanted to disable the duckington.org site, you would issue the following command:

a2dissite ducklington.org

The a2dissite command does the opposite of the a2ensite command.

Remember, after enabling, disabling, or modifying any part of your Apache configuration you will need to reload the Apache configuration again with the /etc/init.d/apache2 reload command.

Assuming that you have configured the DNS for your domain to point to your Linode’s IP address, Virtual hosting for your domain should now work.

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s