java:installationunderlinux
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:installationunderlinux [2011/11/13 15:33] – [Things to do in apache] rlunaro | java:installationunderlinux [2022/12/02 21:02] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Installation of Tomcat under Linux ====== | ||
+ | |||
+ | ===== Preface ===== | ||
+ | |||
+ | These notes are about installing Tomcat in a production environment under apache. This notes were taken while I were installing such product in a machine on a DMZ, so **security were my first concern in every step I took**. | ||
+ | |||
+ | Said that, it is possible that the document has misunderstandings and errors. Nobody is perfect. **Use it at your own risk**. | ||
+ | |||
+ | The machine I am using is a Virtual Server with 32 bits and a SuSE 11.1 installed. | ||
+ | |||
+ | ===== Things we need to install ===== | ||
+ | |||
+ | You should have the JRE installed in your machine. You can download it from Oracle website. | ||
+ | |||
+ | Make sure that you have defined a environmen variable JRE_HOME that points to the location of the JRE. In other words, this command should work: | ||
+ | |||
+ | <code bash> | ||
+ | rluna@machine: | ||
+ | java version " | ||
+ | Java(TM) SE Runtime Environment (build 1.6.0_22-b04) | ||
+ | Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode) | ||
+ | </ | ||
+ | |||
+ | Yes, the version of java is a bit outdated. I've used the version that the current version of linux provided through the package manager. | ||
+ | |||
+ | ===== Create a tomcat user and a tomcat group ===== | ||
+ | |||
+ | The idea is to create a " | ||
+ | |||
+ | <code shell> | ||
+ | useradd --create-home --user-group --system -g tomcat tomcat | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | The '' | ||
+ | |||
+ | ==== If you want to log in as the tomcat user ==== | ||
+ | |||
+ | As you may know, if you want to login as the user '' | ||
+ | |||
+ | < | ||
+ | useradd -s /bin/bash tomcat | ||
+ | </ | ||
+ | |||
+ | and, to return the user to the prior state, you only have to do the following: | ||
+ | |||
+ | < | ||
+ | useradd -s /bin/false tomcat | ||
+ | </ | ||
+ | |||
+ | ==== Ensure that the user has an umask of 0027 or less ==== | ||
+ | |||
+ | This can be checked out entering as the tomcat user or checking the /etc directory. This is for not leaving open files to every other user in the system. | ||
+ | |||
+ | ===== Creating the directory ===== | ||
+ | |||
+ | I've choose / | ||
+ | |||
+ | <code shell> | ||
+ | mkdir apache-tomcat-7.0.22 | ||
+ | chown tomcat: | ||
+ | chmod 750 apache-tomcat-7.0.22 | ||
+ | ln -s apache-tomcat-7.0.22 tomcat | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Copy the contents (beware with the permissions) ===== | ||
+ | |||
+ | Copy all the contents into the new location (or uncompress them), and make sure that the permissions are assigned to tomcat: | ||
+ | |||
+ | <code bash> | ||
+ | cp / | ||
+ | chown tomcat: | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Do a basic cleanup ==== | ||
+ | |||
+ | I've seen that the Windows .bat files are in the /bin files. I suggest delete them. | ||
+ | |||
+ | |||
+ | ==== Checkpoint ==== | ||
+ | |||
+ | At this point, tomcat should start with the default configuration. It is a good idea to make a little test prior to further testing. | ||
+ | |||
+ | Log in as the tomcat user and run the server: | ||
+ | |||
+ | <code bash> | ||
+ | tomcat@machine:/ | ||
+ | Using CATALINA_BASE: | ||
+ | Using CATALINA_HOME: | ||
+ | Using CATALINA_TMPDIR: | ||
+ | Using JRE_HOME: | ||
+ | Using CLASSPATH: | ||
+ | |||
+ | </ | ||
+ | |||
+ | Open a web browser and point it to the http:// | ||
+ | |||
+ | |||
+ | ==== Fine-tunning configuration ==== | ||
+ | |||
+ | |||
+ | === Location of log files === | ||
+ | |||
+ | SuSE has all their log files under /var/log, so I prefer respect this practice and move the logs of apache to this location. Now they are under / | ||
+ | |||
+ | **First,** create a proper directory under /var/log, let's say / | ||
+ | |||
+ | <code bash> | ||
+ | # mkdir / | ||
+ | # chown tomcat: | ||
+ | </ | ||
+ | |||
+ | Then, open the file CATALINA_HOME/ | ||
+ | |||
+ | |||
+ | <code properties> | ||
+ | 1catalina.org.apache.juli.FileHandler.level = FINE | ||
+ | 1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/ | ||
+ | 1catalina.org.apache.juli.FileHandler.prefix = catalina. | ||
+ | |||
+ | 2localhost.org.apache.juli.FileHandler.level = FINE | ||
+ | 2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/ | ||
+ | 2localhost.org.apache.juli.FileHandler.prefix = localhost. | ||
+ | |||
+ | </ | ||
+ | |||
+ | When it appears ${catalina.base}/ | ||
+ | |||
+ | Open the " | ||
+ | |||
+ | <code xml> | ||
+ | <Valve className=" | ||
+ | | ||
+ | | ||
+ | |||
+ | </ | ||
+ | |||
+ | Go to the '' | ||
+ | |||
+ | <code bash> | ||
+ | if [ -z " | ||
+ | CATALINA_OUT=" | ||
+ | fi | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | And change for: | ||
+ | |||
+ | <code bash> | ||
+ | if [ -z " | ||
+ | CATALINA_OUT=/ | ||
+ | fi | ||
+ | |||
+ | </ | ||
+ | |||
+ | Delete the content of the " | ||
+ | |||
+ | If the checking were successful, remove the '' | ||
+ | |||
+ | === Location of config files === | ||
+ | |||
+ | Another step is to change the location of the config files and put them under ''/ | ||
+ | |||
+ | == Creation of the directory under /etc == | ||
+ | |||
+ | <code bash> | ||
+ | machine:/ | ||
+ | machine:/ | ||
+ | </ | ||
+ | |||
+ | == Move the contents to the new location == | ||
+ | |||
+ | Log in as the tomcat user: | ||
+ | |||
+ | <code bash> | ||
+ | tomcat@machine:/ | ||
+ | tomcat@machine:/ | ||
+ | tomcat@machine:/ | ||
+ | tomcat@machine:/ | ||
+ | </ | ||
+ | |||
+ | ===== Create a Service so that it will start automatically ===== | ||
+ | |||
+ | In general, it is a good idea to create a service that will start automatically when the server starts. And that will be gently stopped when the servers is shut down. | ||
+ | |||
+ | **How to do this??** | ||
+ | |||
+ | One idea is to search in internet. But bear in mind that most modern Linux distributions comes with __examples__ of services ready to customize for your needs. These __examples__ are common called as '' | ||
+ | |||
+ | Mess around the /etc directory of your installation or check /etc/rc.d/ or /etc/init.d in order to locate those scripts. | ||
+ | |||
+ | **Here I put the script I created for SuSE** | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/sh | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # This library is free software; you can redistribute it and/or modify it | ||
+ | # under the terms of the GNU Lesser General Public License as published by | ||
+ | # the Free Software Foundation; either version 2.1 of the License, or (at | ||
+ | # your option) any later version. | ||
+ | # | ||
+ | # This library is distributed in the hope that it will be useful, but | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # You should have received a copy of the GNU Lesser General Public | ||
+ | # | ||
+ | # | ||
+ | # USA. | ||
+ | # | ||
+ | # / | ||
+ | # and its symbolic link | ||
+ | # / | ||
+ | # | ||
+ | # | ||
+ | # LSB compatible service control script; see http:// | ||
+ | # | ||
+ | # | ||
+ | ### BEGIN INIT INFO | ||
+ | # Provides: | ||
+ | # Required-Start: | ||
+ | # Should-Start: | ||
+ | # Required-Stop: | ||
+ | # Should-Stop: | ||
+ | # Default-Start: | ||
+ | # Default-Stop: | ||
+ | # Short-Description: | ||
+ | # Description: | ||
+ | ### END INIT INFO | ||
+ | # | ||
+ | # Any extensions to the keywords given above should be preceeded by | ||
+ | # X-VendorTag- (X-UnitedLinux- X-SuSE- for us) according to LSB. | ||
+ | # | ||
+ | # Notes on Required-Start/ | ||
+ | # * There are two different issues that are solved by Required-Start | ||
+ | # and Should-Start | ||
+ | # (a) Hard dependencies: | ||
+ | # which services absolutely need to be started to make the start of | ||
+ | # this service make sense. Example: nfsserver should have | ||
+ | # | ||
+ | # Also, required services are started before the dependent ones. | ||
+ | # The runlevel editor will warn about such missing hard dependencies | ||
+ | # and suggest enabling. During system startup, you may expect an error, | ||
+ | # if the dependency is not fulfilled. | ||
+ | # (b) Specifying the init script ordering, not real (hard) dependencies. | ||
+ | # This is needed by insserv to determine which service should be | ||
+ | # | ||
+ | # in parallel). The tag Should-Start: | ||
+ | # It tells, that if a service is available, it should be started | ||
+ | # | ||
+ | # * When specifying hard dependencies or ordering requirements, | ||
+ | # use names of services (contents of their Provides: section) | ||
+ | # or pseudo names starting with a $. The following ones are available | ||
+ | # | ||
+ | # | ||
+ | # (most services should need this!) | ||
+ | # | ||
+ | # (note that /usr may be remote, so | ||
+ | # many services should Require this!) | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # The $netdaemons pseudo service has been removed in LSB 1.2. | ||
+ | # For now, we still offer it for backward compatibility. | ||
+ | # These are new (LSB 1.2): | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # at the end | ||
+ | # * The services specified in the stop tags | ||
+ | # | ||
+ | # | ||
+ | # is shut down. Often the entries there are just copies or a subset | ||
+ | # from the respective start tag. | ||
+ | # * Should-Start/ | ||
+ | # | ||
+ | # | ||
+ | # * X-UnitedLinux-Default-Enabled: | ||
+ | # | ||
+ | # a startup script should default to be enabled after installation. | ||
+ | # | ||
+ | # | ||
+ | # Note on runlevels: | ||
+ | # 0 - halt/ | ||
+ | # 1 - single user 2 - multiuser without network exported | ||
+ | # 3 - multiuser w/ network (text mode) 5 - multiuser w/ network and X11 (xdm) | ||
+ | # | ||
+ | # Note on script names: | ||
+ | # http:// | ||
+ | # A registry has been set up to manage the init script namespace. | ||
+ | # http:// | ||
+ | # Please use the names already registered or register one or use a | ||
+ | # vendor prefix. | ||
+ | |||
+ | TOMCAT_HOME=/ | ||
+ | test -d $TOMCAT_HOME || { echo " | ||
+ | ; | ||
+ | if [ " | ||
+ | else exit 5; fi; } | ||
+ | # Check for missing binaries (stale symlinks should not happen) | ||
+ | # Note: Special treatment of stop for LSB conformance | ||
+ | TOMCAT_BIN=$TOMCAT_HOME/ | ||
+ | test -s $TOMCAT_BIN || { echo " | ||
+ | ed?"; | ||
+ | if [ " | ||
+ | else exit 5; fi; } | ||
+ | |||
+ | # Check for existence of needed config file and read it | ||
+ | TOMCAT_CONFIG=$TOMCAT_HOME/ | ||
+ | test -r $TOMCAT_CONFIG || { echo " | ||
+ | if [ " | ||
+ | else exit 6; fi; } | ||
+ | |||
+ | # Source LSB init functions | ||
+ | # providing start_daemon, | ||
+ | # log_success_msg, | ||
+ | # This is currently not used by UnitedLinux based distributions and | ||
+ | # not needed for init scripts for UnitedLinux only. If it is used, | ||
+ | # the functions from rc.status should not be sourced or used. | ||
+ | #. / | ||
+ | |||
+ | # Shell functions sourced from / | ||
+ | # rc_check | ||
+ | # rc_status | ||
+ | # rc_status -v be verbose in local rc status and clear it afterwards | ||
+ | # rc_status -v -r ditto and clear both the local and overall rc status | ||
+ | # rc_status -s | ||
+ | # rc_status -u | ||
+ | # rc_failed | ||
+ | # rc_failed < | ||
+ | # rc_reset | ||
+ | # rc_exit | ||
+ | # rc_active | ||
+ | . / | ||
+ | |||
+ | # Reset status of this service | ||
+ | rc_reset | ||
+ | |||
+ | # Return values acc. to LSB for all commands but status: | ||
+ | # 0 - success | ||
+ | # 1 - generic or unspecified error | ||
+ | # 2 - invalid or excess argument(s) | ||
+ | # 3 - unimplemented feature (e.g. " | ||
+ | # 4 - user had insufficient privileges | ||
+ | # 5 - program is not installed | ||
+ | # 6 - program is not configured | ||
+ | # 7 - program is not running | ||
+ | # 8--199 | ||
+ | # | ||
+ | # Note that starting an already running service, stopping | ||
+ | # or restarting a not-running service as well as the restart | ||
+ | # with force-reload (in case signaling is not supported) are | ||
+ | # considered a success. | ||
+ | |||
+ | case " | ||
+ | start) | ||
+ | echo -n " | ||
+ | ## Start daemon with startproc(8). If this fails | ||
+ | ## the return value is set appropriately by startproc. | ||
+ | #/ | ||
+ | |||
+ | su tomcat -c $TOMCAT_HOME/ | ||
+ | |||
+ | # Remember status and be verbose | ||
+ | rc_status -v | ||
+ | ;; | ||
+ | stop) | ||
+ | echo -n " | ||
+ | ## Stop daemon with killproc(8) and if this fails | ||
+ | ## killproc sets the return value according to LSB. | ||
+ | #/ | ||
+ | |||
+ | su tomcat -c $TOMCAT_HOME/ | ||
+ | |||
+ | # Remember status and be verbose | ||
+ | rc_status -v | ||
+ | ;; | ||
+ | restart) | ||
+ | ## Stop the service and regardless of whether it was | ||
+ | ## running or not, start it again. | ||
+ | $0 stop | ||
+ | $0 start | ||
+ | # Remember status and be quiet | ||
+ | rc_status | ||
+ | ;; | ||
+ | *) | ||
+ | echo " | ||
+ | exit 1 | ||
+ | ;; | ||
+ | esac | ||
+ | rc_exit | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ====== Integration with Apache ====== | ||
+ | |||
+ | Due to the fact that I am using apache at the same time in the same machine, Tomcat will act only as the server of the JSP pages, delegating all the static content to apache. | ||
+ | |||
+ | You have to have '' | ||
+ | |||
+ | ===== Tomcat connector mod-jk ===== | ||
+ | |||
+ | The only way to integrate apache with tomcat is to use mod_jk. Here is the documentation: | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | And you have to google the download location. I've downloaded the source code and here are the instructions to compile it: | ||
+ | |||
+ | ==== Installing and compiling mod_jk ==== | ||
+ | |||
+ | Once you have the apxs2 program installed and the source code of the mod_jk, you are prepared to compile it: | ||
+ | |||
+ | < | ||
+ | $ cd native | ||
+ | $ ./configure --with-apxs=/ | ||
+ | $ su | ||
+ | # make install | ||
+ | </ | ||
+ | |||
+ | http:// | ||
+ | |||
+ | |||
+ | |||
+ | ===== Things to do in apache ===== | ||
+ | |||
+ | In the apache configuration directory, create a file called " | ||
+ | |||
+ | < | ||
+ | # Define 1 real worker using ajp13 | ||
+ | worker.list=worker1 | ||
+ | # Set properties for worker1 (ajp13) | ||
+ | worker.worker1.type=ajp13 | ||
+ | worker.worker1.host=localhost | ||
+ | worker.worker1.port=8009 | ||
+ | worker.worker1.lbfactor=50 | ||
+ | worker.worker1.cachesize=10 | ||
+ | worker.worker1.cache_timeout=600 | ||
+ | worker.worker1.socket_keepalive=1 | ||
+ | worker.worker1.socket_timeout=300 | ||
+ | </ | ||
+ | |||
+ | At least the following configuration must be put under apache: | ||
+ | |||
+ | |||
+ | < | ||
+ | # Load mod_jk module | ||
+ | # Under SuSE this is carried out by sysconfig/ | ||
+ | # Update this path to match your modules location | ||
+ | LoadModule jk_module libexec/ | ||
+ | |||
+ | # Under SuSE this is carried out by sysconfig/ | ||
+ | # Declare the module for < | ||
+ | AddModule mod_jk.c | ||
+ | |||
+ | # Where to find workers.properties | ||
+ | # Update this path to match your conf directory location (put workers.properties next to httpd.conf) | ||
+ | JkWorkersFile / | ||
+ | |||
+ | # Where to put jk logs | ||
+ | # Update this path to match your logs directory location (put mod_jk.log next to access_log) | ||
+ | JkLogFile / | ||
+ | |||
+ | # Set the jk log level [debug/ | ||
+ | JkLogLevel info | ||
+ | |||
+ | # Select the log format | ||
+ | JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " | ||
+ | |||
+ | # JkOptions indicate to send SSL KEY SIZE, | ||
+ | JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories | ||
+ | |||
+ | # JkRequestLogFormat set the request format | ||
+ | JkRequestLogFormat "%w %V %T" | ||
+ | |||
+ | # Send everything for context /examples to worker named worker1 (ajp13) | ||
+ | JkMount /examples/* worker1 | ||
+ | |||
+ | # In installation with virtual servers, it allows to respond to | ||
+ | # http:// | ||
+ | # If not, you have to put this directive (JkMountCopy) under | ||
+ | # the VirtualHost directive you want to use | ||
+ | JkMountCopy All | ||
+ | |||
+ | </ | ||
+ | |||
+ | There are several ways to organize it. Under SuSE, the best way is the following: | ||
+ | |||
+ | * under ''/ | ||
+ | * also modify the variable '' | ||
+ | * create the file / | ||
+ | |||
+ | |||
+ | ====== Security Issues ====== | ||
+ | |||
+ | ===== Deactivate the listening through the 8080 port ===== | ||
+ | |||
+ | |||
+ | Disconnect the default Connector port under tomcat: the port 8080. If you aren't gonna use it, the obvious is to disconnect it. | ||
+ | |||
+ | Under TOMCAT_HOME, | ||
+ | |||
+ | <code xml> | ||
+ | <!-- Connector port=" | ||
+ | | ||
+ | | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Delete the " | ||
+ | |||
+ | Go to TOMCAT_HOME/ | ||
+ | |||
+ | ====== Test and conclusion ====== | ||
+ | |||
+ | Test, test, test... In every step you take in this issue, try to test if you are doing the correct!!! This is the best advice I can give you in order to succeed in such projects. | ||
+ | |||
+ | Anyway, the last step is to test the issue. I've opened a browser, point it to http:// | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | |||
+ | You can elaborate this much --as I did-- and change the " | ||
+ | |||
+ | |||
+ | {{ : | ||
+ | |||
+ | ===== Conclusion ===== | ||
+ | |||
+ | |||
+ | In this document I've tried to explain how to install tomcat to serve as a backend for an Apache http server, so it serves only certain types of pages. | ||
+ | |||
+ | The explanation is made from the ground, as if there were no prior knowledge of apache nor tomcat. I hope I achieved this. If not, drop me some lines in the Discussion below. Anyway, any comment or suggestion will help. | ||
+ | |||
+ | ====== Documentation ====== | ||
+ | |||
+ | I checked out this document: [[http:// | ||
+ | |||
+ | Of course, I checked also the Apache Tomcat documentation: | ||
+ | |||
+ | And have a look at this: [[http:// | ||
+ | |||
+ | ~~DISQUS~~ | ||
+ | |||
+ | |||
+ |