April 23, 2024

Multiple sites using Apache Virtualhost directive

A useful and easy way to setup multiple web sites using the Apache HTTP Server is the practice of the virtual host: that is the ability to host multiple web sites on the same instance of httpd service. The community offers an impressive number of modules and libraries that allow you to extend the of Apache core functionality. In this article I will show how to use the proxy_ajp_module extension to configure multiple web sites dns on the same server using the “VirtualHost” Apache directive. The following figure shows the scenario that I will examine.


Apache Web Server – IP:

Install, if not present, the http Apache Web Server and check proxy_ajp module.

$ yum install httpd
$ httpd -M | grep proxy
proxy_module (shared)
proxy_balancer_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_ajp_module (shared)
proxy_connect_module (shared)

Setup the multiple sites proxy pass/reverse definition by creating a configuration file like this.

$ vi /etc/init.d/conf.d/virtualhosts.config

NameVirtualHost *:80

<VirtualHost *:80>
 ServerName site1.mydomain.com
 ProxyPass /my_app1 ajp://
 ProxyPassReverse /my_app1 ajp://
 RedirectMatch ^/$ http://site1.mydomain.com/my_app1

<VirtualHost *:80>
 ServerName site2.mydomain.com
 ProxyPass /my_app2 ajp://
 ProxyPassReverse /my_app2 ajp://
 RedirectMatch ^/$ http://site2.mydomain.com/my_app2

I used the RedirectMatch definition to default redirect  the base ServerName url to ProxyPass definition. In this way, Apache make a default redirect url from http://site1.mydomain.com  to  http://site1.mydomain.com/my_app1. Now proceed with the two Tomcat Server configurations.

Tomcat Application Server -IP:

Tomcat Application Server -IP:

If you have performed a default installation of the Tomcat applicaton server, the ajp connector is already configured. Check the server.xml file. You should make sure that line of ajp definiton is uncommented.

 <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

If you want to make some tests make sure to set the dns entries to the /etc/hosts file on the client and server machines. You clould set someting like this:

$ vi /etc/hosts    site1.mydomain.com site2.mydomain.com

Related posts

Leave a Reply

Your email address will not be published.