How to Install Apache Tomcat on CentOS 7

r00t December 22, 2017

Install Apache Tomcat on CentOS 7

In this tutorial we’ll learn how to install Apache Tomcat on CentOS 7. We will also install and configure its prerequisites. Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, published by the Apache Software Foundation.

I recommend to use a minimal CentOS server setup as a basis for the tutorial, that can be a virtual or a root server image with an CentOS 7 minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Apache Tomcat on CentOS 7

Step 1. First, ensure your system is fully up-to-date by running the following:

yum -y install epel-release
yum -y update

Step 2. Installing Java.

Install the latest available version of Java on your server:

yum install java-1.8.0-openjdk.x86_64

Once Java is installed, you can verify it by running the following command:

java -version

Step 3. Create Tomcat User.

Tomcat should not be run as root user. So, create a normal user for running the Tomcat service:

groupadd tomcat
useradd -g tomcat -d /opt/tomcat -s /bin/nologin tomcat

Step 4. Installing the latest Apache Tomcat.

First, download the latest version of the Apache Tomcat from the website:

wget http://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.2/bin/apache-tomcat-9.0.2.tar.gzhttp://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.2/bin/apache-tomcat-9.0.2.tar.gz.md5

Then extract the Tomcat archive and move it to your desired (/opt/tomcat) directory:

tar -zxvf apache-tomcat-*.tar.gz
mv apache-tomcat-9.0.2/* /opt/tomcat/

Now change the proper ownership using following command:

chown -R tomcat:tomcat /opt/tomcat/

Step 5. Testing Tomcat.

Apache Tomcat can be started and stopped by the script which comes with the package. Start the Apache Tomcat:

cd /opt/tomcat/bin/
sh startup.sh

Tomcat is using port 8080 now, check the open port on the server with the netstat command:

netstat -plntu

Stop the Apache Tomcat:

cd /opt/tomcat/bin/
sh shutdown.sh

Step 6. Setup Systemd unit file for Apache Tomcat.

You should setup a Systemd unit file for Apache Tomcat:

nano /etc/systemd/system/tomcat.service

Add below information to Tomcat systemd service file:

[Unit]
Description=Apache Tomcat 9.x Web Application Container
Wants=network.target
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.el7_3.x86_64/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1G -Djava.net.preferIPv4Stack=true'
Environment='JAVA_OPTS=-Djava.awt.headless=true'

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

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

[Install]
WantedBy=multi-user.target

Reload the systemd daemon, then start and add the Apache Tomcat service at boot time:

systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat

Step 7. Configure Firewall.

In order to test Apache Tomcat in a web browser, you need to modify the firewall rules:

sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

Step 8. Configure Tomcat Web Management Interface.

In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the tomcat-users.xml file:

nano /opt/tomcat/conf/tomcat-users.xml

Place the following two lines just above the last line:

<role rolename="admin-gui,manager-gui"/>
<user username="admin" password="tomcat" roles="manager-gui,admin-gui"/>

Restart Apache Tomcat to put your modifications into effect:

systemctl restart tomcat

Step 9. Accesing Apache Tomcat.

The Apache Tomcat is now accessible on the default port, which is 8080. The admin UI should be accessible at http://your_server_ip:8080/solr.

Congratulation’s! You have successfully install and configured Apache Tomcat on your CentOS 7 server. Thanks for using this tutorial for installing Apache Tomcat on CentOS 7 system.

The Tags:

Leave a Comment

Comments are closed.