Before we install WordPress we need to configure Apache further. We can do this from the WAMP control menu by selecting the Apache configuration file
This will open a text editor containing the Apache configuration file.
One very nice feature of Apache is its ability to create virtual hosts. Currently we have one webserver running, with one document root. Virtual hosts allow us to add any number of virtual webservers, each with their own document root. They are virtual because they all run under the same instance of Apache – no additional software or ports are required. Apache uses the URL to determine which server a request is for, and changes the document root accordingly.
Why would we be interested in this? Because it allows us to host several websites on one computer. Commercial web hosting companies use the same technique. We can also host several websites on our own computer, if required. Also we could host several copies of the same website, possibly running different versions of WordPress or with different themes.
Creating a virtual host is a two-step process.
Step 1 – Think of a name
We need a unique name for our virtual host. Ideally this name should not already exist on the internet. A nice naming guide is to take your existing domain name and replace the www with local. For example,
We need a name so that our Apache webserver can distinguish which virtual host to send the request to. Before we reach that point, we need to tell Windows about our new name.
Edit the file
C:windowssystem32driversetchosts and add:
Naturally you should change
local.urbangiraffe.com to your chosen name. You can add as many names here as you want virtual hosts.
When we browse to a website, our computer takes the URL and looks up an address for the domain using technology known as DNS – Dynamic Naming System. Usually this involves contacting big address databases on the internet, but the
hosts file is a way to locally add addresses that only affect our computer.
The reason we choose a unique name is because local addresses override DNS addresses. If we call our local site
www.microsoft.com and direct it to the loopback address, any attempt to access the Microsoft website would be redirected to our own server.
Step 2 – Modifying
We need to define each virtual host in the Apache configuration. Add the following to the end of the
httpd.conf configuration file:
NameVirtualHost 127.0.0.1 <VirtualHost 127.0.0.1> DocumentRoot "C:/wamp/www" ServerName localhost </VirtualHost> <VirtualHost 127.0.0.1> DocumentRoot "C:/wamp/www/urbangiraffe.com" ServerName local.urbangiraffe.com ErrorLog logs/urbangiraffe_error.log </VirtualHost>
Here two virtual hosts have been defined. One for the normal
localhost address, which we tested previously, and another for a local copy of the urbangiraffe.com website. You should modify the second host to match the name defined in the hosts file.
DocumentRoot can be set to any location you want. In the example above it has been set to a subdirectory of the main website. Whatever location you choose, you must ensure the directory exists.
Once all changes have been made you need to restart the Apache server. This can be done directly from the WAMP menu.
Before we can test our new configuration we need files in the virtual host root directory. Create a file called
phpinfo.php and insert the following text into it:
<?php phpinfo(); ?>
That’s all you need. This little file contains a PHP function that displays the current PHP configuration. It’s a handy function to show what you have installed.
Now test the virtual host by browsing to the URL:
Of course, change the domain name to your chosen name.
If all goes well you will have a screen displaying PHP information:
We can test the
mod_rewrite functionality by creating a
.htaccess file in our root directory:
RewriteEngine on RewriteRule ^test.php$ phpinfo.php
All this does is setup a rewrite rule that tells Apache to use
phpinfo.php whenever the URL is for
test.php. If you now browse to:
You should get the same information screen as for