November 22, 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.
screen07

 

Apache Web Server – IP: 10.10.1.50

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://10.10.1.100:8009/my_app1
 ProxyPassReverse /my_app1 ajp://10.10.1.100:8009/my_app1
 RedirectMatch ^/$ http://site1.mydomain.com/my_app1
</VirtualHost>

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

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:10.10.1.100

Tomcat Application Server -IP:10.10.1.200

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

10.10.1.50    site1.mydomain.com site2.mydomain.com

Related posts

Leave a Reply

Your email address will not be published.