Tomcat Application Deployment with the Apache Web Server
- 24/10/2014 11:38 AM
Applicable Plans - All Cloud Hosting Plans
Tomcat application deployment with Apache Web Server
Overview
Using the eApps Virtual Cloud Server Hosting service, Java SE 7 or Java SE 8, and the Apache Tomcat Application Server, you can create web sites and web applications with dynamic content for your customers and users.
This User Guide is designed to help you deploy your Tomcat applications using either mod_jk or mod_proxy_ajp.
Reading the Installation and Tuning User Guide is a prerequisite for this User Guide, because the things covered in that User Guide are generally not covered again here, such as how to stop, start, or restart Tomcat, or where the configuration files are located.
Pay careful attention to the resources allocated to your Virtual Server when deploying Tomcat applications, especially the available RAM. Tomcat can be very memory intensive, and a lack of resources is one of the main reasons for poor performance. For a small Tomcat application, start with at least 768 MB of RAM, and be prepared to scale upwards as necessary. For larger Tomcat applications, start with at least 1024 MB of RAM, and again be ready to scale upwards if your traffic and resource needs demand it.
This user guide uses the generic TomcatX or tomcatX to refer to Tomcat versions. Please make sure to substitute your version of Tomcat for X (either Tomcat6/tomcat6, Tomcat7/tomcat7, or Tomcat8/tomcat8) where applicable. |
Deploying Tomcat Applications with the Apache Web Server
Deployment examples using Tomcat with Apache
Using mod_jk or mod_proxy_ajp for deployment
1. Deployment with Tomcat serving JSP and Servlet using a WAR file from the default Tomcat directory, Apache serving all other content
Creating the Tomcat application directory
Editing server.xml
mod_jk and mod_proxy_ajp directives to serve eapps-example.com
2. Deployment with Tomcat serving JSP and Servlets using a WAR file from the website DocumentRoot, Apache serving all other content
Tomcat User and DocumentRoot Permissions
Apache DirectoryIndex
Editing server.xml
mod_jk and mod_proxy_ajp directives to serve eapps-example.com
3. Deploying multiple applications with Tomcat using WAR files from the default Tomcat directory, Apache serving all other content
Creating the Tomcat application directory
Editing server.xml
mod_jk and mod_proxy_ajp directives to serve eapps-example.com
Installing Tomcat
Information and instructions on installing Tomcat and related applications can be found in the Tomcat Installation and Tuning Guide. Make sure you have read that guide before starting to deploy your applications.
Deploying Tomcat Applications with the Apache Web Server
Using mod_jk or mod_proxy_ajp you can have the Apache web server as a front-end to Tomcat. This is the most common usage at eApps, because it allows you to serve applications that require Apache along with your Java application being served by Tomcat.
Deployment examples using Tomcat with Apache
There are three ways to deploy applications using Tomcat with Apache:
-
Deployment with Tomcat serving JSP and Servlets using a WAR file from the default Tomcat directory, Apache serving all other content using mod_jk or mod_proxy_ajp
-
Deployment with Tomcat serving JSP and Servlets using a WAR file from the website DocumentRoot, Apache serving all other content using mod_jk or mod_proxy_ajp
-
Deploying multiple applications with Tomcat using WAR files from the default Tomcat directory, Apache serving all other content using mod_jk or mod_proxy_ajp
Using mod_jk or mod_proxy_ajp for deployment
Both mod_jk and mod_proxy_ajp work by using directives to send requests to the Apache web server to files or applications as specified by the directive. For example, this allows you to pass all requests for .php files to Apache, and all requests for .jsp files to Tomcat.
-
mod_jk – this is the most common way of configuring Tomcat and Apache. mod_jk is older and a bit more robust than the newer mod_proxy_ajp. With mod_jk you specify directives that tell Apache whether to pass the request on to Tomcat, or if Apache will handle the request itself. mod_jk is installed as part of any CentOS Tomcat (ISPmanager CP) template.
-
mod_proxy_ajp – similar to using mod_jk, in that you have to specify directives to forward requests from Apache to Tomcat. mod_proxy_ajp is built into the Apache web server starting with version 2.2.
Both mod_jk and mod_proxy_ajp work in a very similar manner. If you have a preferred approach, use that. If you do not have a preferred approach, use mod_jk.
1. Deployment with Tomcat serving JSP and Servlet using a WAR file from the default Tomcat directory, Apache serving all other content
If you are serving your Tomcat application as a WAR file or JSP files from the default Tomcat directory (/opt/tomcatX) and not from the Apache DocumentRoot for the web site, this is the approach to use. This deployment approach is used by the majority of eApps customers who are using Tomcat and mod_jk or mod_proxy_ajp.
For this approach you will need to create a directory for your WAR file in the Tomcat webapps directory, and then add a Host block to the /opt/tomcatX/conf/server.xml file, and then add mod_jk or mod_proxy_ajp directives to pass requests from Apache to Tomcat.
Creating the Tomcat application directory
You will need to create a directory in the Tomcat webapps directory where you will put the files for the application. This directory will be the appBase for the Host in the server.xml file. Generally this directory will have the same name as the domain where you are serving the application.
You can create this directory from the file manager in your Control Panel, or from the command line.
Using the ISPmanager Control Panel File Manager
-
ISPmanager 4 - the File Manager is located at Management Tools > File Manager. More information can be found here - ISPmanager 4 File Manager
-
ISPmanager 5 - the File Manager is located at System > File manager. More information can be found here - ISPmanager 5 File Manager
Once you are in the File Manager, navigate to opt > tomcatX > webapps. (NOTE - these screenshots show ISPmanager 4, but ISPmanager 5 is very similar in functionality).
To create the application directory, click on New. This opens the Add file or folder dialog box. Set the Type to Folder, and the Name to the domain name you are going use to serve the Tomcat application. In this example, the domain is eapps-example.com.
Click OK to create the new directory.
This creates the new directory, but it has the wrong user and group, since it was created by the root user. This must be changed to tomcat for both user and group.
Click on the name of the newly created directory (in this example, eapps-example.com) to highlight it, and then click on the Attrib. button.
This opens the Edit properties dialog box.
-
User - select tomcat from the list of users
-
Group - select tomcat from the list of groups
-
Recursive - select change permissions for files and folder from the drop down list
-
Permissons - set to 770
Once you have made your changes, click OK. This sets the new directory with the correct user, group, and permissions.
Now you can upload or import your files and applications into the new directory. Make sure that you change the user and group to match, and also that the file permissions are correct. Most directories should have 775 or 770 permissions, and most files should have 644 permissions.
Command line
To create the application directory from the command line, you will need to log in to the Virtual Server using SSH. You will also need to be able to work at the root user.
Once you are logged in, change directories to the /opt/tomcatX/webapps directory.
[root@eapps-example ~]# cd /opt/tomcatX/webapps/ [root@eapps-example webapps]# pwd /opt/tomcatX/webapps [root@eapps-example webapps]# ll total 20 drwxrwx--- 13 tomcat tomcat 4096 Jul 26 17:39 docs drwxrwx--- 5 tomcat tomcat 4096 Jul 26 17:39 examples drwxrwx--- 5 tomcat tomcat 4096 Jul 26 17:39 host-manager drwxrwx--- 5 tomcat tomcat 4096 Jul 26 17:39 manager drwxrwx--- 3 tomcat tomcat 4096 Jul 26 17:39 ROOT [root@eapps-example webapps]# |
Next, use the mkdir command to create the directory, and then the chown and chmod commands to set the correct ownership and permissions.
Since the Tomcat application will be served from the http://eapps-example.com site, the directory name will be eapps-example.com. This isn't strictly necessary (you can name the directory whatever you want), but using the same name as the site keeps things organized.
[root@eapps-example webapps]# mkdir eapps-example.com [root@eapps-example webapps]# ll total 24 drwxrwx--- 13 tomcat tomcat 4096 Jul 26 17:39 docs drwxr-xr-x 2 root root 4096 Aug 9 15:12 eapps-example.com drwxrwx--- 5 tomcat tomcat 4096 Jul 26 17:39 examples drwxrwx--- 5 tomcat tomcat 4096 Jul 26 17:39 host-manager drwxrwx--- 5 tomcat tomcat 4096 Jul 26 17:39 manager drwxrwx--- 3 tomcat tomcat 4096 Jul 26 17:39 ROOT [root@eapps-example webapps]# chown tomcat:tomcat eapps-example.com/ [root@eapps-example webapps]# chmod 770 eapps-example.com/ [root@eapps-example webapps]# ll total 24 drwxrwx--- 13 tomcat tomcat 4096 Jul 26 17:39 docs drwxrwx--- 2 tomcat tomcat 4096 Aug 9 15:12 eapps-example.com drwxrwx--- 5 tomcat tomcat 4096 Jul 26 17:39 examples drwxrwx--- 5 tomcat tomcat 4096 Jul 26 17:39 host-manager drwxrwx--- 5 tomcat tomcat 4096 Jul 26 17:39 manager drwxrwx--- 3 tomcat tomcat 4096 Jul 26 17:39 ROOT [root@eapps-example webapps]# |
Now you can upload or import your files and applications into the new directory. Make sure that you change the user and group to match, and also that the file permissions are correct. Most directories should have 775 or 770 permissions, and most files should have 644 permissions.
Since the deployment of the applications is outside of the default webapps directory, each Host must have an application named ROOT in the appBase. If the application is a WAR file, the application has to be named ROOT.war. If any additional applications are deployed in this Host they can have any name except for ROOT.
Editing server.xml
You will need to edit the server.xml (/opt/tomcatX/conf/server.xml) file so that Tomcat knows where your application is located. This will require you to add a HostBlock to the server.xml file that references the location of your application and the hostname where the application will be served.
The server.xml file can be edited using the File Manager from the Control Panel, or from the command line of the Virtual Server.
NOTE - only make the specified changes to the server.xml file, in the specified locations.
Add the following Host block just below the last closing </Host>
tag in the server.xml file, and then restart Tomcat. This will serve your application at http://www.eapps-example.com or http://eapps-example.com.
Also, remember that this is an example - you will need to substitute your own domain name.
<Host appBase="webapps/eapps-example.com" name="eapps-example.com" unpackWARs="true" autoDeploy="true">
<Alias>www.eapps-example.com</Alias>
<Context path="" docBase="ROOT" debug="0" reloadable="true"/>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="eapps-example.com." suffix=".txt" timestamp="true"/>
</Host>
mod_jk and mod_proxy_ajp directives to serve eapps-example.com
To serve eapps-example.com using mod_jk or mod_proxy_ajp, add the following directives to the VirtualHost block for the website. These directives will serve all applications provided by eApps but forward all other requests to Tomcat.
You can all the directives to the VirtualHost block from the Control Panel or from the command line.
Using a Control Panel
-
ISPmanager 4 - go to Domains > WWW Domains and select the domain where you want to add the mod_jk or mod_proxy_ajp directives. Click on Config, and then add your directives near the end of the file, just above the closing
</VirtualHost>
tag. More information about managing WWW Domains in ISPmanager 4 can be found here - Managing WWW Domains -
ISPmanager 5 - go to Domains > Web-domains and select the domain where you want to add the mod_jk or mod_proxy_ajp directives. Click on Config, and then add your directives near the end of the file, just above the closing
</VirtualHost>
tag. More information about managing Web-domains in ISPmanager 5 can be found here - Managing Web-domains
Information on how to tell which version of ISPmanager you are using can be found here - ISPmanager versions
Using the command line
You can also add the directives to the VirtualHost block for the website in the httpd.conf file, and then restart Apache.
-
For CentOS 6, use the
service httpd restart
command:[root@eapps-example ~]# service httpd restart
-
For CentOS 7, use the
systemctl restart httpd
command:[root@eapps-example ~]# systemctl restart httpd
mod_jk Directives
JkMount /* ajp13 # Directives to enable Apache to continue serving applications dependent on it. SetEnvIf Request_URI "/webmail" no-jk SetEnvIf Request_URI "/awstats" no-jk SetEnvIf Request_URI "/myadmin" no-jk SetEnvIf Request_URI "/pgadmin" no-jk SetEnvIf Request_URI "/cgi-bin" no-jk SetEnvIf Request_URI "/joomla" no-jk SetEnvIf Request_URI "/wordpress" no-jk SetEnvIf Request_URI "/drupal" no-jk SetEnvIf Request_URI "/mailman" no-jk |
Depending on the applications you have installed, you may need different (or additional or fewer) no-jk settings.
If you are using ISPmanager 4, make a note of these three "no-jk" directives:
SetEnvIf Request_URI "/manager/ispmgr*" no-jk SetEnvIf Request_URI "/manimg*" no-jk SetEnvIf Request_URI "/mancgi*" no-jk |
These are the directives that allow the ISPmanager 4 Control Panel to still be available, even if everything else is being forwarded to Tomcat. These must be included if you still want to use ISPmanager 4.
mod_proxy_ajp Directives
# Directives for your application ProxyPass / ajp://localhost:8009/ ProxyPassReverse / ajp://localhost:8009/ # Directives for eApps applications dependent on Apache ProxyPass /webmail ! ProxyPass /joomla ! ProxyPass /awstats ! ProxyPass /myadmin ! ProxyPass /cgi-bin ! ProxyPass /pgadmin ! |
Depending on the applications you have installed, you may need different (or additional or fewer) ProxyPass settings.
If you are using ISPmanager 4, make a note of these three "mod_proxy_ajp" directives:
ProxyPass /manager/ispmgr ! ProxyPass /manimg ! ProxyPass /mancgi ! |
These are the directives that allow the ISPmanager 4 Control Panel to still be available, even if everything else is being forwarded to Tomcat. These must be included if you still want to use ISPmanager 4.
2. Deployment with Tomcat serving JSP and Servlets using a WAR file from the website DocumentRoot, Apache serving all other content
This deployment example assumes you have a WAR file called mywebapp.war, using a Virtual Host of eapps-example.com that you are serving from the Apache DocumentRoot directory for eapps-example.com - in this example: /var/www/webadmin/data/www/eapps-example.com.
Tomcat User and DocumentRoot Permissions
Tomcat will need to have access to the Apache DocumentRoot, so it must be added to the group that owns the DocumentRoot of the site. In these examples that is the webadmin group, your actual group may be different. The permissions for the directory the DocumentRoot is in may also need to be changed so that group has permission to read and write in that directory. These changes have to be made from the command line of the Virtual Server using SSH.
Log in as the root user, with the root password. Then use the usermod -G webadmin tomcat command to add the tomcat user to the webadmin group.
[root@example ~]# usermod -G webadmin tomcat |
If you need to change the permissions of the DocumentRoot for the website, use chmod 775 eapps-example.com to allow the group to be able to read, write, and execute files in the DocumentRoot directory.
[root@example ~]# cd /var/www/webadmin/data/www [root@example www]# ll total 4 drwxrwxr-x 3 webadmin webadmin 4096 Nov 7 11:53 eapps-example.com [root@example www]# chmod 775 eapps-example.com/ [root@example www]# ll total 4 drwxrwxr-x 3 webadmin webadmin 4096 Nov 7 11:53 eapps-example.com [root@example www]# |
Apache DirectoryIndex
The mywebapp directory that is expanded from the mywebapp.war file will need to be the DirectoryIndex for the site. The DirectoryIndex is set in the Config option for each Virtual Server you create that uses Tomcat.
The line to add for the DirectoryIndex is: DirectoryIndex /mywebapp
Using a Control Panel
-
ISPmanager 4 - go to Domains > WWW Domains and select the domain where you want to add the DirectoryIndex. Click on Config, and then add the DirectoryIndex line near the end of the file, just above the closing
</VirtualHost>
tag. More information about managing WWW Domains in ISPmanager 4 can be found here - Managing WWW Domains -
ISPmanager 5 - go to Domains > Web-domains and select the domain where you want to add the DirectoryIndex. Click on Config, and then add the DirectoryIndex line near the end of the file, just above the closing
</VirtualHost>
tag. More information about managing Web-domains in ISPmanager 5 can be found here - Managing Web-domains
Once you have added your DirectoryIndex line, click Ok. This will reload Apache so that the new settings are read.
Using the command line
If you are comfortable working with the Apache httpd.conf configuration file directly, you can add the DirectoryIndex to the VirtualHost block for the web site from the command line. The web server will need to be restarted after the changes have been made.
-
For CentOS 6, use the
service httpd restart
command:[root@eapps-example ~]# service httpd restart
-
For CentOS 7, use the
systemctl restart httpd
command:[root@eapps-example ~]# systemctl restart httpd
Editing server.xml
You will need to edit the server.xml file so that Tomcat knows where your application is located. Add the following Host block just below the last closing </Host>
tag in the server.xml file, and then restart Tomcat. This will serve your application at http://www.eapps-example.com or http://eapps-example.com.
You can edit server.xml from either the File Manager or the command line.
Also, remember that this is an example - you will need to substitute your own domain name.
<Host appBase="/var/www/webadmin/data/www/eapps-example.com/html" name="eapps-example.com" unpackWARs="true" autoDeploy="true">
<Alias>www.eapps-example.com</Alias>
<Context path="" docBase="mywebapp" debug="0" reloadable="true"/>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="eapps-example.com." suffix=".txt" timestamp="true"/>
</Host>
mod_jk and mod_proxy_ajp directives to serve eapps-example.com
To serve eapps-example.com using mod_jk or mod_proxy_ajp, add the following directives to the VirtualHost block for the website. These directives will serve all applications provided by eApps but forward all other requests to Tomcat.
You can all the directives to the VirtualHost block from the Control Panel or from the command line.
Using a Control Panel
-
ISPmanager 4 - go to Domains > WWW Domains and select the domain where you want to add the mod_jk or mod_proxy_ajp directives. Click on Config, and then add your directives near the end of the file, just above the closing
</VirtualHost>
tag. More information about managing WWW Domains in ISPmanager 4 can be found here - Managing WWW Domains -
ISPmanager 5 - go to Domains > Web-domains and select the domain where you want to add the mod_jk or mod_proxy_ajp directives. Click on Config, and then add your directives near the end of the file, just above the closing
</VirtualHost>
tag. More information about managing Web-domains in ISPmanager 5 can be found here - Managing Web-domains
Information on how to tell which version of ISPmanager you are using can be found here - ISPmanager versions
Using the command line
You can also add the directives to the VirtualHost block for the website in the httpd.conf file, and then restart Apache.
-
For CentOS 6, use the
service httpd restart
command:[root@eapps-example ~]# service httpd restart
-
For CentOS 7, use the
systemctl restart httpd
command:[root@eapps-example ~]# systemctl restart httpd
mod_jk Directives
JkMount /mywebapp* ajp13 |
This directive passes all requests for /mywebapp to Tomcat, and allows Apache to serve all other content.
mod_proxy_ajp Directives
ProxyPass /mywebapp ajp://localhost:8009/mywebapp |
3. Deploying multiple applications with Tomcat using WAR files from the default Tomcat directory, Apache serving all other content
It is possible to serve two applications from the same Tomcat directory. For example, you can configure Tomcat so that someone going to http://eapps-example.com would be served one application, but someone going to http://eapps-example.com/webapp would be served a different application.
Both applications in this example are in the same directory - /opt/tomcatX/webapps/eapps-example.com. The application being served at eapps-example.com is ROOT.war, and the application being served at eapps-example.com/webapp is the webapp.war file in the /webapp subdirectory (/opt/tomcatX/webapps/eapps-example.com/webapp)
Creating the Tomcat application directory
Instructions on creating an application directory for Tomcat are here - Creating the Tomcat application directory
Editing server.xml
You will need to edit the server.xml (/opt/tomcatX/conf/server.xml) file so that Tomcat knows where your application is located. This will require you to add a HostBlock to the server.xml file that references the location of your application and the hostname where the application will be served.
The server.xml file can be edited using the File Manager from the Control Panel, or from the command line of the Virtual Server.
NOTE - only make the specified changes to the server.xml file, in the specified locations.
Add the following Host block just below the last closing </Host>
tag in the server.xml file, and then restart Tomcat. This will serve your application at http://www.eapps-example.com or http://eapps-example.com.
Also, remember that this is an example - you will need to substitute your own domain name.
<Host appBase="webapps/eapps-example.com" name="eapps-example.com" unpackWARs="true" autoDeploy="true">
<Alias>www.eapps-example.com</Alias>
<Context path="" docBase="ROOT" debug="0" reloadable="true"/>
<Context path="/webapp" docBase="webapp" debug="0" reloadable="true"/>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="eapps-example.com." suffix=".txt" timestamp="true"/>
</Host>
mod_jk and mod_proxy_ajp directives to serve eapps-example.com
To serve eapps-example.com using mod_jk or mod_proxy_ajp, add the following directives to the VirtualHost block for the website. These directives will serve all applications provided by eApps but forward all other requests to Tomcat.
You can all the directives to the VirtualHost block from the Control Panel or from the command line.
Using a Control Panel
-
ISPmanager 4 - go to Domains > WWW Domains and select the domain where you want to add the mod_jk or mod_proxy_ajp directives. Click on Config, and then add your directives near the end of the file, just above the closing
</VirtualHost>
tag. More information about managing WWW Domains in ISPmanager 4 can be found here - Managing WWW Domains -
ISPmanager 5 - go to Domains > Web-domains and select the domain where you want to add the mod_jk or mod_proxy_ajp directives. Click on Config, and then add your directives near the end of the file, just above the closing
</VirtualHost>
tag. More information about managing Web-domains in ISPmanager 5 can be found here - Managing Web-domains
Information on how to tell which version of ISPmanager you are using can be found here - ISPmanager versions
Using the command line
You can also add the directives to the VirtualHost block for the website in the httpd.conf file, and then restart Apache.
-
For CentOS 6, use the
service httpd restart
command:[root@eapps-example ~]# service httpd restart
-
For CentOS 7, use the
systemctl restart httpd
command:[root@eapps-example ~]# systemctl restart httpd
mod_jk Directives
JkMount /* ajp13 # Directives to enable Apache to continue serving applications dependent on it. SetEnvIf Request_URI "/webmail*" no-jk SetEnvIf Request_URI "/awstats*" no-jk SetEnvIf Request_URI "/myadmin*" no-jk SetEnvIf Request_URI "/pgadmin*" no-jk SetEnvIf Request_URI "/cgi-bin*" no-jk SetEnvIf Request_URI "/joomla*" no-jk SetEnvIf Request_URI "/wordpress*" no-jk SetEnvIf Request_URI "/drupal*" no-jk SetEnvIf Request_URI "/mailman*" no-jk |
Depending on the applications you have installed, you may need different (or additional or fewer) no-jk settings.
If you are using ISPmanager 4, make a note of these three "no-jk" directives:
SetEnvIf Request_URI "/manager/ispmgr*" no-jk SetEnvIf Request_URI "/manimg*" no-jk SetEnvIf Request_URI "/mancgi*" no-jk |
These are the directives that allow the ISPmanager 4 Control Panel to still be available, even if everything else is being forwarded to Tomcat. These must be included if you still want to use ISPmanager 4.
mod_proxy_ajp Directives
# Directives for eApps applications dependent on Apache ProxyPass /webmail ! ProxyPass /joomla ! ProxyPass /awstats ! ProxyPass /myadmin ! # Directives for your application ProxyPass / ajp://localhost:8009/ ProxyPassReverse / ajp://localhost:8009/ |
Depending on the applications you have installed, you may need different (or additional or fewer) ProxyPass settings.
Control Panel Configuration
If you are using ISPmanager 4, make a note of these three "mod_proxy_ajp" directives:
ProxyPass /manager/ispmgr ! ProxyPass /manimg ! ProxyPass /mancgi ! |
These are the directives that allow the ISPmanager Control Panel to still be available, even if everything else is being forwarded to Tomcat. These must be included if you still want to use ISPmanager 4.
Comments
Powered by WHMCompleteSolution