This article shows how to configure Virtual Hosts in Apache Geronimo with Tomcat.
To configure a virtual host in Geronimo, you basically need to do the following actions:
This article used the simple HelloWorld application as a reference. This application is covered in the Quick start - Apache Geronimo for the impatient section.
To make this configuration work, you need to ensure that Geronimo can resolve the virtual host name you are about to define. Depending on your network configuration, you can add an entry to your DNS, or an alias to the Geronimo server IP. Alternatively, you can add an entry to the local host table. Each operating system has its own way to define a local host table. For example Windows will have %SystemRoot%\system32\drivers\etc\hosts
, and UNIX based operating systems would normally have an /etc/hosts
.
For this example we will be using the following host names defined in the local host table:
127.0.0.1 localhost virtualhost1.com virtualhost2.com virtualhost3.com virtualhost4.com
Make sure that your system can resolve these names.
There are three approaches to defining a virtual host in Geronimo:
var/config/config.xml
to set the definition. Requires a restart of Geronimo.var/catalina/server.xml
to set the definition. Requires a restart of Geronimo.We will cover the first two options which are specific to Geronimo's methodology of configuration. Instructions on performing the third option can be found in documentation on the Apache Tomcat project web site.
var/config/config.xml
Now you need to define those virtual hosts in Geronimo's config.xml
file so that Geronimo can recognize them. This section provides two different virtual host definitions, one defined with multiple host names aliases and one without host names alias. The virtual hosts are defined by creating two new HostGBean ( TomcatVirtualHost1 and TomcatVirtualHost2 ) in the Geronimo configuration. The goal of this example is to have an application listen on a single virtual host ( virtualhost1.com ) and another application listen on a different virtual host ( virtualhost2.com ), which has two aliases ( virtualhost3.com and virtualhost4.com ).
Open the config.xml
file located in the <geronimo_home>/var
directory and look for the following line <module name="org.apache.geronimo.configs/tomcat7/3.0/car">
. This is the beginning of the Tomcat configuration module. All the additional virtual host configurations will be done immediately after this line.
To define the first HostGBean TomcatVirtualHost_1, add the following lines right after <module name="org.apache.geronimo.configs/tomcat7/3.0/car">
.
To add the second HostGBean TomcatVirtualHost_2, add the following lines right after the first HostGBean. These two HostGBeans have been split so that it is easier to identify them. The main difference between these two HostGBeans is the <attribute name="aliases">..,..</attribute>
line that defines the aliases.
At this point, you have successfully configured two different virtual hosts in Geronimo. Make sure that you save the changes to the config.xml
file and then start Geronimo.
For additional reference, see the following excerpt from config.xml
of the entire <module name="org.apache.geronimo.configs/tomcat7/3.0/car">
entry with the two HostGBean already defined.
Note: Some tags in this config.xml
excerpt shown here are presented in multiple lines for displaying purposes only.
First create the GBean. Note the formatting of the <attribute name="initParams">
element which must be left as seen.
Deploy the GBean to your Geronimo server.
deploy --user system --password manager deploy gbean-Virtualhost1_com.xml
deploy --user system --password manager --port <RMI_port> deploy --targets <instance_repo_target> gbean-Virtualhost1_com.xml
The deployed GBean configuration is an installed module. All modules can be listed, which will show the Virtual Host GBean module just deployed.
deploy --user system --password manager --port <RMI_port> list-modules
To remove the configuration, undeploy the GBean
deploy --user system --password manager undeploy org.example.configs.virtualhosts/virtualhost1/1.0/car
For this topic, the HelloWorld sample application covered in the Quick start - Apache Geronimo for the impatient section is used as an example. Because this is a Web application, the deployment plan that you need to modify is a geronimo-web.xml
file. If you use a different type of application, you might need to modify a different deployment plan, for example geronimo-application.xml
.
In the previous section, you defined two Virtual Hosts. Now, you can configure two applications so that they can be deployed to those Virtual Hosts exclusively.
The following section uses the same application for the two Virtual Hosts, but will differentiate one deployment from the other by giving it a different artifactId. This way you can identify each deployment without modifying the code.
If you followed the steps covered in the Quick start - Apache Geronimo for the impatient section, your HelloWorld application should have the following structure:
Open the geronimo-web.xml
file and edit the artifactId and context-root to make this deployment unique. Within the <web-app> section, add the host attribute and specify the Virtual Host that you want this application to listen on, in this case virtualhost1.com
.
Save the changed to the geronimo-web.xml
file and generate a WAR file by typing the following command from the <APP_HOME>
directory:
jar -cvf HelloWorld_1.war *
When the HelloWorld application is deployed, this application should only be listed in the host with the name virtualhost1.com.
Now repeat the steps to create a second WAR. Edit the geronimo-web.xml
file once again and copy the content from the following example. Note that we are only changing the <artifactId>, <context-root> and <host>.
Save the changes to the geronimo-web.xml
file and generate a second WAR file by typing the following command from the <APP_HOME>
directory:
jar -cvf HelloWorld_2.war *
You now have two applications ready to be deployed to two different virtual hosts.
At this point, you have configured Geronimo to use two different Virtual Hosts, one of which is also configured to listen under additional aliases. All you need to do now is to deploy the applications and test them. To deploy the applications, type the following commands from the <GERONIMO_HOME>\bin
directory:
deploy --user system --password manager deploy <APP_HOME>\HelloWorld_1.war
You should get a successful confirmation message similar to the following one:
Repeat the deployment process for the second application.
deploy --user system --password manager deploy <APP_HOME>\HelloWorld_2.war
You should get a successful confirmation message similar to the following one:
With the applications deployed, you can now test them. To test the first application that has the context-root hello_1, access the host names defined on the Geronimo server machine with the URLs in the following table:
Host name / Virtual Host | Access |
---|---|
Fail | |
SUCCESS!!! | |
Fail | |
Fail | |
Fail |
Now repeat the tests for the second application that has the context-root hello_2.
Host name / Virtual Host | Access |
---|---|
Fail | |
Fail | |
SUCCESS!!! | |
SUCCESS!!! | |
SUCCESS!!! |
Congratulations!!! you have successfully configured and deployed two applications to two different virtual hosts and aliases.
Bookmark this on Delicious Digg this | Privacy Policy - Copyright © 2003-2013, The Apache Software Foundation, Licensed under ASL 2.0. |