Home > Support > HOWTO List > JSP Hosting: Tomcat

Tomcat Servlet Engine


Tomcat is the 'reference' Servlet/JSP implementation.  It is probably the most widely used Servlet engine out there.  There are probably smaller, faster Servlet engines available, but if it won't run on Tomcat, it probably won't run period.

Installing Tomcat

On most modern distros you can install tomcat from the distribution repositories (ie 'yum install tomcat' or 'apt-get install tomcat'), or you may have a hosting panel installed that takes care of that for you. However anyone who wants more control over the tomcat setup, or just to get the very latest version can use our install script to setup a local tomcat instance. To do that just run:

wget http://proj.ri.mu/installtomcat.sh
bash installtomcat.sh

After the install tomcat should be visible on localhost only (eg with w3m) at http://127.0.0.1:8080 and you should see a blank page to start with untile you add something to the webapps folder. See the details below on how to configure ajp with apache to get public access. It is also possible to reconfigure tomcat to show directly on http://yourserverip:8080 but this is not recomended for security.

If you need more options, eg to install an older version, then run the install script with the --help option to see what you can do.

This should work on most Redhat-, Ubuntu- and Debian-based distros.

Installing Java

Tomcat requires the Java SDK to be installed to be used to maximum effect.

On some distros RimuHosting pre-installs a JDK. Or you might install that using your favourite package manager. Otherwise our tomcat install script will do that for you, or you can install one using our installjava script. When using that run "bash installjava.sh --help" to see script options.  installjava will install a recent jdk by default, or you can select a particular JDK version the --jre argument ( --jre 8, --jre 10, etc).

Managing the Tomcat service

On newer distributions with systemd

You can start/stop/restart the service on older distributions with: systemctl start/stop/restart tomcat.

You can check the service is configured to run at bootup, which is the default, you would use systemctl status tomcat. Check that the "Loaded" line is present in the output, and includes "enabled".

To stop it running automatically on boot you would use systemctl disable tomcat, to reenable: systemctl enable tomcat.

For older sysvinit distributions

You can start/stop/restart the service with: service tomcat start/stop/restart.

To enable tomcat on boot...
On CentOS/Fedora do chkconfig --level 3 tomcat on.
On Debian/Ubuntu run update-rc.d tomcat defaults.

Using Tomcat

To run Tomcat without the :8080 port number, follow our mod_jk2/mod_proxy_ajp/iptables howto.

File locations of note inside the installation folder...

At this point you may want to edit the tomcat configuration to suit your setup.  After changing configuration settings, you may need to restart tomcat to apply the changes.

Other important files...

Tomcat File Permissions

Be careful of changing file permissions. If a webapp or configuration file has the wrong permissions, then Tomcat will probably spit permissions-related errors at you.  Tomcat runs as a regular Unix user (usually 'tomcat'). This is good, since it is not running as, say, root which introduces the potential for more security problems.

You can always re-set the file ownership eg by running

chown -R tomcat:tomcat /usr/local/tomcat

Easy server.xml Edits with the Admin WebApp

From the admin webapp you can change and view your tomcat settings, add new Contexts, Loggers, and more.  Very handy if you are too shy to edit the server.xml file directly.

By default your Tomcat install is not setup with the admin webapp. We do that for security reasons (i.e. so that someone cannot come along and use the default tomcat username and password to view your database details and change your Tomcat setup).

Setting up the Admin User

Edit /usr/local/tomcat/conf/tomcat-users.xml.

Add a line like:

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>

<user username="someusername" password="somepassword" roles="manager-gui,manager-status"/>

Check that none of the default users have the manager or admin roles.

Activate' the Admin and Manager Webapps

The original webapps are moved out of the way on install, for safe-keeping and security.  You can find them in the webapps.removed directory. You can easily move them back...

Eg to activate the Admin webapp run this:

mv /usr/local/tomcat/webapps.removed/manager /usr/local/tomcat/webapps/
mv /usr/local/tomcat/webapps.removed/host-manager /usr/local/tomcat/webapps/

You can now browse to http://yourip:8080/manager/ and you will be prompted for the username/password you setup in tomcat-users.xml.

You may need to restart tomcat to make changes active in some cases.

Further documentation

The official documentation for Tomcat can always be found on Apache foundation site

Resolving: "Connection Refused" Error on Tomcat Stop

Seeing an error like this?

Catalina.stop: java.net.ConnectException: Connection refused
java.net.ConnectException: Connection refused at
java.net.PlainSocketImpl.socketConnect(Native Method) at
java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)

If you run: ps axf | grep [j]ava.endorsed.dir It should give you one or more lines like this when Tomcat is running:

22587 pts/00:04 /usr/local/jdk/bin/java -Djava.endorsed.dirs= -classp

If you do not see any lines like that, then Tomcat may have died already. In that case, execute this command:

rm /var/lock/subsys/tomcat4

It will remove the lock and you can restart tomcat.