Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Unpack the tomcat tar.gz file into /opt/tomcat and set the file ownership and permissions.

Code Block
sudo mkdir /opt/tomcat
cd /opt/tomcat
sudo tar xvf /tmp/apache-tomcat-8*tar.gz --strip-components=1

...

This will have created the tomcat folders (conf, logs, webapps etc.) directly under /opt/tomcat.

Now to set the file ownership and permissions.

Code Block
cd /opt/tomcat
sudo chown -R tomcat webapps/ work/ temp/ logs/
sudo chgrp -R tomcat .
sudo chmod -R g+r conf
sudo chmod g+x conf

Create a service wrapper

This stage creates a wrapper which allows tomcat to be managed as a service; this allows tomcat to be stopped and started reliably, and ensures that the running environment (e.g. the starting directory) is well defined.

Specifically, these instructions create a systemd service.

First, create a service definition file:

Code Block
sudo vi /etc/systemd/system/tomcat.service

and paste the following into it:

Code Block
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/opt/jdk/jdk1.8.0_85/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms1000M -Xmx2000M -server -verbose:gc'
Environment='JAVA_OPTS=-Djava.awt.headless=true'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

WorkingDirectory=/opt/tomcat

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Before saving this,

  1. check that JAVA_HOME points to the version of java that you want to use
  2. check the CATALINE_OPTS -Xms and -Xmx memory settings

Now enable, then start the service:

Code Block
sudo systemctl daemon-reload
sudo systemctl start tomcat

Lastly, check that the service started:

Code Block
sudo systemctl status tomcat

Conclusion

From this point on, tomcat will start automatically when the host restarts, and you can re-start tomcat manually like this:

Code Block
sudo systemctl restart tomcat