Apache Tomcat (sometimes simply referred to as Tomcat) is a popular open-source web server software used to set up Java-based server environments. The technologies used in Tomcat include Java Servlet, Java Server Pages, Java Expression Language, and WebSocket and provides an HTTP server environment ideal for running Java-based applications. In this article, we will outline how to install Tomcat on your CentOS 7 server. Please note, in order to perform the instructions on this guide, you will need root SSH access to your VPS or Dedicated server.
Before you begin, make sure you have installed Java.
Please note that this article only pertains to Private Cloud and Bare Metal servers. Also, Control Web Panel (CWP) users can install this in a few clicks.
Installing Apache Tomcat
- First, ensure your machine and all related packages are up-to-date with the following commands:
sudo yum install epel-release
sudo yum update -y && sudo reboot
- Next, create a tomcat user and add it to the tomcat group with the following commands:
sudo groupadd tomcat
sudo mkdir /opt/tomcat
sudo useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat
- Next, change back to the home directory and download the Tomcat .tar file using the following commands:
Note: You will want to check the official Apache website for the latest stable release of the Tomcat software.
- Once it has finished downloading, extract the Tomcat archive using the following command:
sudo tar -zxvf apache-tomcat-8.0.33.tar.gz -C /opt/tomcat --strip-components=1
- Once the extraction finishes, update the file permissions with the following commands:
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/*
- Next, you will need to set up a systemd unit file using the following command to open a text editor:
sudo nano /etc/systemd/system/tomcat.service
- With the text editor open, add the following code:
[Unit]Description=Apache Tomcat Web Application Container
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
ExecStop=/bin/kill -15 $MAINPID
- Press Crtl + X to save and quit the text editor.
- Next, you will need to install and enable the haveged utility for security using the following commands:
sudo yum install haveged
sudo systemctl start haveged.service
sudo systemctl enable haveged.service
- Once that is done, you can start Tomcat and enable it to start on reboot with the following commands:
sudo systemctl start tomcat.service
sudo systemctl enable tomcat.service
Configuring Apache Tomcat
- Now that Tomcat has been installed, you will need to configure the firewall to allow browser access using the following commands:
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
- In order to start using the Tomcat Graphical User Interface (GUI), you will need to configure the users file with a new username and password. In this example, replace newusername and newpassword with the username and password you want to use to log in to Tomcat.
sudo nano /opt/tomcat/conf/tomcat-users.xml
<user username="newusername" password="newpassword" roles="manager-gui,admin-gui"/>
- Restart Tomcat
sudo systemctl restart tomcat.service
Testing Apache Tomcat
To test your Tomcat installation, you can simply visit your server IP followed by :8080. If the installation was successful, you should immediately see the login page.