May 10, 2025 - 02:47
Installing Tomcat 9 on CentOS 7 Image
Server Management

Installing Tomcat 9 on CentOS 7

Comments

Normally, I do this with a SH script, but since I need to write it down, I’ll describe the steps for you. What is Tomcat used for? I can say it runs Java scripts in a web environment. To be more specific: To run Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket, you need to install this software. My preference is CentOS 7. If you do a minimal installation, you will need to install additional packages. Pay attention to this. For example, you’ll need to install wget.

Let’s do some preliminary steps together. Let’s SSH into the server. First, let’s update all the packages.

GENEL
yum -y update

If the kernel was updated, let’s reboot the server. After that, the first thing we need to do is install Java.

Let’s check with java --version. If you didn’t get the screen below, it means we need to install it.

GENEL
java version '1.8.0_161'
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

The installation is easy :) Just run this command.

GENEL
yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64

Now that Java is installed, let’s start the Tomcat installation. The version of Tomcat we’re using when writing this is 9.0.12. Based on this, I’m using wget to fetch the packages to the server. If you get a "file not found" error, go to https://www.apache.org/dist/tomcat/tomcat-9/ and download it by checking the version. I prefer the .zip extension, but you can download the .tar.gz version if you prefer. (UPDATE: If you download it as .tar.gz, the extraction command will be different!)

GENEL
wget https://www.apache.org/dist/tomcat/tomcat-9/v9.0.12/bin/apache-tomcat-9.0.12.zip

Now, let’s extract the downloaded zip file to the /opt directory.

GENEL
unzip apache-tomcat-9.0.12.zip -d /opt

After extracting, let’s go to the opt directory and rename the folder to tomcat.

GENEL
cd /opt
mv apache-tomcat-9.0.12/ tomcat

UPDATE: For those who downloaded .tar.gz, let’s update the commands.

First, extract the package, then move it.

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

Now, we need to set the CATALINA_HOME environment variable. If you don’t do this, you won’t be able to run Tomcat. Run the following commands.

GENEL
echo 'export CATALINA_HOME=/opt/tomcat' >> ~/.bashrc
source ~/.bashrc

Let’s create a user for Tomcat.

GENEL
useradd -r tomcat --shell /bin/false

Let’s assign ownership of the tomcat folder to the user we just created. Why are we doing this? We don’t want Tomcat running with root privileges.

GENEL
chown -R tomcat:tomcat /opt/tomcat/

Now, let’s define a service for Tomcat so we can run commands like restart, start, stop, and status. Open the editor with the command below. I prefer using nano, but you can use vi if you like.

GENEL
nano /etc/systemd/system/tomcat.service

A blank file will open. Add the following code and save it.

GENEL
[Unit]
Description=Apache Tomcat 9
After=syslog.target network.target

[Service]
User=tomcat
Group=tomcat
Type=forking
Environment=CATALINA_PID=/opt/tomcat/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Let’s reload the CentOS 7 daemons since we added a new service.

GENEL
systemctl daemon-reload

Now, we can use the start, restart, stop, and status commands. Let’s start Tomcat and add it to the services so it runs automatically on reboot.

GENEL
systemctl start tomcat
systemctl enable tomcat

To check if Tomcat is running, use the command below.

GENEL
systemctl status tomcat

If you made any changes and want to restart it, just replace status with restart in the command.

GENEL
systemctl restart tomcat

Now, we can access Tomcat. To access it, go to: http://IP_ADDRESS:8080/. The Apache Tomcat page will appear. The rest is up to your Java knowledge. Goodbye.

If you have any additional questions, leave them in the comments. I will try to respond as best I can.

Related Articles

Comments ()

No comments yet. Be the first to comment!

Leave a Comment