Home

Apache Virtual Hosts

9th December 2006

If you’ve managed to get Apache / Tomcat / PHP / Ruby set up working successfully you soon come up against a problem. Every time you create a new ‘site’ you have to get it working as a path relative to http://localhost . So, client 1 gives you a new site and your test site is http://localhost/client1 , client 2 becomes http://localhost/client2 and so on. All well and good but what happens when you have html that refers to the root directory of the site? – some thing like <img src="/images/myimage.jpg" /> is not uncommon.

What would be better is if you had lots of test urls you could use locally. That would allow you to use references to the top level site directory without seeing lots of broken images in the browser or having broken links on the page. With Apache, you can do just that. I'm assuming that you've followed all the guides at http://apacheguide.org to correctly set up Apache and Tomcat/PHP/Ruby

Steps
1. Create your new test website address. You should avoid real website addresses, it’s possible to set up a test site called http://www.google.com but not really a good idea. Suppose for that we want to use http://mytest.test as the url.
Open up your hosts file. You’ll find it in the C:\windows\system32\drivers\etc folder.
It’ll look something like

# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost

You’ll need the following line after the 127.0.0.1 localhost line

127.0.0.1       mytest.test

That’ll take care of making sure that your local setup will send the correct url to Apache
2. The next step is to get Apache to work with mytest.test. You'll need to edit the httpd.conf file. Copy and paste this into your <apache>/conf/httpd.conf and update the paths to the ones on your system.


NameVirtualHost mytest.test:80
<VirtualHost mytest.test>
	ServerName mytest.test
    ServerAdmin webmaster@localhost
    DocumentRoot C:/path/to/mytest.test
    ErrorLog logs/my-error_log
    CustomLog logs/my_log common
	<Directory "C:/path/to/mytest.test">
		Options Indexes FollowSymLinks MultiViews Includes
		AllowOverride All
		Order allow,deny
		Allow from all
	</Directory>
</VirtualHost>
  
  

If you restart Apache and go to http://mytest.test it should work fine.

If your PHP files are not working correctly, you might need to add the following line to the VirtualHost listing above.

  
  # Change to whatever is the correct path to your php.ini file
PHPIniDir "C:/PHP"

If you're running Tomcat as well, you'll need to set up Tomcat to work with the new test url. You can use the Tomcat admin manager to do this (try http://localhost:8080/admin ) but I've had very mixed results with this approach and I prefer to set up the xml directly.

3. Edit the server.xml file in your <tomcat>/conf directory. Look for the <Host name="localhost" appBase="webapps" /> tag in the xml and add the following after it

  
  <Host name="mytest.test"></Host> 
	  
	  

4. Now go to your <tomcat>/conf/Catalina directory and add a folder called mytest.test

5. In the <tomcat>/conf/Catalina/mytest.test directory add a file called ROOT.xml

6. Copy and paste this into the ROOT.xml file

  
  <?xml version="1.0" encoding="UTF-8"?>
	<Context
		docBase="C:/path/to/mytest.test"
		reloadable="true">
	</Context>
  
  

7. Restart Tomcat and you should be able get your JSPs working on http://mytest.test:8080

8. Now you need to tie Tomcat and Apache together. Go back to your httpd.conf and paste the JkMount directives in there.


NameVirtualHost mytest.test:80
<VirtualHost mytest.test>
	ServerName mytest.test
    ServerAdmin webmaster@localhost
    DocumentRoot C:/path/to/mytest.test
    ErrorLog logs/my-error_log
    CustomLog logs/my_log common
	<Directory "C:/path/to/mytest.test">
		Options Indexes FollowSymLinks MultiViews Includes
		AllowOverride All
		Order allow,deny
		Allow from all
	</Directory>
	JkMount /*.jsp ajp13
    JkMount /* ajp13
	# Make sure that Tomcat doesn't try and handle php
	JkUnMount /*.php ajp13
	JkUnMount /*.gif ajp13
	JkUnMount /*.html ajp13
	JkUnMount /*.css ajp13
	JkUnMount /*.png ajp13
	JkUnMount /*.jpg ajp13
</VirtualHost>
  
  

The JkMount directives tell Apache to hand off requests to Tomcat. However the line JkMount /* ajp13 hands all requests to Tomcat which means that Tomcat is trying to handle all your requests for non Java stuff like PHP or Ruby. Using the JkUnMount directive allows you filter requests so that Tomcat only handles Java stuff and Apache deals with images and PHP.