How to install Apache Tomcat on CentOS 7?

0
823
tomcat
tomcat

To install Apache Tomcat on Linux requires certain steps that are not very complex. However, we’ve talked about it before recently. Today, I will show you how to install it, this time, on CentOS 7.

In short, Apache Tomcat executes Java servlets and renders Web pages that include Java Server Page coding. It constitutes a base element for the development with Java. It is very popular and it is open source.

This time I will install using CentOS 7, which is a Red Hat-based Linux distribution for use on Servers.

What you need

The process is very simple and you should not take long to do it, but it is good that you know that there are some small requirements to successfully complete this tutorial.

  • A server with CentOS 7 installed and allowing access via ssh.
  • A basic knowledge about the use of commands in the terminal.
  • A user who allows commands to be run as root. Or root access.

I think this will be enough for you to do everything without problems.

Let’s get to work.

1. Upgrade the system

In order to have the latest security patches, it is necessary to update the system completely. Also with this, you will have a more secure server and in many cases faster. Run as root user:

:~# yum update
1.- Upgrading CentOS
1.- Upgrading CentOS

That’s enough.

2.- Install Java

To install Apache Tomcat you first need to install Java because it is a dependency. To do this, type the following:

:~# yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
2.- Installing Java from the CLI
2.- Installing Java from the CLI

Next, check the installed Java version.

:~# java -version
3.- Showing what java version is installed
3.- Showing what java version is installed

As you can see, Java was correctly installed. This was done because Apache Tomcat 9 requires at least Java 8.

3. Install Apache Tomcat 9

Go to the official Apache Tomcat site and download the latest stable version.

4.- Apache Tomcat website
4.- Apache Tomcat website

Of course, you are working on a server, the best is to get the link directly and run:

:~# cd /opt/
:~# wget http://ftp.wayne.edu/apache/tomcat/tomcat-9/v9.0.12/bin/apache-tomcat-9.0.12.zip
5.- Downloading Apache Tomcat
5.- Downloading Apache Tomcat

Now proceed to decompress it.

:~# unzip apache-tomcat-9.0.12.zip
6.- Unzipping the file
6.- Unzipping the file

Then, rename the decompressed file to a simpler one.

:~# mv apache-tomcat-9.0.12 tomcat

Next, you have to set the CATALINA_HOME environment variable.

:~# echo "export CATALINA_HOME='/opt/tomcat/'" >> ~/.bashrc
:~# source ~/.bashrc
Setting the CATALINA_HOME variable
Setting the CATALINA_HOME variable

If you are going to install Apache Tomcat on a public server, then it is preferable to create a new system user to run it.

:~# useradd -r tomcat --shell /bin/false

Then, it changes the ownership of the folder to that of the new user. I named it to tomcat.

:~# chown -R tomcat:tomcat /opt/tomcat/
8.- Changing the owner of tomcat folder
8.- Changing the owner of the tomcat folder

4. Making Apache Tomcat as a service

To properly handle Apache Tomcat, it is a good idea to create a file that is made by a system service. This way you can start it, restart it or stop it in a simpler way. Run:

:~# nano /etc/systemd/system/tomcat.service

And put the following:

[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
9.- Making apache tomcat as a service
9.- Making apache tomcat as a service

Save and close the file. Now run:

:~# systemctl daemon-reload

5. Create an Apache Tomcat user account

To access Tomcat Manager, you have to create a new user. The process is simple, you have to edit the file /opt/tomcat/conf/tomcat-users.xml.

:~# nano /opt/tomcat/conf/tomcat-users.xml

And add the following:

<role rolename="admin-gui"/>
<user username="admin" password="PASSWORD" roles="manager-gui,admin-gui"/>
</tomcat-users>
10.- Creating a new user for Apache Tomcat
10.- Creating a new user for Apache Tomcat

Remember to change “PASSWORD” for your real password.

6. Starting the service and accessing through a web browser

Now you can start the Apache Tomcat service. Then check its status and finally access it from the web browser.

Now, you can start Apache Tomcat either using systemctl or from its own script.

:~# sh /opt/tomcat/bin/startup.sh
11.- Starting Apache Tomcat
11.- Starting Apache Tomcat

Now go to http://IP_SERVER/manage/html and log in with your username and your password.

Apache Tomcat manager page
Apache Tomcat manager page

Conclusion

Apache Tomcat is a vital tool for Java development and its installation is really simple on Linux and CentOS 7.

Let us know about you, Java programs? what is your experience with Tomcat? Did you find the tutorial easy?

Please spread this article through your social networks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here