Introduction: Installing a New Virtual Host in the Apache Web Server

Picture of Installing a New Virtual Host in the Apache Web Server

The purpose of this tutorial is to walk through the process of configuring and initiating a new Apache web server virtual host. A virtual host is a "profile" that detects which DNS host (e.g., www.MyOtherhostname.com) is being called for at any given IP address. It is possible to narrow this further by only coupling IP addresses and hostnames in the virtual host configuration, but I will skip that and assume that every IP address the server has is permitted to access the virtual host.

This instructable was made specifically with a Debian server running Apache 2.2.x.

Step 1: Login and Get to the Right Place

Picture of Login and Get to the Right Place

First, log in and change directories to your configuration directory. In most sane servers, this means logging in as a user with superuser privileges, and going somewhere in /etc/

$ ssh me@myserver.com
Password: exciting_password
Welcome!
~$ cd /etc/apache2/sites-available

Step 2: Create the Virtualhost From a Default Template

Picture of Create the Virtualhost From a Default Template

Usually I keep a default file around, which I copy to a clipboard and paste for use. From that default file, you can edit the specifics. Below is a reasonable default file you can refer to, which assigns the document to a Drupal directory:

$ pico MyOtherHostname.com

<VirtualHost *:80>
ServerAdmin Admin@server.com
DocumentRoot /home/web/drupal/drupal-6
ServerName www.MyOtherHostname.com
ServerAlias MyOtherHostname.com *.MyOtherHostname.com
RewriteEngine On
RewriteOptions inherit
CustomLog /var/log/apache2/MyOtherHostname.log combined
</VirtualHost>

Needless to say, you can make whatever customizations you wish according to the information found in the Apache 2.2 virtual host documentation.

Step 3: Enable the Site and Restart Your Server

Picture of Enable the Site and Restart Your Server

Now it's time to enable the site and restart the server. Debian has a few cool server management tricks here:

First, let's enable the site:

$ sudo a2ensite MyOtherHostname.com
Site MyOtherHostname.com installed; run /etc/init.d/apache2 reload to enable.

$ sudo /etc/init.d/apache2 reload
Reloading web server config.... PID#

And now you should be able to access the site so long as the DNS server points it to your server.

For Drupal sites, I often take this opportunity to add the cron.php file to my crontab before I forget:

$ sudo pico /etc/cron.d/drupal
2 0,5,10,15,20 * * 1-6 nobody curl --silent http://MyOtherHostname.com/cron.php

That's it! Congratulations!

tarek : )

Comments

admin (author)2008-10-27

Hey, this is a great instructable and is very informative. Just one thing is missing... pictures! It really helps a lot when trying to follow directions so you should consider taking some photographs. Once you do that and leave me a message when you have so that we can publish your work. Thanks! Thanks for the cool instructable and we hope to publish this soon!

Tarek_Loubani (author)admin2008-10-27

I'm actually not sure how photos would aid this particular instructable, except to make it a bit more pretty. Anyhow, pretty pictures inserted! : ) tarek : )

Computothought (author)2011-07-30

ubuntu 10.04 has some gotchas with virtual hosts. Also had to change 000-default.

uranus_b_hurtin (author)2010-02-05

FYI -- Apache 2.2 has a dummy vhosts-conf file in the /conf/extras directory that you can replicate and call from httpd.conf.

The design minimizes the edit work in httpd.conf.

About This Instructable

2,316views

10favorites

License:

More by Tarek_Loubani:Adding a virtual Drupal site to a fully segregated Drupal FarmInstalling a new virtual host in the Apache Web server
Add instructable to: