1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
|
<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>Integrating Tomcat and Apache on RedHat 9 LG #95</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<!-- *** BEGIN navbar *** -->
<A HREF="hughes.html"><< Prev</A> | <A HREF="index.html">TOC</A> | <A HREF="../index.html">Front Page</A> | <A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue95/millson.html">Talkback</A> | <A HREF="../faq/index.html">FAQ</A> | <A HREF="pramode.html">Next >></A>
<!-- *** END navbar *** -->
<!--endcut ============================================================-->
<TABLE BORDER><TR><TD WIDTH="200">
<A HREF="http://www.linuxgazette.com/">
<IMG ALT="LINUX GAZETTE" SRC="../gx/2002/lglogo_200x41.png"
WIDTH="200" HEIGHT="41" border="0"></A>
<BR CLEAR="all">
<SMALL>...<I>making Linux just a little more fun!</I></SMALL>
</TD><TD WIDTH="380">
<CENTER>
<BIG><BIG><STRONG><FONT COLOR="maroon">Integrating Tomcat and Apache on RedHat 9</FONT></STRONG></BIG></BIG>
<BR>
<STRONG>By <A HREF="../authors/millson.html">Mike Millson</A></STRONG>
</CENTER>
</TD></TR>
</TABLE>
<P>
<!-- END header -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Integrating Tomcat and Apache on RedHat 9.0</title>
</head>
<body>
<h1>Integrating Tomcat and Apache on RedHat 9.0</h1>
<hr>
<table width="100%" summary="byline">
<tr>
<td>Mike Millson<br>Web Systems Engineer<br><a href="mailto:mmillson@meritonlinesystems.com">mmillson@meritonlinesystems.com</a></td>
<td align="right">August 26, 2003<br>Merit Online Systems, Inc.<br><a href="http://www.meritonlinesystems.com/" target="_blank">www.meritonlinesystems.com</a></td>
</tr>
</table>
<hr>
<h2>Introduction</h2>
<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>
<p>This article discusses how to integrate Tomcat with the Apache web server on RedHat 9.0. The goal is to provide a simple, stable configuration that will allow users to gain confidence using Tomcat.</p>
<p>Please note all commands are issued as root unless otherwise noted.</p>
<h2>Installing Apache</h2>
<p>I chose to install Apache using the RedHat RPM. Using the RPM instead of compiling Apache from source simplifies system administration in the following ways:</p>
<ul>
<li>Updates and bug fixes can be installed automatically from the <a href="http://rhn.redhat.com/" target="_blank">RedHat Network</a>.</li>
<li>Startup and shutdown scripts are already configured and available.</li>
</ul>
<p>I recommend using the RedHat up2date command line utility to install RedHat RPMs. Although up2date can be used without purchasing a RedHat Network subscription, a basic subscription is a great value. 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>
<p>RedHat RPMs that must be installed:</p>
<ul>
<li>httpd: the Apache web server</li>
<li>httpd-devel: development tools that will be needed to create the mod_jk connector</li>
</ul>
<p>To install these packages using up2date, make sure you are connected to the Internet, and enter the following:</p>
<div class="code">
<pre>
up2date -i httpd
up2date -i httpd-devel
</pre>
</div>
<p>You should now be able to start/stop/restart Apache as follows:</p>
<div class="code">
<pre>
service httpd start
service httpd stop
service httpd restart
</pre>
</div>
<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>
<h2>Installing Tomcat</h2>
<p>The only requirements to run Tomcat are that a Java Development Kit (JDK), also called a Java Software Developement Kit (SDK), be installed and the JAVA_HOME environment variable be set.</p>
<h3>Java SDK</h3>
<p>I chose to install Sun's 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>
<p>Change to the directory where you downloaded the SDK and make the self-extracting binary executable:</p>
<div class="code">
<pre>
chmod +x j2sdk-1_4_2-linux-i586.bin
</pre>
</div>
<p>Run the self-extracting binary:</p>
<div class="code">
<pre>
./j2sdk-1_4_2-linux-i586.bin
</pre>
</div>
<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't exist. Here is the command I used from inside the download directory:</p>
<div class="code">
<pre>
mv j2sdk1.4.2 /usr/java
</pre>
</div>
<p>Set the JAVA_HOME environment variable, by modifying /etc/profile so it includes the following:</p>
<div class="code">
<pre>
JAVA_HOME="usr/java/j2sdk1.4.2"
export JAVA_HOME
</pre>
</div>
<p>There will be other environment variables being set in /etc/profile, so you will probably be adding JAVA_HOME to an existing export command. /etc/profile is run at startup and when a user logs into a system.</p>
<h3>Tomcat Account</h3>
<p>You will install and configure Tomcat as root; however, you should create a group and user account for Tomcat to run under as follows:</p>
<div class="code">
<pre>
groupadd tomcat
useradd -g tomcat tomcat
</pre>
</div>
<p>This will create the /home/tomcat directory, where I will install my Tomcat applications.</p>
<h3>Download Tomcat</h3>
<p>Download the latest release build from <a href="http://www.apache.org/dist/jakarta/tomcat-4/binaries/" target="_blank">http://www.apache.org/dist/jakarta/tomcat-4/binaries/</a>. Since Tomcat runs directly on top of a standard JDK, I cannot think of any reason to building it from source.</p>
<p>The Tomcat binary is available in two different flavors:</p>
<ol>
<li>non-LE
<ul>
<li>Full binary distribution</li>
<li>Includes all optional libraries and an XML parser (Xerces)</li>
<li>Can be run on JDK 1.2+</li>
</ul>
<li>LE
<ul>
<li>Lightweight binary distribution</li>
<li>Designed to be run on JDK 1.4</li>
<li>Does not include an XML parser because one is included in JDK 1.4</li>
<li>Can be run on JDK 1.2 by adding an XML parser</li>
<li>All the components of this distribution are open source software</li>
<li>Does not include any of the following optional binaries: JavaMail, Java Activation Framework, Xerces, JNDI, or the JDBC Standard Extension</li>
</ul>
</ol>
<p>There are a number of different download formats. I chose the LE version gnu zipped tar file (jakarta-tomcat-4.1.27-LE-jdk14.tar.gz).</p>
<h3>Tomcat Standalone</h3>
<p>Unzip Tomcat by issuing the following command from your download directory:</p>
<div class="code">
<pre>
tar xvzf tomcat-4.1.27-LE-jdk14.tar.gz
</pre>
</div>
<p>This will create a directory called jakarta-tomcat-4.1.27-LE-jdk14. 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>
<div class="code">
<pre>
mv jakarta-tomcat-4.1.27-LE-jdk14 /usr/local/
</pre>
</div>
<p>The directory where Tomcat is installed is referred to as CATALINA_HOME in the Tomcat documentation. In this case CATALINA_HOME=/usr/local/jakarta-tomcat-4.1.27-LE-jdk14.</p>
<p>I recommend setting up a symbolic link to point to your current Tomcat version. This will save you from having to change your startup and shutdown scripts each time you upgrade Tomcat or set a CATALINA_HOME environment variable. 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-4.1.27-LE-jdk14:</p>
<div class="code">
<pre>
ln -s jakarta-tomcat-4.1.27-LE-jdk14 jakarta-tomcat
</pre>
</div>
<p>Change the group and owner of the /usr/local/jakarta-tomcat and /usr/local/jakarta-tomcat-4.1.27-LE-jdk14 directories to tomcat:</p>
<div class="code">
<pre>
chown tomcat.tomcat /usr/local/jakarta-tomcat
chown -R tomcat.tomcat /usr/local/jakarta-tomcat-4.1.27-LE-jdk14
</pre>
</div>
<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>
<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 clicking on some of the examples links.</p>
<h3>Selecting A Connector</h3>
<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 compelling reasons to use Apache as the front end. If you run Tomcat standalone:</p>
<ol>
<li>You will have to run Tomcat as root on port 80. This is a security concern.</li>
<li>You will not be able to use a connector such as mod_jk to load balance amongst several Tomcat instances.</li>
<li>You will not be able to take advantage of Apache features such as cgi and PHP.</li>
<li>You will not be able to take advantage of Apache modules such as mod_rewrite.</li>
<li>You will not be able to isolate virtual hosts in their own Tomcat instances.</li>
</ol>
<p>I think the increased functionality obtained by using Apache on the front end far outweighs the effort required to install and configure a connector. With that said, I selected the tried and true mod_jk connector. It has been around a long while and is very stable. mod_jk2 is the wave of the future, but I'm holding off on that for now. In early 2002 I invested a considerable amount of time on the "wave of the future" connector at that time, mod_webapp, which is now no longer being developed. For that reason, I am being cautious about migrating to mod_jk2.</p>
<h3>Building the mod_jk Connector</h3>
<p>The mod_jk connector is the communication link between Apache and Tomcat. It listens on port 8009 for requests from Apache.</p>
<p>In my experience, it's safest to think of connectors as being version dependent. If you upgrade Tomcat and you have a connector issue, try compiling the connector using the version-specific connector source.</p>
<p>Download the connector source for your version of Tomcat from <a href="http://www.apache.org/dist/jakarta/tomcat-4/source/" target="_blank">http://www.apache.org/dist/jakarta/tomcat-4/source/</a>. I used jakarta-tomcat-connectors-4.1.27-src.tar.gz. The source for all the different connectors (mod_jk, mod_jk2, coyote, etc.) is distributed in this one file.</p>
<p>Unzip the contents of the file into your download directory as follows:</p>
<div class="code">
<pre>
tar xvzf jakarta-tomcat-connectors-4.1.27-src.tar.gz
</pre>
</div>
<p>This will create a folder called jakarta-tomcat-connectors-4.1.27-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>
<div class="code">
<pre>
mv jakarta-tomcat-connectors-4.1.27-src /usr/src/
</pre>
</div>
<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-4.1.27-src.</p>
<p>Run the buildconf script to to create the CONN_SRC_HOME/jk/native/configure file.</p>
<div class="code">
<pre>
CONN_SRC_HOME/jk/native/buildconf.sh
</pre>
</div>
<p>Run the configure script with the path to the apxs file on your system and the options below:</p>
<div class="code">
<pre>
./configure --with-apxs=/usr/sbin/apxs
</pre>
</div>
<p>Build mod_jk with the following command:</p>
<div class="code">
<pre>
make
</pre>
</div>
<p>If all went well, the mod_jk.so file was successfully created. Manually copy it to Apache's shared object files directory:</p>
<div class="code">
<pre>
cp CONN_SRC_HOME/jk/native/apache-2.0/mod_jk.so /etc/httpd/modules
</pre>
</div>
<h2>Configuring Tomcat</h2>
<h3>workers.properties</h3>
<p>The workers.properties file contains information so mod_jk can connect to the Tomcat worker processes.</p>
<p>Create a directory called CATALINA_HOME/conf/jk and place the workers.properties file found in the <a href="#wp">Appendix</a> in this directory.</p>
<h3>server.xml</h3>
<p>The server.xml file contains Tomcat server configuration information. The default CATALINA_HOME/conf/server.xml file that comes with Tomcat contains so much information that I recommend saving it for future reference (e.g. server.xml.bak) and starting from scratch. 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 I have found it is not the best starting point when you want to integrate Apache with Tomcat. Instead, create a bare bones server.xml file as follows:</p>
<div class="code">
<pre>
<Server port="8005" shutdown="SHUTDOWN" debug="0">
<Service name="Tomcat-Apache">
<Connector className="org.apache.ajp.tomcat.Ajp13Connector"
port="8009" minProcessors="5" maxProcessors="75"
acceptCount="10" debug="0"/>
<Engine name="your_engine" debug="0" defaultHost="your_domain.com">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="apache_log." suffix=".txt"
timestamp="true"/>
<Host name="your_domain" debug="0" appBase="webapps"
unpackWARs="true">
<Context path="" docBase="/home/tomcat/your_application"
debug="0" reloadable="true" />
</Host>
</Engine>
</Service>
</Server>
</pre>
</div>
Notes:
<ol>
<li>The setup assumes you will put your Tomcat applications in /home/tomcat, not CATALINA_HOME/webapps. This will allow you to easily upgrade Tomcat and back up your Tomcat applications.</li>
<li>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't needed when you connect directly to Tomcat in standalone mode (port 8080), so I'm not sure why this connector is enabled by default.</li>
</ol>
<h2>Configuring Apache</h2>
<h3>httpd.conf</h3>
<p>Apache is configured with directives placed in the Apache configuration file, /etc/httpd/conf/httpd.conf. You will notice that there are three sections labeled in the httpd.conf file supplied by RedHat: (1) Global Environment, (2) Main Server Configuration, and (3) Virtual Hosts.</p>
<p>Add the following to the bottom of the existing LoadModule directives in the Global Environment section:</p>
<div class="code">
<pre>
LoadModule jk_module modules/mod_jk.so
</pre>
</div>
<p>Add the following to the bottom of the existing AddModule directives in the Global Environment section:</p>
<div class="code">
<pre>
AddModule mod_jk.c
</pre>
</div>
<p>Add the following to the bottom of the Main Server Configuration section:</p>
<div class="code">
<pre>
JkWorkersFile "/usr/local/jakarta-tomcat/conf/jk/workers.properties"
JkLogFile "/usr/local/jakarta-tomcat/logs/mod_jk.log"
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
</pre>
</div>
<p>The configuration above assumes you created a symbolic link /usr/jakarta-tomcat that points to the directory where your version of Tomcat is installed.</p>
<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 to forward all URLs with "servlet" in the path to Tomcat:</p>
<div class="code">
<pre>
NameVirtualHost *:80
<VirtualHost 192.168.1.1>
ServerAdmin webmaster@your_domain
ServerName your_domain
DocumentRoot /home/www/your_domain/html
ErrorLog /home/www/your_domain/logs/error_log
CustomLog /home/www/your_domain/logs/access_log common
JkMount /servlet/* ajp13
</VirtualHost>
</pre>
</div>
<p>The configuration above assumes that your application's static html files will be served from the /home/www/your_domain/html directory.</p>
<p>You can test your Apache configuration by typing the following:</p>
<div class="code">
<pre>
apachectl configtest
</pre>
</div>
<p>You will receive the response "Syntax OK" if there are no errors in httpd.conf.</p>
<h2>Setting Up your_domain</h2>
<p>your_domain.com 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>
<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>
<div class="code">
<pre>
127.0.0.1 your_domain
</pre>
</div>
<h2>Testing</h2>
<p>We will now create and install a simple Hello World servlet so we can test our setup.</p>
<h3>Hello World Servlet</h3>
<p>Copy the following into a file called HelloWorld.java:</p>
<div class="code">
<pre>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld
extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
</pre>
</div>
<p>Compile the source into a class file as follows:</p>
<div class="code">
<pre>
javac -classpath /usr/java/jakarta-tomcat/common/lib/servlet.jar HelloWorld.java
</pre>
</div>
<p>This will create a file called HelloWorld.class.</p>
<h3>Tomcat Application</h3>
<p>Create the following directories and files in /home/tomcat/your_application:</p>
<div class="code">
<pre>
/home/tomcat/your_application/WEB_INF
/home/tomcat/your_application/WEB_INF/classes
/home/tomcat/your_application/WEB_INF/web.xml
</pre>
</div>
<p>The web.xml file is where you map the name of your servlet 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>
<div class="code">
<pre>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/servlet/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
</pre>
</div>
<p>Copy the HelloWorld.class file to the /tomcat/home/your_application/WEB-INF/classes directory.</p>
<p>Restart Tomcat as follows:</p>
<div class="code">
<pre>
/CATALINA_HOME/bin/shutdown.sh
/CATALINA_HOME/bin/startup.sh
</pre>
</div>
<p>Restart Apache as follows:</p>
<div class="code">
<pre>
service httpd restart
</pre>
</div>
<p>You should now be able to type http://your_domain/servlet/HelloWorld into your browser and see the always-exciting "Hello World" message.</p>
<h2>Advanced Configuration</h2>
<p>The following steps are not mandatory, but are suggested for a better, tighter Tomcat installation.</p>
<h3>Tomcat Startup Script</h3>
<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>
<p>Create the following Tomcat initialization script as /etc/rc.d/init.d/tomcat</p>
<div class="code">
<pre>
#!/bin/sh
#
# Startup script for Tomcat, the Apache Servlet Engine
#
# chkconfig: 345 80 20
# description: Tomcat is the Apache Servlet Engine
# processname: tomcat
# pidfile: /var/run/tomcat.pid
#
# Mike Millson <mmillson@meritonlinesystems.com>
#
# version 1.02 - Clear work directory on shutdown per John Turner suggestion.
# version 1.01 - Cross between RedHat Tomcat RPM and Chris Bush scripts
# Tomcat name :)
TOMCAT_PROG=tomcat
# if TOMCAT_USER is not set, use tomcat like Apache HTTP server
if [ -z "$TOMCAT_USER" ]; then
TOMCAT_USER="tomcat"
fi
RETVAL=0
# start and stop functions
start() {
echo -n "Starting tomcat: "
chown -R $TOMCAT_USER:$TOMCAT_USER /usr/local/jakarta-tomcat/*
chown -R $TOMCAT_USER:$TOMCAT_USER /home/tomcat/*
su -l $TOMCAT_USER -c '/usr/local/jakarta-tomcat/bin/startup.sh'
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat
return $RETVAL
}
stop() {
echo -n "Stopping tomcat: "
su -l $TOMCAT_USER -c '/usr/local/jakarta-tomcat/bin/shutdown.sh'
RETVAL=$?
Echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/tomcat /var/run/tomcat.pid
rm -rf /usr/local/jakarta-tomcat/work/*
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
# Ugly hack
# We should really make sure tomcat
# is stopped before leaving stop
sleep 2
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $RETVAL
</pre>
</div>
<p>Add the startup script to your system as follows:
</p>
<div class="code">
<pre>
chkconfig --add tomcat
</pre>
</div>
<p>You will be able to start/stop/restart it using the following commands:</p>
<div class="code">
<pre>
service tomcat start
service tomcat stop
service tomcat restart
</pre>
</div>
<p>If you want Tomcat to start automatically when your system boots, you need to add tomcat to your runlevel as follows:</p>
<div class="code">
<pre>
chkconfig --level 5 tomcat on
</pre>
</div>
<p>Runlevel 5 is the X Window System, typical for a development computer. Runlevel 3 is typical for a dedicated web server.</p>
<p>The start order of Apache and Tomcat is very important. Tomcat must be started before you start Apache so Apache can attach itself to the Tomcat processes.</p>
<h3>Development Setup</h3>
<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 <pre>yourname</pre> account:</p>
<div class="code">
<pre>
tomcat:x:502:yourname
</pre>
</div>
<p>Make sure the tomcat group has write permission to /home/tomcat so you can publish files (e.g. using ant) to your Tomcat application in /home/tomcat/your_application. Issue the following command as root:</p>
<div class="code">
<pre>
chmod g+w /home/tomcat
</pre>
</div>
<hr>
<h2><a name="wp">Appendix</a></h2>
<h3>workers.properties</h3>
<div class="code">
<pre>
# workers.properties
#
# This file provides jk derived plugins with the needed information to
# connect to the different tomcat workers. Note that the distributed
# version of this file requires modification before it is usable by a
# plugin.
#
# As a general note, the characters $( and ) are used internally to define
# macros. Do not use them in your own configuration!!!
#
# Whenever you see a set of lines such as:
# x=value
# y=$(x)\something
#
# the final value for y will be value\something
#
# Normaly all you will need to do is un-comment and modify the first three
# properties, i.e. workers.tomcat_home, workers.java_home and ps.
# Most of the configuration is derived from these.
#
# When you are done updating workers.tomcat_home, workers.java_home and ps
# you should have 3 workers configured:
#
# - An ajp12 worker that connects to localhost:8007
# - An ajp13 worker that connects to localhost:8009
# - A jni inprocess worker.
# - A load balancer worker
#
# However by default the plugins will only use the ajp12 worker. To have
# the plugins use other workers you should modify the worker.list property.
#
# OPTIONS ( very important for jni mode )
#
# workers.tomcat_home should point to the location where you
# installed tomcat. This is where you have your conf, webapps and lib
# directories.
#
workers.tomcat_home=/usr/local/jakarta-tomcat
#
# workers.java_home should point to your Java installation. Normally
# you should have a bin and lib directories beneath it.
#
workers.java_home=$(JAVA_HOME)
#
# You should configure your environment slash... ps=\ on NT and / on UNIX
# and maybe something different elsewhere.
#
ps=/
#
#------ ADVANCED MODE ------------------------------------------------
#---------------------------------------------------------------------
#
#------ DEFAULT worket list ------------------------------------------
#---------------------------------------------------------------------
#
# The workers that your plugins should create and work with
#
worker.list=ajp12, ajp13
#
#------ DEFAULT ajp12 WORKER DEFINITION ------------------------------
#---------------------------------------------------------------------
#
#
# Defining a worker named ajp12 and of type ajp12
# Note that the name and the type do not have to match.
#
worker.ajp12.port=8007
worker.ajp12.host=localhost
worker.ajp12.type=ajp12
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.ajp12.lbfactor=1
#
#------ DEFAULT ajp13 WORKER DEFINITION ------------------------------
#---------------------------------------------------------------------
#
# Defining a worker named ajp13 and of type ajp13
# Note that the name and the type do not have to match.
#
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13
#
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
# ----> lbfactor must be > 0
# ----> Low lbfactor means less work done by the worker.
worker.ajp13.lbfactor=1
#
# Specify the size of the open connection cache.
#worker.ajp13.cachesize
#
#------ DEFAULT LOAD BALANCER WORKER DEFINITION ----------------------
#---------------------------------------------------------------------
#
# The loadbalancer (type lb) workers perform wighted round-robin
# load balancing with sticky sessions.
# Note:
# ----> If a worker dies, the load balancer will check its state
# once in a while. Until then all work is redirected to peer
# workers.
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=ajp12, ajp13
#
#------ DEFAULT JNI WORKER DEFINITION---------------------------------
#---------------------------------------------------------------------
#
# Defining a worker named inprocess and of type jni
# Note that the name and the type do not have to match.
#
worker.inprocess.type=jni
#
#------ CLASSPATH DEFINITION -----------------------------------------
#---------------------------------------------------------------------
#
# Additional class path components.
#
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)lib$(ps)tomcat.jar
#
# Setting the command line for tomcat.
# Note: The cmd_line string may not contain spaces.
#
worker.inprocess.cmd_line=start
#
# Not needed, but can be customized.
# worker.inprocess.cmd_line=-config
# worker.inprocess.cmd_line=$(workers.tomcat_home)$(ps)conf$(ps)server.xml
# worker.inprocess.cmd_line=-home
# worker.inprocess.cmd_line=$(workers.tomcat_home)
#
# The JVM that we are about to use
#
# This is for Java2
#
# Windows
# #worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)bin$(ps)classic$(ps)jvm.dll
# IBM JDK1.3
# worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)bin$(ps)classic$(ps)libjvm.so
# Unix - Sun VM or blackdown
#worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)lib$(ps)i386$(ps)classic$(ps)libjvm.so
# RH + JDK1.4
worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)lib$(ps)i386$(ps)server$(ps)libjvm.so
#
# And this is for jdk1.1.X
#
# worker.inprocess.jvm_lib=$(workers.java_home)$(ps)bin$(ps)javai.dll
#
# Setting the place for the stdout and stderr of tomcat
#
worker.inprocess.stdout=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stdout
worker.inprocess.stderr=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stderr
#
# Setting the tomcat.home Java property
#
# worker.inprocess.sysprops=tomcat.home=$(workers.tomcat_home)
#
# Java system properties
#
# worker.inprocess.sysprops=java.compiler=NONE
# worker.inprocess.sysprops=myprop=mypropvalue
#
# Additional path components.
#
# worker.inprocess.ld_path=d:$(ps)SQLLIB$(ps)bin
</pre>
</div>
<hr>
<h2>Related Linux Gazette Articles</h2>
<p><a href="http://www.linuxgazette.com/issue69/peda.html" target="_blank">Installing Tomcat on Linux</a> by Allan Peda, August 2001</p>
<hr>
<h2>Bio</h2>
<p>Mike is a Web Systems Engineer with <a href="http://www.meritonlinesystems.com/" target="_blank">Merit Online Systems</a> in Atlanta, GA. His first computer experience came programming BASIC on an IBM PC in 1981. When he isn't wearing his propeller cap, he enjoys spending time with his wife, Debora, and spoiling his Golden Retriever, Belle.</p>
<hr>
© 2003 Merit Online Systems, Inc.
</body>
</html>
<!-- *** BEGIN author bio *** -->
<P>
<P>
<!-- *** BEGIN bio *** -->
<P>
<img ALIGN="LEFT" ALT="[BIO]" SRC="../gx/2002/note.png">
<em>
Mike is a Web Systems Engineer with <a href="http://www.meritonlinesystems.com/" target="_blank">Merit Online Systems</a> in Atlanta, GA. His first computer experience came programming BASIC on an IBM PC in 1981. When he isn't wearing his propeller cap, he enjoys spending time with his wife, Debora, and spoiling his Golden Retriever, Belle.
</em>
<br CLEAR="all">
<!-- *** END bio *** -->
<!-- *** END author bio *** -->
<!-- *** BEGIN copyright *** -->
<hr>
<CENTER><SMALL><STRONG>
Copyright © 2003, Mike Millson.
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
Published in Issue 95 of <i>Linux Gazette</i>, October 2003
</STRONG></SMALL></CENTER>
<!-- *** END copyright *** -->
<HR>
<!--startcut ==========================================================-->
<CENTER>
<!-- *** BEGIN navbar *** -->
<A HREF="hughes.html"><< Prev</A> | <A HREF="index.html">TOC</A> | <A HREF="../index.html">Front Page</A> | <A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue95/millson.html">Talkback</A> | <A HREF="../faq/index.html">FAQ</A> | <A HREF="pramode.html">Next >></A>
<!-- *** END navbar *** -->
</CENTER>
</BODY></HTML>
<!--endcut ============================================================-->
|