How to Install Apache Tomcat on a CentOS 7 Cloud Server?

01-02-2024 02:05:19

Apache Tomcat is an open-source web server software designed to support the execution of Java web pages, and it is extensively utilized across significant web applications worldwide. As an introductory guide, this article outlines the procedure for installing Apache Tomcat 8 on a cloud server equipped with CentOS 7.

Updating the CentOS System

First, we update the operating system to the latest state and reboot the system upon completion.

sudo yum install epel-release
sudo yum update -y
sudo reboot

Installing Java

Before installing Apache Tomcat, it is necessary to install the Java SE environment. We can use the yum command to install the OpenJDK runtime environment.

sudo yum install java-1.8.0-openjdk.x86_64

After installation, verify the version of Java.

java -version

openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

For security purposes, it is advisable to create a separate user for Apache Tomcat, with both the username and group name being tomcat.

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

It is important to note that this tomcat user cannot log into the system. The user's home directory is /opt/tomcat, which also serves as the installation path for the Apache Tomcat program.

Installing Apache Tomcat

The latest version of the software can be downloaded from the official Apache Tomcat website at https://tomcat.apache.org/. Here, we will use Apache Tomcat 8.0.33 as an example.

cd ~
wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.33/bin/apache-tomcat-8.0.33.tar.gz
sudo tar -zxvf apache-tomcat-8.0.33.tar.gz -C /opt/tomcat --strip-components=1

Before officially using Apache Tomcat, it is necessary to set appropriate permissions.

cd /opt/tomcat
sudo chgrp -R tomcat conf
sudo chmod g+rwx conf
sudo chmod g+r conf/*
sudo chown -R tomcat logs/ temp/ webapps/ work/

sudo chgrp -R tomcat bin
sudo chgrp -R tomcat lib
sudo chmod g+rwx bin
sudo chmod g+r bin/*

Setting Up a Systemd Service

For convenience, we set up a Systemd service for Apache Tomcat.

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

The content is as follows:

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target

Now, we can easily start Apache Tomcat and set it to launch at system startup.

sudo systemctl start tomcat.service
sudo systemctl enable tomcat.service

Testing

To test whether Apache Tomcat has been successfully installed on the browser, we need to add the following rule to the firewall to open port 8080.

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

Finally, by visiting http://[your-server-IP]:8080, if everything is in order, the default homepage of Apache Tomcat should be visible. With the completion of the Apache Tomcat installation, we can now proceed to deploy web applications.