Setting up a virtual host in Apache

Setting up virtual hosts in Apache is a fairly straight forward process, and these steps will help you get there.

Find this section of code in the httpd.conf file and uncomment the virtual hosts configuration file

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Open the host file. Details are below.

File: hosts (there is no file extension)
Directory: C:\WINDOWS\system32\drivers\etc

Make the necessary entries for the virtual hosts that you will be running as shown in the example below.	localhost	application	brentknigge

In the httpd-vhosts.conf we need to set up our localhost the same way that we did in httpd.conf.
<VirtualHost *:80>
    ServerAdmin <a href="mailto:brent@localhost">brent@localhost</a>
    DocumentRoot "C:/MyWebpages"
    ServerName localhost
    ErrorLog logs/localhost_error.log

Next we need to set up application. This virtual host will be used to store applications, e.g. phpMyAdmin, SQLite Manager, PHPDocumentor etc.

<VirtualHost *:80>
    ServerAdmin <a href="mailto:brent@localhost">brent@localhost</a>
    DocumentRoot "C:/MyWebApplications"
    ServerName application
    ErrorLog logs/application_error.log
    <Directory "C:/MyWebApplications">
        Require ip

Note that the server name application is also the same name that was added to the hosts file. What this set up means is that I can now navigate to a webpage on my local machine as http://application/phpMyAdmin. Notice that the directory for localhost is separate from application which means that I can keep web specific applications like phpMyAdmin separate from the websites that I'm working on.

Continue following the step above for each of the virtual hosts that you have created. Just remember to restart Apache!

From Apache 2.4 onwards, a new Require directive has replaced the Order allow,deny. You can read more about this in the upgrade notes.


Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.