How to Install Apache Tomcat on CentOS 8

Install Apache Tomcat on CentOS 8

In this article, we will have explained the necessary steps to install and configure Apache Tomcat on CentOS 8. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Apache Tomcat is an opensource web server used to server Java Applications. It is an opensource implementation of Java Servlet, Java Server Pages and Java Expression Language.

Install Apache Tomcat on CentOS

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf update

Step 2. Install Java.

Apache Tomcat requires Java to be installed, Now we install Java using following command:

sudo dnf install java-1.8.0-openjdk-devel

Confirm Java version installed:

java -version

Step 3. Create Tomcat User and Group.

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

sudo groupadd --system tomcat
sudo useradd -d /usr/share/tomcat -r -s /bin/false -g tomcat tomcat

Step 4. Install Tomcat.

First, download Apache Tomcat from the official website and save it in your working directory. At the time of writing this article, Tomcat v9.0.30 is available for the installation:

wget http://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gz

Then, extract the Tomcat archive and create a new symbolic link to the folder:

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

Change the ownership of the directory to the tomcat user:

chown -R tomcat:tomcat /opt/tomcat/

Step 5. Create Systemd Unit File.

Tomcat’s systemd service file requires a Java installation location. So, list the available Java versions on your system using the following command:

alternatives --list  | grep ^java

Result:

java     auto    /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el8_0.x86_64/jre/bin/java

Run following command to create tomcat.service unit file inside /etc/systemd/system/ directory:

nano /etc/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat 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.222.b10-0.el8_0.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

Notify systemd that we created a new unit file and start the Tomcat service by executing:

sudo systemctl daemon-reload
sudo systemctl start tomcat

Step 6. Configure The Firewall Settings.

If your firewall running on your CentOS system and you want to access the tomcat interface from the outside of your local network you’ll need to open the port 8080:

firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload

Step 7. Configure Tomcat Web Management Interface.

Tomcat users and roles are defined in the tomcat-users.xml file. This file is a template with comments and examples describing how to configure the create a user or role:

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

Now add username and password for admin-gui and manager-gui. Make it sure you are setting strong username and password:

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

By default, Apache Tomcat restrict access to Manager and Host Manager apps to connections coming from the server also. You should remove these restrictions.

To change IP address restriction open following files. Open Manager app context file using below command:

nano /opt/tomcat/webapps/manager/META-INF/context.xml

Allow everyone to have access to Web manager:

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|.*" />

Open Host Manager app context file using below command:

nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

Allow everyone to have access to the Host manager.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|.*" />

Remember to restart the Tomcat service each time you edit Tomcat configuration files for changes to take effect:

sudo systemctl restart tomcat

Step 8. Test the Tomcat Installation.

Open your browser and type: http://<your-domain_or_IP-address>:8080

You should get following output for successful installation:

Install Apache Tomcat on CentOS 8

Now use Manager App visiting http://YOUR-DOMAIN_NAME_OR_IP-ADDRESS:8080/manager/html.

Install Apache Tomcat on CentOS 8

Congratulation, you have learned how to install and configure Apache Tomcat on CentOS 8. If you have any question, please leave a comment below.