In order to run Java applications on a web server, Apache Tomcat is always a good choice. It is open source (FREE), has proved success stories for the implementation of Java Servlet and JavaServer Pages technologies. The latest stable version is 8.5. Today we are going to have a taste on the development version 9. Like other tutorials for Tomcat 8 available on the Internet, we will cover the basic installation and some configuration of the latest release of Tomcat 9 on CentOS 7 server.
We will have Tomcat 9 installed into the directory /opt/tomcat, and configured as a system service on CentOS 7 server. The Tomcat service will be started on server boot.
Before you begin with this guide, you should have a Linux server running with CentOS 7, and you must have password for the root user, or a non-root user granted with sudo permissions.
First of first, Java Development Toolkit is necessary for running Tomcat, so any Java web application code can be executed. As we are going to have Tomcat 9, let’s follow the official document to install OpenJDK 8 with yum.
[[email protected] ~]# yum -y install java-1.7.0-openjdk-devel
Once the Java is installed successfully, let’s create a tomcat user for the Tomcat service.
Create Tomcat User
For security reason, Tomcat should never be run as root user. The best practice is to create a new user and group that will be allowed to run only the Tomcat service.
Let’s create new tomcat user and the tomcat group, with the installation directory of /opt/tomcat, and with a shell of /bin/false (so this account will not be used for login):
[[email protected] ~]# sudo groupadd tomcat
[[email protected] ~]# sudo useradd -M -s /bin/nologin -g tomcat -d /opt/tomcat tomcat
To install Tomcat 9 is easy, just download the latest binary release and then decompress it to the installation directory.
Download Tomcat Binary
Find the latest version of Tomcat 9 at http://ftp.nluug.nl/internet/apache/tomcat/tomcat-9/. At the time of writing, the latest version is v9.0.0.M15. Under the Binary Distributions section, then under the Core list, copy the link to the “zip”.
Then use wget and paste in the link to download the Tomcat 9 archive.
Let’s create the installation directory /opt/tomcat, and then extract the the archive to it with these commands:
[[email protected] ~]# mkdir -pv /opt/tomcat
[[email protected] ~]# unzip apache-tomcat-9.0.0.M15.zip
[[email protected] ~]# mv apache-tomcat-9.0.0.M15 /opt/
[[email protected] ~]# ln -s /opt/apache-tomcat-9.0.0.M15 /opt/tomcat
The last command is to create a shortcut /opt/tomcat for easy access to /opt/apache-tomcat-9.0.0.M15. If you would prefer to rename the /opt/apache-tomcat-18.104.22.168.M15, that’s also a good choice.
To enable the tomcat user to have the proper access to the installation, we will perform following actions:
[[email protected] ~]# cd /opt/tomcat
[[email protected] ~]# chgrp -R tomcat conf
[[email protected] ~]# chmod g+rwx conf
[[email protected] ~]# chmod g+r conf/*
[[email protected] ~]# chown -R tomcat webapps/ work/ temp/ logs/
[[email protected] ~]# chmod a+x bin/*.sh
Now all required permissions are granted, let’s configure the system service.
Produce Systemd Unit File
Tomcat can be configured as a system service, to make it we must have Tomcat Systemd unit file.
[[email protected] ~]# vi /etc/systemd/system/tomcat.service
Paste in the following script. If want to modify the memory allocation settings that are specified in CATALINA_OPTS, feel free to update:
# Systemd unit file for tomcat
Description=Apache Tomcat Web Application Container
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
ExecStop=/bin/kill -15 $MAINPID
Press ESC and type wq! to save and exit.
Now reload Systemd and let’s start the Tomcat service:
One of our objectives is to start the Tomcat service on server boot, run this command:
systemctl enable tomcat
Now let’s check our achievement, open in web browser: http://server_IP_address:8080
We should see the default Tomcat splash page, in addition to other information.