Tomcat Installation and Tuning
Posted by on 24 October 2014 11:35 AM
|
|||||||
Applicable Plans - All Cloud Hosting PlansTomcat Installation and TuningOverviewUsing 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 install Java and Tomcat on your Virtual Server, and to do basic tuning if necessary. Information on how to actually deploy your applications using one of the supported deployment methods: Tomcat with the Apache Web Server or Tomcat without the Apache Web Server, are found in the User Guides specific to that method:
Installing Tomcat Starting and Stopping Tomcat Tomcat Manager Tuning Tomcat Installing TomcatThere are two choices for installing Tomcat:
For either installation choice, you will need to install a database and a database connector (JDBC). Which databases are available will depend on your OS Template (CentOS 6 or CentOS 7).
You will also need to install either the MySQL JDBC Driver for MySQL or MariaDB, or the PostgreSQL JDBC Driver for PostgreSQL. This is explained in the user guide for each database. If you are going to use the mod_jk deployment method (used by the majority of eApps customers), you will need to install mod_jk. If you are going to use the mod_proxy_ajp deployment method, nothing additional needs to be installed because mod_proxy_ajp is an Apache module, and is available if Apache is installed. Installing Tomcat using a Control PanelInstalling Tomcat using the ISPmanager Control PanelIf you are using the ISPmanager Control Panel, you can install Tomcat and related applications from that Control Panel.
Information on how to tell which version of ISPmanager you are using can be found here - ISPmanager versions Installing Tomcat using the command lineYou can install Tomcat and related applications from the command line of the Virtual Server. You will need to install either Java SE 7 or Java SE 8 and several Java plugins first, and then install Tomcat. All of this is done using yum. To install Java SE 7 and the related applications, use the [root@eapps-example ~]# yum install -y java-1.7.0-sun java-1.7.0-sun-plugin java-1.7.0-sun-jdbc java-1.7.0-sun-src java-1.7.0-sun-devel java-1.7.0-sun-javafx To install Java SE 8 and the related applications, use the [root@eapps-example ~]# yum install -y java-1.8.0-oracle java-1.8.0-oracle-plugin java-1.8.0-oracle-jdbc java-1.8.0-oracle-src java-1.8.0-oracle-devel java-1.8.0-oracle-javafx To install Tomcat, use the [root@eapps-example ~]# yum install -y tomcatX If you are going to use the mod_jk approach for Tomcat, you will need to install mod_jk using the [root@eapps-example ~]# yum install -y mod_jk If you are going to install your database from the command line, please see the specific user guide for that database:
Starting and Stopping TomcatUsing the Control PanelUsing ISPmanagerThe way to start, stop, and restart Tomcat from ISPmanager will depend on which version of ISPmanager you are using. Make sure to substitute your version of Tomcat for TomcatX.
Using the Command lineTo stop, start, or restart Tomcat from the command line, you will need to log in to the Virtual Server using SSH, and be able to work as the root user.
Tomcat Application deploymentYour Virtual Server includes the Apache web server as part of the base environment. Apache is designed to serve static HTML content, perl scripts, PHP applications, and other types of content that requires a web server. Tomcat is designed primarily to serve JSP pages and servlets. One of the decisions you will need to make when deploying your Tomcat application is how you want Tomcat and Apache to interact. In most cases you will want to use Apache as a front end to Tomcat. In very rare cases you may want to run Tomcat without Apache. Before you proceed you will need to determine which of the three approaches below is appropriate for you. The three supported approaches for deployment are:
For instructions on how to use each of the three supported approaches, please see the User Guide for that specific approach. Note that mod_jk and mod_proxy_ajp are covered in the same user guide. Tomcat ManagerThe Tomcat Manager application can be used to deploy WAR files, stop and reload Tomcat applications, and check the status of the Tomcat server. For more information on how to use and configure Tomcat Manager, click on the HTML Manager Help and Manager Help links in the Manager interface.
Enabling Tomcat ManagerTomcat Manager is enabled on a per-domain basis. This means that you will need to add the Context for the Tomcat Manager application to the Host block in server.xml for each domain where you want to access it. To enable Tomcat Manager, you will need to edit the server.xml file located in the /opt/tomcatX/conf directory (substitute X for your version of Tomcat). This is done from the command line of the Virtual Server, or from the File Manager of your Control Panel. Once you have finished editing server.xml, you will need to restart Tomcat. In server.xml find the Host block where you wish to add Tomcat Manager, and add this Context below the existing one:
When you're done, the entire Host block will look similar to this:
Remember, this is just an example, you will need to use your own domain name and appBase/docBase locations. Save and exit the file, and then restart Tomcat. Accessing Tomcat ManagerTo access the Tomcat Manager, go to http://eapps-example.com:8080/manager/html or http://IP_ADDRESS:8080/manager/html. Make sure to substitute your own domain name or IP address for eapps-example.com. You will be asked to log in to Tomcat Manager. The User Name is admin, and the Password is found in /opt/tomcatX/conf/tomcat-users.xml - look at the next to last line in that file. Changing the Tomcat Manager password To change the password for Tomcat Manager, edit the tomcat-users.xml file in the /opt/tomcatX/conf directory. The next to last line in the file will have the user name and password: <user username="admin" password="password" roles="tomcat,manager-gui,manager-status,admin-gui"/> Set the password to your desired password, and save and exit the file. Restart Tomcat for the changes to take effect. Tuning TomcatJava Heap Settings – an overviewOne of the most important parts of tuning Tomcat for better performance is to optimize the Java Heap settings. The Java Heap size is the amount of memory allocated to the Java Virtual Machine (JVM). The heap is where Java objects live, and there must be enough memory allocated to the JVM to support the needs of the deployed Tomcat applications.
How to find the correct Java Heap Settings for your Virtual ServerBy default Tomcat is configured to use a minimum of 64 MB of RAM and a maximum of 128 MB of RAM allocated to the heap. At a minimum, the maximum setting will need to change in order to correctly optimize Tomcat. To find the correct heap size, you will have to stop Tomcat, and use the Linux free -m command to see the available memory. Then you will need to multiply that value by two, and then subtract that result from the total RAM available for your plan. This number will be roughly what you need to set for the maximum heap size. All of this must be done from the command line of the Virtual Server, as the root user. [root@eapps-example ~]# free -m In this example, with Tomcat stopped, the Virtual Server and all other applications are using 285 MB of memory. Multiply this value by two, giving a total of 570 MB. Subtract that from the total amount of memory available to the Virtual Server (1024 MB), for a maximum value of 454 MB for the maximum heap size for the VS. Remember, this is only an example. Your Virtual Server + applications will produce a different result. You will find that you need to adjust this number up or down as necessary, depending on how your application is configured and what other services your Virtual Server is running. Generally, the maximum heap size should be just under half the amount of memory available to your VS. You can of course adjust this value higher or lower, but you may have to experiment to determine the true optimum heap size. However, do not set your maximum heap size to a number higher than your Virtual Server actually has available for memory. This will cause problems with Tomcat, including constant crashes. Adjusting the Java Heap settingsTo set the Java Heap for Tomcat you must modify the wrapper.java.initmemory and the wrapper.java.maxmemory settings in the /etc/tomcatX/tomcat.conf file. It is best to set the initmemory parameter lower than the maxmemory parameter to ensure that Java does not initialize more memory than is actually needed. This will optimize memory for non-Java processes. You can use either the File Manager in your Control Panel to change the Java Heap settings, or connect to the Virtual Server and edit the file from the command line. If you have the ISPmanager Control Panel, more information can be found here - File Manager for ISPmanager 4 or File Manager for ISPmanager 5. The default values for the Java Heap Size are 64 MB for wrapper.java.initmemory and 128 MB for wrapper.java.maxmemory. Change the value to your desired Java Heap settings, and save and exit the file. # Initial Java Heap Size (in MB) Once you have made the changes and saved the file, restart Tomcat for the new Java Heap settings to take affect. Use Database Connection Pooling (DBCP) When NecessaryDatabase connection pooling can reduce connection creation time and resource usage, especially on sites with a high load. The following steps can help you implement Database Connection Pooling for your application deployed on Tomcat. Install the correct JDBC driverIf you are using MySQL for you database, you will need to install the MySQL JBDC Driver application. For PostgreSQL, you will need the PostgreSQL JDBC Driver. Context ConfigurationAdd the lines below to your context configuration in your Host element in server.xml, in your application META-INF/context.xml, or in your /opt/tomcatX/conf/Catalina/hostname/context.xml. The exact location will depend on how you have deployed your application. For MySQL: <Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource" For PostgreSQL: <Resource name="jdbc/myDB" auth="Container" General Notes About the Parameters Used Above (from the apache.org Tomcat Documentation)
web.xml ConfigurationYou will also need to configure your application WEB-INF/web.xml file to use the database resource. In your web.xml file add a resource reference like the one below for both MySQL and PostgreSQL. <resource-ref> | |||||||
|