Reader small image

You're reading from  Apache Tomcat 7 Essentials

Product typeBook
Published inMar 2012
Reading LevelBeginner
PublisherPackt
ISBN-139781849516624
Edition1st Edition
Languages
Right arrow
Author (1)
Tanuj Khare
Tanuj Khare
author image
Tanuj Khare

Tanuj Khare has worked as a professional in IT for six+ years. He is involved in Process improvements using ITIL framework and techniques (LEAN and Six sigma). He is MCSA and ITIL certified and has expertise in handling critical production server issues and has a track record of dealing with many complex problems. His quick resolution to issues faced in a production environment helped his team and clients a lot. Tanuj has SME (subject matter expertise) in Tomcat, weblogic, and JBOSS Server Administration. His experience includes working with large enterprise web hosting environment for J2EE container and small teams and his quick turnaround time resulted in on-time delivery. Apart from this, his technical expertise in root cause analysis, problem management, migration of enterprise application, and upgrade of web application servers is commendable. Until now he has done around 100+ enterprise application migrations. He has also done J2EE web and application upgrades and managed environments with 1,000+ middleware instances. Outside of work life, Tanuj enjoys playing table tennis and exploring new technologies. Tanuj is a good dancer. This is his first book.
Read more about Tanuj Khare

Right arrow

Chapter 11. Advanced Configuration for Apache Tomcat 7

In the previous chapters, we have discussed various topics for Tomcat 7 such as clustering, load balancing and so on. But, in practice, there are some different configurations needed to perform on the system, apart from the Tomcat internal configuration, in order to manage the systems. In this chapter, we will discuss the advanced topics for Tomcat 7, used in real-world industries, to create the web infrastructure and support multiple web applications.

In this chapter, we will discuss the following topics:

  • Virtual hosting

  • Running multiple applications on a single Tomcat server

  • Multiple Tomcat environments such as Development, QA, Stage, and Production

  • Tuning cache

  • Optimization of Tomcat

Virtual hosting

It's a method through which you can host multiple domain names on the same web server or on a single IP. The concept is called shared hosting, where one server is used to host multiple websites. For example, if you want to host abc.com and xyz...

Virtual hosting


It's a method through which you can host multiple domain names on the same web server or on a single IP. The concept is called shared hosting, where one server is used to host multiple websites. For example, if you want to host abc.com and xyz.com, and later you want to add one more website on the same web server, that can be achieved by virtual hosting. Basically, there are two types of virtual hosting:

  • Name-based virtual hosting

  • IP-based virtual hosting

Name-based virtual hosting

It's a method through which you can host multiple domains on a single IP. It uses the concept of shared services. In practice, web hosting companies follow this approach to host multiple sites for a low cost. For example, we have multiple sites such as www.abc.com,www.xyz.com, and www.xzy.com, and we want to configure it on the single web server using a single IP, then name-based virtual hosting is used. Following are the advantages of name-based virtual hosting:

  • Putting more than one website on...

Virtual hosting in Tomcat 7


Tomcat 7 supports name-based virtual hosting. This approach is very useful in hosting multiple web applications on the single instance of Tomcat 7. It also gives more privileges to the administrator to separate the applications from each other and their access control restrictions. You cannot understand the real concept of virtual hosting unless you implement it. So why wait, let's do the real-life implementation for virtual hosting in Tomcat 7.

For example, if you want to host the previously mentioned sites on the web server, then the DNS will be configured in the following manner. Let us assume the web server name is webserver1.yxz.com and is hosted on the IP 192.168.0.1. To implement the previous scenario, the following steps need to be performed:

  1. 1. Configure the domain names in the DNS server and reload the DNS services so that it can be replicated on the server. Following are the DNS records with the address and CNAME:

    Hostname aliases


    There is one more important feature that comes with Tomcat 7 called Host name aliases. It's a very good feature that gives freedom to the administrator for multiple sites on the same network

    For example, if you have a website which needs to be accessed through a subdomain by different users, then host aliases are created. It's also called Sub domain aliases for the main domain. It is not possible to implement aliases in the previous versions of Tomcat. In case we want to implement aliases for any website, we have to use Apache, IIS, or a separate web server before Tomcat as a front-end server.

    The following mentioned code describes how to set the alias for a particular site:

    <Host name="www.xyz.com" appBase="../Webapps">
    <Context path="" docBase="."/>
    <Alias>tomcatalias.com</Alias>
    </Host>
    

    Followed by recycle, once the system is up, you can browse the same application with different names.

    Multiple applications hosting on a single Tomcat 7 instance


    Once we are done with virtual hosting, a few potential problems may arise such as multiple application hosting, security, and deployment of multiple applications on a single instance of Tomcat 7. Configuration of multiple domains on one single instance of Tomcat 7 is a bit tricky. If we give the applications one document root, then all applications can be accessed by all developers. The solution is to implement a separate document root for each domain. This way, we can implement a separate security on each application that is hosted in the Tomcat instance. Let us implement the solution by creating multiple document roots in Tomcat 7. To do so, we have to edit the server.xml to enable multiple document roots in the server, as shown in the following code snippet:

    <Host name="www.xyz.com" appBase="../home/tomcatuser1">
    <Context path="" docBase="/home/tomcatuser1/data"/>
    <Alias>tomcatalias.com</Alias>
    </Host...

    Multiple Tomcat environments—Development/QA/Stage/Production


    Information technology organizations follow a set of environments to manage their applications. These environments are based on their functionality and usage. Support available for any environment depends on the environment's functionality. Based on the functionality, the production environment has a high priority and development the least priority, as shown in the following figure:

    Alias

    Record

    Domain

    webserver1...

    The following table compares the different environments and their functionalities with respect to different tasks performed during creation and management of the web infrastructure:

    Task

    Development

    QA

    Stage

    Production

    Auto deployment

    Yes

    Yes

    No

    No

    Single machine

    Yes

    No

    No

    No

    Clustering

    No

    Yes

    Yes

    Yes

    Developer access

    Yes

    Yes

    No

    No

    High-end machine

    No

    No

    Yes

    Yes

    Change control

    No

    No

    No

    Yes

    Performance testing

    No

    No

    Yes

    No

    Functional testing

    No

    Yes

    No

    No

Tuning cache


When we are running multiple applications on Tomcat 7, it is always recommended to utilize the resource correctly. In order to do so, we need to optimize the tuning parameter. Every time the server receives a request, it consumes the amount of CPU and memory in the system. In order to resolve this problem, we generate cache on the server from the first request. One of the best examples used for caching in major web hosting organizations is to generate cache for the static content.

The following code shows the configuration for adding Expires and Cache-Control: max-age= headers to images, CSS, and JavaScript. This code is added in web.xml, which is present in TOMCAT_HOME/CONF.

<filter>
<filter-name>ExpiresFilter</filter-name>
<filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
<init-param>
<param-name>ExpiresByType image</param-name>
<param-value>access plus 15 minutes</param-value>
</init-param...

Optimization of Tomcat 7


In Chapter 3, Performance Tuning, we have discussed various optimization methods for Tomcat at the software level, but until now, we have not done any system-level configurations. We will now discuss the various optimization methods, which are required for system administrators to make their work more successful. One of the most important things required is to run the Tomcat services as a non-privileged user.

Running Tomcat 7 as a non privileged user

It is not recommended way to run Tomcat as the root, because of security reasons and IT compliance policies. To resolve this issue, you have to run Tomcat as a non-privileged user. To implement this, you have to perform the following changes in the user permission. Let us assume tomcatuser1 will run the Tomcat server as a non-privileged user.

# groupadd tomcatuser1
# useradd -g tomcatuser1 -d /opt/apache-tomcat1
# chown -R tomcatuser1:tomcatuser1 /opt/apache-tomcat1

Once you change the permission at the OS level, it...

Summary


In this chapter, we have discussed the advanced configuration of Tomcat 7 and optimization parameters, key points covered in the environment such as virtual hosting, features of Development/QA/Stage/Production, Tomcat as a service, and running Tomcat as a non privileged user.

With this chapter, we have completed the journey of Tomcat 7. In this book, I have tried to complete major issues, which the web administrators and IT administrators face in day-to-day environments. I hope by reading the topics, you have gained enough confidence in running Tomcat 7 in real-time environments.

Best of luck!

Tanuj Khare

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Apache Tomcat 7 Essentials
Published in: Mar 2012Publisher: PacktISBN-13: 9781849516624
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tanuj Khare

Tanuj Khare has worked as a professional in IT for six+ years. He is involved in Process improvements using ITIL framework and techniques (LEAN and Six sigma). He is MCSA and ITIL certified and has expertise in handling critical production server issues and has a track record of dealing with many complex problems. His quick resolution to issues faced in a production environment helped his team and clients a lot. Tanuj has SME (subject matter expertise) in Tomcat, weblogic, and JBOSS Server Administration. His experience includes working with large enterprise web hosting environment for J2EE container and small teams and his quick turnaround time resulted in on-time delivery. Apart from this, his technical expertise in root cause analysis, problem management, migration of enterprise application, and upgrade of web application servers is commendable. Until now he has done around 100+ enterprise application migrations. He has also done J2EE web and application upgrades and managed environments with 1,000+ middleware instances. Outside of work life, Tanuj enjoys playing table tennis and exploring new technologies. Tanuj is a good dancer. This is his first book.
Read more about Tanuj Khare