{"id":486,"date":"2012-07-04T17:00:33","date_gmt":"2012-07-04T09:00:33","guid":{"rendered":"http:\/\/rmohan.com\/?p=486"},"modified":"2012-07-04T17:05:21","modified_gmt":"2012-07-04T09:05:21","slug":"integrating-tomcat-and-apache-on-red-hat-linux","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=486","title":{"rendered":"Integrating Tomcat and Apache on Red Hat Linux"},"content":{"rendered":"<h2 id=\"a1_0\">Referance:\u00a0 http:\/\/www.meritonlinesystems.com\/docs\/apache_tomcat_redhat.html<\/h2>\n<p>1.0 Introduction<\/p>\n<p>Java servlets are a powerful tool for building websites and web based applications. One skill that every Java web developer should have is the ability to install and configure the Tomcat servlet engine. Many thanks to the <a href=\"http:\/\/www.apache.org\/\" target=\"_blank\">Apache Software Foundation<\/a> for providing this mature, stable, open source software. It was recently voted the <a href=\"http:\/\/www.infoworld.com\/article\/03\/07\/25\/29FErcamain_1.html?s=feature\" target=\"_blank\">Best Application Server of 2003<\/a> by InfoWorld readers.<\/p>\n<p>This article discusses how to integrate Tomcat with the Apache web server on Red Hat Linux 9 or Red Hat Enterprise Linux 3. The goal is to provide a simple, stable configuration that will allow users to gain confidence using Tomcat in a development environment. Setting up a production Tomcat server is outside the scope of this article.<\/p>\n<p>Please note the following conventions:<\/p>\n<ul>\n<li>All commands are issued as root unless otherwise noted.<\/li>\n<li>{YOUR_DOMAIN} and {YOUR_APPLICATION} are placeholder values that should be customized to your setup. For example, {YOUR_DOMAIN} might be \u201clocalhost.test\u201d and {YOUR_APPLICATION} might be \u201ctest\u201d.<\/li>\n<\/ul>\n<h2 id=\"a2_0\">2.0 Installing Apache<\/h2>\n<p>I chose to install Apache using the Red Hat RPM. Using the RPM instead of compiling Apache from source simplifies system administration in the following ways:<\/p>\n<ul>\n<li>Updates and bug fixes can be installed automatically from the <a href=\"http:\/\/rhn.redhat.com\/\" target=\"_blank\">Red Hat Network<\/a>.<\/li>\n<li>Startup and shutdown scripts are already configured and available.<\/li>\n<\/ul>\n<p>I recommend using the Red Hat up2date command line utility to install Red Hat RPMs. It eliminates a multitude of headaches by ensuring the software you install is the correct version and you have the right dependencies installed on your system.<\/p>\n<p>Install the following Red Hat RPMs if they are not already installed:<\/p>\n<ul>\n<li>httpd: the Apache web server<\/li>\n<li>httpd-devel: development tools that will be needed to create the mod_jk connector<\/li>\n<\/ul>\n<p>To install these packages using up2date, make sure you are connected to the Internet, and enter the following:<\/p>\n<p>&nbsp;<\/p>\n<pre>up2date -i httpd httpd-devel<\/pre>\n<p>You should now be able to start\/stop\/restart Apache as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>service httpd start\r\nservice httpd stop\r\nservice httpd restart<\/pre>\n<p>Verify that Apache is working by starting Apache and typing http:\/\/localhost\/ into your browser. You should see the default Apache install page with links to documentation.<\/p>\n<h2 id=\"a3_0\">3.0 Installing Tomcat<\/h2>\n<p>The only requirements to run Tomcat are that a Java Development Kit (JDK), also called a Java Software Development Kit (SDK), be installed and the JAVA_HOME environment variable be set.<\/p>\n<h3 id=\"a3_1\">3.1 Java SDK<\/h3>\n<p>I chose to install Sun\u2019s Java 2 Platform, Standard Edition, which can be downloaded from <a href=\"http:\/\/java.sun.com\/j2se\/\" target=\"_blank\">http:\/\/java.sun.com\/j2se\/)<\/a>. I chose the J2SE v1.4.2 SDK Linux self-extracting binary file.<\/p>\n<p>Change to the directory where you downloaded the SDK and make the self-extracting binary executable:<\/p>\n<p>&nbsp;<\/p>\n<pre>chmod +x j2sdk-1_4_2-linux-i586.bin<\/pre>\n<p>Run the self-extracting binary:<\/p>\n<p>&nbsp;<\/p>\n<pre>.\/j2sdk-1_4_2-linux-i586.bin<\/pre>\n<p>There should now be a directory called j2sdk1.4.2 in the download directory. Move the SDK directory to where you want it to be installed. I chose to install it in \/usr\/java. Create \/usr\/java if it doesn\u2019t exist. Here is the command I used from inside the download directory:<\/p>\n<p>&nbsp;<\/p>\n<pre>mv j2sdk1.4.2 \/usr\/java<\/pre>\n<p>Set the JAVA_HOME environment variable, by modifying \/etc\/profile so it includes the following:<\/p>\n<p>&nbsp;<\/p>\n<pre>JAVA_HOME=\"\/usr\/java\/j2sdk1.4.2\"\r\nexport JAVA_HOME<\/pre>\n<p>\/etc\/profile is run at startup and when a user logs into the system, so you will need to log out and log back in for JAVA_HOME to be defined.<\/p>\n<p>&nbsp;<\/p>\n<pre>exit\r\nsu -<\/pre>\n<p>Check to make sure JAVA_HOME is defined correctly using the command below. You should see the path to your Java SDK.<\/p>\n<p>&nbsp;<\/p>\n<pre>echo $JAVA_HOME<\/pre>\n<h3 id=\"a3_2\">3.2 Tomcat Account<\/h3>\n<p>You will install and configure Tomcat as root; however, you should create a dedicated group and user account for Tomcat to run under as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>groupadd tomcat\r\nuseradd -g tomcat tomcat<\/pre>\n<h3 id=\"a3_3\">3.3 Download Tomcat<\/h3>\n<p>Download the latest release binary build from <a href=\"http:\/\/www.apache.org\/dist\/jakarta\/tomcat-5\/\" target=\"_blank\">http:\/\/www.apache.org\/dist\/jakarta\/tomcat-5\/<\/a>. Since Tomcat runs directly on top of a standard JDK, I cannot think of any reason to building it from source. There are a number of different download formats. I chose the gnu zipped tar file (jakarta-tomcat-5.0.28.tar.gz).<\/p>\n<h3 id=\"a3_4\">3.4 Tomcat Standalone<\/h3>\n<p>Unzip Tomcat by issuing the following command from your download directory:<\/p>\n<p>&nbsp;<\/p>\n<pre>tar xvzf jakarta-tomcat-5.0.28.tar.gz<\/pre>\n<p>This will create a directory called jakarta-tomcat-5.0.28. Move this directory to wherever you would like to install Tomcat. I chose \/usr\/local. Here is the command I issued from inside the download directory:<\/p>\n<p>&nbsp;<\/p>\n<pre>mv jakarta-tomcat-5.0.28 \/usr\/local\/<\/pre>\n<p>The directory where Tomcat is installed is referred to as CATALINA_HOME in the Tomcat documentation. In this installation CATALINA_HOME=\/usr\/local\/jakarta-tomcat-5.0.28.<\/p>\n<p>I recommend setting up a symbolic link to point to your current Tomcat version. This will save you from having to make changes to startup and shutdown scripts each time you upgrade Tomcat. It also allows you to keep several versions of Tomcat on your system and easily switch amongst them. Here is the command I issued from inside \/usr\/local to create a symbolic link called \/usr\/local\/jakarta-tomcat that points to \/usr\/local\/jakarta-tomcat-5.0.28:<\/p>\n<p>&nbsp;<\/p>\n<pre>ln -s jakarta-tomcat-5.0.28 jakarta-tomcat<\/pre>\n<p>Change the group and owner of the \/usr\/local\/jakarta-tomcat and \/usr\/local\/jakarta-tomcat-5.0.28 directories to tomcat:<\/p>\n<p>&nbsp;<\/p>\n<pre>chown tomcat.tomcat \/usr\/local\/jakarta-tomcat\r\nchown -R tomcat.tomcat \/usr\/local\/jakarta-tomcat-5.0.28<\/pre>\n<p>It is not necessary to set the CATALINA_HOME environment variable. Tomcat is smart enough to figure out CATALINA_HOME on its own.<\/p>\n<p>You should now be able to start and stop Tomcat from the CATALINA_HOME\/bin directory by typing .\/startup.sh and .\/shutdown.sh respectively. Test that Tomcat is working by starting it and typing http:\/\/localhost:8080 into your browser. You should see the Tomcat welcome page with links to documentation and sample code. Verify Tomcat is working by running some of the examples.<\/p>\n<h2 id=\"a4_0\">4.0 Installing the Connector<\/h2>\n<h3 id=\"a4_1\">4.1 Connector Benefits<\/h3>\n<p>At this point, Apache and Tomcat should be working separately in standalone mode. You can run Tomcat in standalone mode as an alternative to Apache. In fact, in some cases, it is said that Tomcat standalone is faster than serving static content from Apache and dynamic content from Tomcat. However, there are the following compelling reasons to use Apache as the front end:<\/p>\n<ol>\n<li>You can use Apache to buffer slow connections. Tomcat uses java.io, which uses a thread for each request, so Tomcat can run out of connections as the number of slow requests grows. This could be an issue if your application supports a large number of dial-up users.<\/li>\n<li>You can use a connector such as mod_jk to load balance amongst several Tomcat instances.<\/li>\n<li>You can take advantage of Apache features such as cgi and PHP.<\/li>\n<li>You can take advantage of Apache modules such as mod_rewrite, mod_headers, and mod_expire.<\/li>\n<li>You can isolate virtual hosts in their own Tomcat instances.<\/li>\n<\/ol>\n<p>The increased functionality obtained by using Apache on the front end can outweigh the effort required to install and configure a connector.<\/p>\n<h3 id=\"a4_2\">4.2 Selecting a Connector<\/h3>\n<p>Development on the mod_jk2 connector was discontinued on 11\/15\/2004; therefore, you no longer have to decide between the mod_jk and mod_jk2 connectors. Use the mod_jk connector. It has been around a long while and is very stable.<\/p>\n<h3 id=\"a4_3\">4.3 Building the mod_jk Connector<\/h3>\n<p>The mod_jk connector is the communication link between Apache and Tomcat. It listens on a defined port for requests from Apache and forwards those requests to Tomcat.<\/p>\n<p>Install the following Red Hat RPMs if they are not already installed:<\/p>\n<ul>\n<li>libtool<\/li>\n<li>automake<\/li>\n<li>autoconf<\/li>\n<\/ul>\n<p>Download the jk connector source from <a href=\"http:\/\/www.apache.org\/dist\/jakarta\/tomcat-connectors\/jk\/\" target=\"_blank\">http:\/\/www.apache.org\/dist\/jakarta\/tomcat-connectors\/jk\/<\/a>. I used jakarta-tomcat-connectors-1.2.8-src.tar.gz.<\/p>\n<p>Unzip the contents of the file into your download directory as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>tar xvzf jakarta-tomcat-connectors-1.2.8-src.tar.gz<\/pre>\n<p>This will create a folder called jakarta-tomcat-connectors-1.2.8-src. Move this folder to wherever you store source files on your system. I chose \/usr\/src. Here is the command I issued from inside the download directory:<\/p>\n<p>&nbsp;<\/p>\n<pre>mv jakarta-tomcat-connectors-1.2.8-src \/usr\/src<\/pre>\n<p>I refer to the folder where the connector source is installed as CONN_SRC_HOME. In my case CONN_SRC_HOME = \/usr\/src\/jakarta-tomcat-connectors-1.2.8-src.<\/p>\n<p>Change to directory CONN_SRC_HOME\/jk\/native and run the buildconf.sh script as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>.\/buildconf.sh<\/pre>\n<p>This will create the CONN_SRC_HOME\/jk\/native\/configure file. Run the configure script with the path to the apxs file on your system. If you installed Apache using the Red Hat RPM, apxs should be located at \/usr\/sbin\/apxs.<\/p>\n<p>&nbsp;<\/p>\n<pre>.\/configure --with-apxs=\/usr\/sbin\/apxs<\/pre>\n<p>Build mod_jk with the following command:<\/p>\n<p>&nbsp;<\/p>\n<pre>make<\/pre>\n<p>If you see missing object errors, try this alternate command:<\/p>\n<p>&nbsp;<\/p>\n<pre>make LIBTOOL=\/etc\/httpd\/build\/libtool<\/pre>\n<p>If all went well, the mod_jk.so file was successfully created. Manually copy it to Apache\u2019s shared object files directory:<\/p>\n<p>&nbsp;<\/p>\n<pre>cp CONN_SRC_HOME\/jk\/native\/apache-2.0\/mod_jk.so \/etc\/httpd\/modules<\/pre>\n<h2 id=\"a5_0\">5.0 Creating the Application Directories<\/h2>\n<p>Both Apache and Tomcat application files will be installed under the \/home\/tomcat\/webapps directory. This setup will allow you to easily upgrade Tomcat and back up your application files.<\/p>\n<p>Create your application directories as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>mkdir \/home\/tomcat\/webapps\r\nmkdir \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\r\nmkdir \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/logs\r\nmkdir \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/{YOUR_APPLICATION}\r\nmkdir \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/{YOUR_APPLICATION}\/WEB-INF\r\nmkdir \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/{YOUR_APPLICATION}\/WEB-INF\/classes<\/pre>\n<p>Set directory permissions as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>chmod 755 \/home\/tomcat\/webapps\r\nchmod 755 \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\r\nchmod 755 \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/{YOUR_APPLICATION}<\/pre>\n<h2 id=\"a6_0\">6.0 Configuring Tomcat<\/h2>\n<h3 id=\"a6_1\">6.1 workers.properties<\/h3>\n<p>The workers.properties file contains information so mod_jk can connect to the Tomcat worker processes.<\/p>\n<p>Place the following workers.properties file in the \/etc\/httpd\/conf directory:<\/p>\n<p>&nbsp;<\/p>\n<pre># workers.properties - ajp13\r\n#\r\n# List workers\r\nworker.list=wrkr\r\n#\r\n# Define wrkr\r\nworker.wrkr.port=8009\r\nworker.wrkr.host=localhost\r\nworker.wrkr.type=ajp13\r\nworker.wrkr.cachesize=10\r\nworker.wrkr.cache_timeout=600\r\nworker.wrkr.socket_timeout=300<\/pre>\n<p>Notes<\/p>\n<ol>\n<li>There is an example workers.properties file located in the CONN_SRC_HOME\/jk\/conf directory. The example file provides a lot of useful information and insight into the workers.properties file, but it contains so much information that it can be confusing. I recommend using it as a learning tool but creating your own workers.properties file from scratch.<\/li>\n<li>The configuration above assumes Apache and Tomcat are located on the same box and requests are forwarded to Tomcat using type ajp13 workers. Type ajp13 workers forward requests to out-of-process Tomcat workers using the ajpv13 protocol over TCP\/IP sockets.<\/li>\n<li>The name of the worker in the JkMount directive in httpd.conf must match the name of the worker in worker.list (\u201cwrkr\u201d in the configuration above).<\/li>\n<\/ol>\n<h3 id=\"a6_2\">6.2 server.xml<\/h3>\n<p>The CATALINA_HOME\/conf\/server.xml file contains Tomcat server configuration information. The default server.xml is great for verifying that Tomcat works in standalone mode and for viewing the examples that come with the application, but it contains so much information that it can be confusing. I recommend saving it for future reference and creating a new server.xml.<\/p>\n<p>Save the default server.xml as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>mv CATALINA_HOME\/conf\/server.xml CATALINA_HOME\/conf\/server.xml.orig<\/pre>\n<p>Copy the following into a new server.xml file:<\/p>\n<p>&nbsp;<\/p>\n<pre>&lt;Server port=\"8005\" shutdown=\"0fbb9aebcbfbef203eca71b6be367859\" debug=\"0\"&gt;\r\n\r\n\t&lt;Service name=\"Tomcat-Apache\"&gt;\r\n\r\n\t\t&lt;Connector address=\"127.0.0.1\"\r\n\t\t\tport=\"8009\"\r\n\t\t\tminProcessors=\"5\"\r\n\t\t\tmaxProcessors=\"75\"\r\n\t\t\tenableLookups=\"false\"\r\n\t\t\tprotocol=\"AJP\/1.3\"\r\n\t\t\tdebug=\"0\"\/&gt;\r\n\r\n\t\t&lt;Engine name=\"appserver\"\r\n\t\t\tdebug=\"0\"\r\n\t\t\tdefaultHost=\"{YOUR_DOMAIN}\"&gt;\r\n\r\n\t\t\t&lt;Host name=\"{YOUR_DOMAIN}\"\r\n\t\t\t\tappBase=\"\/home\/tomcat\/webapps\"\r\n\t\t\t\tautoDeploy=\"false\"\r\n\t\t\t\tdeployOnStartup=\"false\"\r\n\t\t\t\tunpackWARs=\"false\"\r\n\t\t\t\tdeployXML=\"true\"\r\n\t\t\t\tdebug=\"0\"\/&gt;\r\n\r\n\t\t&lt;\/Engine&gt;\r\n\r\n\t&lt;\/Service&gt;\r\n\r\n&lt;\/Server&gt;<\/pre>\n<p>If you do keep the default server.xml, make sure you comment out any other connectors besides mod_jk that are listening on port 8009. The default file comes with the Coyote\/JK2 connector enabled for the Tomcat-Standalone service. This will conflict with the mod_jk connector in your Tomcat-Apache service. You should comment this connector out. It isn\u2019t needed when you connect directly to Tomcat in standalone mode (port 8080), so I\u2019m not sure why this connector is enabled by default.<\/p>\n<p>The Connector address defines the interface that Tomcat will listen on for mod_jk requests from Apache. In my configuration, Apache and Tomcat reside on the same box, so I have set the address to the loopback address. The default is for Tomcat to listen on all interfaces, so restricting it to the loopback interface improves security.<\/p>\n<p>The Server shutdown property is the text string that is sent over a socket connection to stop Tomcat. The default value is \u201cSHUTDOWN\u201d. The shutdown port is always on the loopback interface, which provides host-level protection. However, there is the possibility that the host could be compromised and someone could send the command SHUTDOWN to all ports and knock Tomcat offline. To prevent this, replace the default value with one that is difficult to guess. <strong>Do not use the example string above.<\/strong> Create your own by feeding random bytes into md5sum as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>head -1024c \/dev\/random | md5sum<\/pre>\n<p>Change the permissions on server.xml to prevent unprivileged users from reading the shutdown string:<\/p>\n<p>&nbsp;<\/p>\n<pre>chmod 600 CATALINA_HOME\/conf\/server.xml<\/pre>\n<h3 id=\"a6_3\">6.3 Configuring the Context<\/h3>\n<p>It is recommended that contexts be defined in separate files, not in server.xml. The context configuration directory has the same name as the Engine in server.xml.<\/p>\n<p>Create the context configuration directory as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>mkdir CATALINA_HOME\/conf\/appserver<\/pre>\n<p>The host context configuration directory has the same name as the corresponding Host in server.xml.<\/p>\n<p>Create the context configuration directory for your domain as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>mkdir CATALINA_HOME\/conf\/appserver\/{YOUR_DOMAIN}<\/pre>\n<p>Create the context configuration file as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>touch CATALINA_HOME\/conf\/appserver\/{YOUR_DOMAIN}\/{YOUR_APPLICATION}.xml<\/pre>\n<p>Copy the following text into {YOUR_APPLICATION}.xml:<\/p>\n<p>&nbsp;<\/p>\n<pre>&lt;Context path=\"\"\r\n\tdocBase=\"{YOUR_APPLICATION}\"\r\n\treloadable=\"true\"\r\n\tdebug=\"0\"\/&gt;<\/pre>\n<p>Setting the Context reloadable property to true tells Tomcat to automatically load new and changed application class files found in \/WEB-INF\/classes and \/WEB-INF\/lib. This feature is very useful during development. However, it is recommended to <strong>set reloadable to false in production environments<\/strong> because monitoring class file changes requires significant server resources.<\/p>\n<h3 id=\"a6_4\">6.4 log4j<\/h3>\n<p>Download the latest <a href=\"http:\/\/logging.apache.org\/log4j\/\" target=\"_blank\">log4j<\/a> and install log4j.jar in CATALINA_HOME\/common\/lib.<\/p>\n<p>Download the latest <a href=\"http:\/\/jakarta.apache.org\/commons\/logging\/\" target=\"_blank\">commons logging<\/a> and install commons-logging.jar in CATALINA_HOME\/common\/lib.<\/p>\n<p>Create a file called log4j.properties as follows and place it in the CATALINA_HOME\/common\/classes directory:<\/p>\n<p>&nbsp;<\/p>\n<pre>log4j.rootLogger=WARN, R\r\nlog4j.appender.R=org.apache.log4j.RollingFileAppender\r\nlog4j.appender.R.File=\/usr\/local\/jakarta-tomcat\/logs\/tomcat.log\r\nlog4j.appender.R.MaxFileSize=10MB\r\nlog4j.appender.R.MaxBackupIndex=10\r\nlog4j.appender.R.layout=org.apache.log4j.PatternLayout\r\nlog4j.appender.R.layout.ConversionPattern=%d{DATE} - %p %c - %m%n\r\nlog4j.logger.org.apache.catalina=WARN, R<\/pre>\n<h2 id=\"a7_0\">7.0 Configuring Apache<\/h2>\n<p>Apache is configured with directives placed in the main Apache configuration file, \/etc\/httpd\/conf\/httpd.conf. In addition, Apache 2 has configuration files for perl, php, and ssl located in \/etc\/httpd\/conf.d\/.<\/p>\n<p>Rename the \/etc\/httpd\/conf.d\/ssl.conf file to ssl.conf.bak. The default Red Hat Apache 2 installation comes with ssl support enabled. If ssl is needed, you can re-enable it after you have successfully integrated Apache and Tomcat.<\/p>\n<h3 id=\"a7_1\">7.1 httpd.conf<\/h3>\n<p>You will notice that there are three sections labeled in the httpd.conf file supplied by Red Hat: (1) Global Environment, (2) Main Server Configuration, and (3) Virtual Hosts.<\/p>\n<p>Add the following to the bottom of the existing LoadModule directives in the Global Environment section:<\/p>\n<p>&nbsp;<\/p>\n<pre>LoadModule jk_module modules\/mod_jk.so<\/pre>\n<p>Add the following to the bottom of the Main Server Configuration section:<\/p>\n<p>&nbsp;<\/p>\n<pre>JkWorkersFile \"\/etc\/httpd\/conf\/workers.properties\"\r\nJkLogFile \"\/etc\/httpd\/logs\/mod_jk.log\"\r\nJkLogLevel info\r\nJkLogStampFormat \"[%a %b %d %H:%M:%S %Y]\"<\/pre>\n<p>Set up a Virtual Host directive in the Virtual Hosts section of httpd.conf. Below is an example of how to set up the {YOUR_DOMAIN} website so Tomcat handles all jsp pages and requests with \u201cservlet\u201d in the path:<\/p>\n<p>&nbsp;<\/p>\n<pre>NameVirtualHost 127.0.0.1:80\r\n\r\n&lt;VirtualHost 127.0.0.1:80&gt;\r\n\tServerAdmin webmaster@{YOUR_DOMAIN}\r\n\tServerName {YOUR_DOMAIN}\r\n\tDocumentRoot \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/{YOUR_APPLICATION}\r\n\tErrorLog \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/logs\/error_log\r\n\tCustomLog \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/logs\/access_log common\r\n\tJkMount \/*.jsp wrkr\r\n\tJkMount \/servlet\/* wrkr\r\n\t# Deny direct access to WEB-INF\r\n\t&lt;LocationMatch \".*WEB-INF.*\"&gt;\r\n\t\tAllowOverride None\r\n\t\tdeny from all\r\n\t&lt;\/LocationMatch&gt;\r\n&lt;\/VirtualHost&gt;<\/pre>\n<p>The argument for the NameVirtualHost directive must match exactly the argument for the VirtualHost directive (127.0.0.1:80).<\/p>\n<p>The JkMount directive specifies url patterns of requests that will be forwarded to Tomcat for processing.<\/p>\n<p>You can test your Apache configuration by typing the following:<\/p>\n<p>&nbsp;<\/p>\n<pre>httpd -t -D DUMP_VHOSTS<\/pre>\n<p>You should get something like the following response:<\/p>\n<p>&nbsp;<\/p>\n<pre>127.0.0.1:80           is a NameVirtualHost\r\n         default server {YOUR_DOMAIN} (\/etc\/httpd\/conf\/httpd.conf:1056)\r\n         port 80 namevhost {YOUR_DOMAIN} (\/etc\/httpd\/conf\/httpd.conf:1056)\r\nSyntax OK<\/pre>\n<h2 id=\"a8_0\">8.0 Setting Up {YOUR_DOMAIN}<\/h2>\n<p>{YOUR_DOMAIN} does not need to be a domain name with a DNS entry. For testing purposes, you can set up any domain you want in the \/etc\/hosts file of the machine that you will be using to access your application.<\/p>\n<p>The example below shows the entry for {YOUR_DOMAIN} when running Apache and Tomcat on a single machine, typical for a development computer.<\/p>\n<p>&nbsp;<\/p>\n<pre>127.0.0.1\t{YOUR_DOMAIN}<\/pre>\n<h2 id=\"a9_0\">9.0 Testing Apache<\/h2>\n<p>We will create and install a simple Hello World html page so we can test to make sure Apache handles requests for static html pages.<\/p>\n<h3 id=\"a9_1\">9.1 Hello World HTML<\/h3>\n<p>Copy the following text into a file called HelloWorld.html and install the file in the \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/{YOUR_APPLICATION} directory.<\/p>\n<p>&nbsp;<\/p>\n<pre>&lt;!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\"&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\"&gt;\r\n&lt;title&gt;Hello World HTML!&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;Hello World HTML!&lt;\/h1&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>If Apache has not been restarted since you added your virtual host, do so as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>service httpd restart<\/pre>\n<p>You should now be able to type http:\/\/{YOUR_DOMAIN}\/HelloWorld.html into your browser and see the always-exciting \u201cHello World\u201d message.<\/p>\n<h2 id=\"a10_0\">10.0 Testing Tomcat<\/h2>\n<p>We will create and install a simple Hello World jsp page and servlet so we can test to make sure Apache forwards servlet requests to Tomcat for handling.<\/p>\n<h3 id=\"a10_1\">10.1 Hello World JSP<\/h3>\n<p>Copy the following text into a file called HelloWorld.jsp and install the file in the \/home\/tomcat\/webapps\/{YOUR_DOMAIN} directory.<\/p>\n<p>&nbsp;<\/p>\n<pre>&lt;%@ page contentType=\"text\/html;charset=WINDOWS-1252\"%&gt;\r\n&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\"&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=\"content-type\" content=\"text\/html; charset=ISO-8859-1\"&gt;\r\n&lt;title&gt;Hello World JSP&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;&lt;% out.println(\" Hello World JSP!\"); %&gt;&lt;\/h1&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h3 id=\"a10_2\">10.2 Hello World Servlet<\/h3>\n<p>Copy the following into a file called HelloWorld.java:<\/p>\n<p>&nbsp;<\/p>\n<pre>import java.io.*;\r\nimport javax.servlet.*;\r\nimport javax.servlet.http.*;\r\npublic class HelloWorld\r\n    extends HttpServlet {\r\n    public void doGet(HttpServletRequest request, \r\n                       HttpServletResponse response)\r\n                throws IOException, ServletException {\r\n\r\n\t\tresponse.setContentType(\"text\/html\");\r\n\t\tPrintWriter out = response.getWriter();\r\n\r\n\t\tout.println(\"Hello World Servlet!\");\r\n\r\n\t}\r\n\r\n}<\/pre>\n<p>Compile the source into a class file as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>javac -classpath \/usr\/local\/jakarta-tomcat\/common\/lib\/servlet.jar HelloWorld.java<\/pre>\n<p>This will create a file called HelloWorld.class. Copy the HelloWorld.class file to the \/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/{YOUR_APPLICATION}\/WEB-INF\/classes directory.<\/p>\n<h3 id=\"a10_3\">10.3 Tomcat Application<\/h3>\n<p>The web.xml file is where a servlet name is mapped to a URL pattern so Tomcat can run your servlet when requested. Below is the web.xml file that runs the HelloWorld servlet whenever the URL http:\/\/{YOUR_DOMAIN}\/servlet\/HelloWorld is entered in the browser:<\/p>\n<p>&nbsp;<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"ISO-8859-1\"?&gt;\r\n\r\n&lt;!DOCTYPE web-app\r\n    PUBLIC \"-\/\/Sun Microsystems, Inc.\/\/DTD Web Application 2.3\/\/EN\"\r\n    \"http:\/\/java.sun.com\/dtd\/web-app_2_3.dtd\"&gt;\r\n\r\n&lt;web-app&gt;\r\n\r\n\t&lt;servlet&gt;\r\n\t\t&lt;servlet-name&gt;HelloWorld&lt;\/servlet-name&gt;\r\n\t\t&lt;servlet-class&gt;HelloWorld&lt;\/servlet-class&gt;\r\n\t&lt;\/servlet&gt;\r\n\t&lt;servlet-mapping&gt;\r\n\t\t&lt;servlet-name&gt;HelloWorld&lt;\/servlet-name&gt;\r\n\t\t&lt;url-pattern&gt;\/servlet\/HelloWorld&lt;\/url-pattern&gt;\r\n\t&lt;\/servlet-mapping&gt;\r\n\r\n&lt;\/web-app&gt;<\/pre>\n<p>Restart Tomcat as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>\/CATALINA_HOME\/bin\/shutdown.sh\r\n\/CATALINA_HOME\/bin\/startup.sh<\/pre>\n<p>Restart Apache as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>service httpd restart<\/pre>\n<p>You should now be able to type the following into your browser and see the always-exciting \u201cHello World\u201d message:<\/p>\n<p>http:\/\/{YOUR_DOMAIN}\/HelloWorld.jsp<\/p>\n<p>http:\/\/{YOUR_DOMAIN}\/servlet\/HelloWorld<\/p>\n<h2 id=\"a11_0\">11.0 Advanced Configuration<\/h2>\n<p>The following steps are not mandatory, but are suggested for a better, tighter Tomcat installation.<\/p>\n<h3 id=\"a11_1\">11.1 Tomcat Startup Script<\/h3>\n<p>If you want to automatically start Tomcat when your system boots and manage it using the service command as we do Apache, you must create an initialization script.<\/p>\n<p>Copy the following text into a file called tomcat and install the file in the \/etc\/rc.d\/init.d directory.<\/p>\n<p>&nbsp;<\/p>\n<pre>#!\/bin\/sh\r\n#\r\n# Startup script for Tomcat Servlet Engine\r\n#\r\n# chkconfig: 345 86 14\r\n# description: Tomcat Servlet Engine\r\n# processname: tomcat\r\n# pidfile: \/usr\/local\/jakarta-tomcat\/bin\/tomcat.pid\r\n#\r\n\r\n# User under which tomcat will run\r\nTOMCAT_USER=tomcat\r\n\r\nRETVAL=0\r\n\r\n# start, debug, stop, and status functions\r\nstart() {\r\n    # Start Tomcat in normal mode\r\n    SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n    if [ $SHUTDOWN_PORT -ne 0 ]; then\r\n        echo \"Tomcat already started\"\r\n    else\r\n        echo \"Starting tomcat...\"\r\n        chown -R $TOMCAT_USER:$TOMCAT_USER \/usr\/local\/jakarta-tomcat\/*\r\n        chown -R $TOMCAT_USER:$TOMCAT_USER \/home\/tomcat\/*\r\n        su -l $TOMCAT_USER -c '\/usr\/local\/jakarta-tomcat\/bin\/startup.sh'\r\n\tSHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n        while [ $SHUTDOWN_PORT -eq 0 ]; do\r\n            sleep 1\r\n            SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n        done\t\r\n        RETVAL=$?\r\n        echo \"Tomcat started in normal mode\"\r\n        [ $RETVAL=0 ] &amp;&amp; touch \/var\/lock\/subsys\/tomcat\r\n    fi\r\n}\r\n\r\ndebug() {\r\n    # Start Tomcat in debug mode\r\n    SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n    if [ $SHUTDOWN_PORT -ne 0 ]; then\r\n        echo \"Tomcat already started\"\r\n    else\r\n        echo \"Starting tomcat in debug mode...\"\r\n        chown -R $TOMCAT_USER:$TOMCAT_USER \/usr\/local\/jakarta-tomcat\/*\r\n        chown -R $TOMCAT_USER:$TOMCAT_USER \/home\/tomcat\/*\r\n        su -l $TOMCAT_USER -c '\/usr\/local\/jakarta-tomcat\/bin\/catalina.sh jpda start'\r\n\tSHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n        while [ $SHUTDOWN_PORT -eq 0 ]; do\r\n            sleep 1\r\n            SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n        done\t\r\n        RETVAL=$?\r\n        echo \"Tomcat started in debug mode\"\r\n        [ $RETVAL=0 ] &amp;&amp; touch \/var\/lock\/subsys\/tomcat\r\n    fi\r\n}\r\n\r\nstop() {\r\n    SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n    if [ $SHUTDOWN_PORT -eq 0 ]; then\r\n        echo \"Tomcat already stopped\"\r\n    else\r\n        echo \"Stopping tomcat...\"\r\n        su -l $TOMCAT_USER -c '\/usr\/local\/jakarta-tomcat\/bin\/shutdown.sh'\r\n\tSHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n        while [ $SHUTDOWN_PORT -ne 0 ]; do\r\n            sleep 1\r\n            SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n        done\r\n\tRETVAL=$?\r\n        echo \"Tomcat stopped\"\r\n        [ $RETVAL=0 ] &amp;&amp; rm -f \/var\/lock\/subsys\/tomcat \/usr\/local\/jakarta-tomcat\/bin\/tomcat.pid\r\n    fi\r\n}\r\n\r\nstatus() {\r\n    SHUTDOWN_PORT=`netstat -vatn|grep LISTEN|grep 8005|wc -l`\r\n    if [ $SHUTDOWN_PORT -eq 0 ]; then\r\n        echo \"Tomcat stopped\"\r\n    else\r\n        MODE=\"normal\"\r\n        JPDA_PORT=`netstat -vatn|grep LISTEN|grep 8000|wc -l`\r\n        if [ $JPDA_PORT -ne 0 ]; then\r\n            MODE=\"debug\"\r\n        fi\r\n\techo \"Tomcat running in $MODE mode\"\r\n    fi\r\n}\r\n\r\ncase \"$1\" in\r\n  start)\r\n        start\r\n        ;;\r\n  debug)\r\n        debug\r\n        ;;\r\n  stop)\r\n        stop\r\n        ;;\r\n  restart)\r\n        stop\r\n        start\r\n        ;;\r\n  redebug)\r\n        stop\r\n        debug\r\n        ;;\r\n  status)\r\n  \tstatus\r\n\t;;\r\n  *)\r\n\techo \"Usage: $0 {start|debug|stop|restart|redebug|status}\"\r\n\texit 1\r\nesac\r\n\r\nexit $RETVAL<\/pre>\n<p>Add the startup script to your system as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>chkconfig --add tomcat<\/pre>\n<p>The path of the file that contains the pid of the catalina startup java process can be set with the CATALINA_PID environment variable. CATALINA_HOME\/bin\/catalina.sh automatically calls a file called setenv.sh if it exists, so this is a good place to set environment variables.<\/p>\n<p>Create setenv.sh as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>cd CATALINA_HOME\/bin\r\ntouch setenv.sh\r\nchmod 644 setenv.sh<\/pre>\n<p>Copy the following text into setenv.sh:<\/p>\n<p>&nbsp;<\/p>\n<pre>CATALINA_PID=\/usr\/local\/jakarta-tomcat\/bin\/tomcat.pid<\/pre>\n<p>Now you will be able to start\/stop\/restart\/status Tomcat using the following commands:<\/p>\n<p>&nbsp;<\/p>\n<pre>service tomcat start\r\nservice tomcat stop\r\nservice tomcat restart\r\nservice tomcat status<\/pre>\n<p>If you want Tomcat to start automatically when your system boots, you need to add tomcat to your runlevel as follows:<\/p>\n<p>&nbsp;<\/p>\n<pre>chkconfig --level 5 tomcat on<\/pre>\n<p>Runlevel 5 is the X Window System, typical for a development computer. Runlevel 3 is typical for a dedicated web server.<\/p>\n<p>Apache and Tomcat can be started and restarted in any order. In the past (specifically with the 1.2.5 connector), if Tomcat was restarted, Apache would have to be restarted. This was because the AJP13 protocol maintains open sockets between Apache and Tomcat, and when Tomcat was restarted the connections would be hung in CLOSE_WAIT status until Apache was restarted. This has been fixed starting with the 1.2.6 connector.<\/p>\n<h3 id=\"a11_2\">11.2 Development Setup<\/h3>\n<p>During development, you will need access to your tomcat application directory. Add the user account under which you will be doing development to the tomcat group in \/etc\/group. For example, this is what the tomcat entry might look like in \/etc\/group if you do development under the yourname account:<\/p>\n<p>&nbsp;<\/p>\n<pre>tomcat:x:502:yourname<\/pre>\n<p>Make sure the tomcat group has permission to publish files (e.g. using ant) to your Tomcat application in \/home\/tomcat\/webapps\/{YOUR_DOMAIN}. Issue the following commands as root:<\/p>\n<p>&nbsp;<\/p>\n<pre>chmod g+x \/home\/tomcat\r\nchmod -R g+rw \/home\/tomcat<\/pre>\n<h2 id=\"a12_0\">12.0 Troubleshooting<\/h2>\n<h3 id=\"a12_1\">12.1 Log Files To Watch<\/h3>\n<h4>\/home\/tomcat\/webapps\/{YOUR_DOMAIN}\/logs\/error_log<\/h4>\n<p>Look here for clues to Apache httpd.conf configuration issues, for example VirtualHost setup.<\/p>\n<h4>CATALINA_HOME\/logs\/catalina.out<\/h4>\n<p>Look here for clues to Tomcat server.xml configuration issues. This file is written to when Tomcat starts and stops. It also catches System.out and System.err.<\/p>\n<h4>CATALINA_HOME\/logs\/tomcat.log<\/h4>\n<p>Look here for clues to all Tomcat issues. This is the main Tomcat log file.<\/p>\n<h4>\/etc\/httpd\/logs\/mod_jk.log<\/h4>\n<p>Look here for clues to mod_jk configuration issues.<\/p>\n<h3 id=\"a12_2\">12.2 Monitoring Connections<\/h3>\n<p>The following command can be used to monitor the Apache, Tomcat, and mod_jk connections:<\/p>\n<p>&nbsp;<\/p>\n<pre>netstat -vatn | grep 80<\/pre>\n<p>Below is output from running this command. Line numbers have been added to the beginning of each line for discussion purposes.<\/p>\n<p>&nbsp;<\/p>\n<pre>1 tcp        0      0 127.0.0.1:8005          0.0.0.0:*               LISTEN\r\n2 tcp        0      0 127.0.0.1:8009          0.0.0.0:*               LISTEN\r\n3 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN\r\n4 tcp        0      0 127.0.0.1:8009          127.0.0.1:34449         ESTABLISHED\r\n5 tcp        0      0 127.0.0.1:34449         127.0.0.1:8009          ESTABLISHED<\/pre>\n<p>Notes<\/p>\n<ol>\n<li>Line 1 shows Tomcat listening on port 8005 for the shutdown command.<\/li>\n<li>Line 2 shows Tomcat listening on port 8009 for requests from Apache.<\/li>\n<li>Line 3 shows Apache listening on port 80 for user requests.<\/li>\n<li>Line 4 shows the Tomcat end of a mod_jk connection.<\/li>\n<li>Line 5 shows the Apache end of a mod_jk connection.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Referance: http:\/\/www.meritonlinesystems.com\/docs\/apache_tomcat_redhat.html <\/p>\n<p>1.0 Introduction<\/p>\n<p>Java servlets are a powerful tool for building websites and web based applications. One skill that every Java web developer should have is the ability to install and configure the Tomcat servlet engine. Many thanks to the Apache Software Foundation for providing this mature, stable, open source software. It was recently [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/486"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=486"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":488,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions\/488"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}