How to install Apache Tomcat 9 on CentOS 7

Tutorials 0 lượt xem

Apache Tomcat is a web server used to run web applications written in Java.

Apache Tomcat implements Java related technologies such as Servlet, JSP. As of the time of this article Apache Tomcat 9 is the latest version.

In today’s tutorial I will show you how to install Apache Tomcat 9 on CentOS 7.

Prepare

You need to prepare the following:

  • A brand new Linux server with CentOS 7 installed. I used Linode to complete this tutorial. You can refer to cheap Java Hosting service.
  • Log in to the Linux server with an SSH Client such as MobaXTerm as the root user.

Step 1: Install Java

First you need to update the system. Run the following command:

You also need some utility tools like wget and nano, which we will use in a later step. Run the following command:

Next you need to install the Java Development Kit (JDK). Go to the JDK 8 download page on the Oracle website . Click the Accept License Agreement button and copy the appropriate .rpm package download link:

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Download JDK 8 RPM with the following command:

Remember to change the version number to suit your situation. Next install the JDK with the following command:

Once Java is installed you check the Java version with the following command:

You will see results like this:

install apache tomcat 9 on centos 7 5

Setting up the Java Home environment

Before starting to install Tomcat you need to set up a Java Home environment, check the location of Java installation with the following command:

You will see results like below:

install apache tomcat 9 on centos 7 6

Now edit the environment file (/etc/environment) with the nano editor:

Add environment variable like this:

install apache tomcat 9 on centos 7 7

Save the file and exit nano.

Next, edit the .bash_profile file:

Add environment variable as below:

Save the file and exit nano.

Reload the .bash_profile file with the following command:

Check the JAVA_HOME variable again with the following command:

You will see results like below:

install apache tomcat 9 on centos 7 2

Install Apache Tomcat 9

First we create a tomcat group:

Next, they add a user tomcat to this group:

-s /bin/false: disable shell access

-g tomcat: assign new user to tomcat group

-d /opt/tomcat: user’s home directory.

Download and install Apache Tomcat:

Navigate to the /opt directory with the following command:

Download the latest version of Apache Tomcat 9 :

Now extract with tar command like below:

Move all files located in apache-tomcat-9.0.1 to the tomcat directory:

Change ownership of the tomcat directory with the following command:

Create service file

You need to create service file so it will be easy to start, stop or restart apache tomcat service.

Run the following command to create the service file:

Copy and paste the content below into the service file:

Save and exit nano. Then run the following commands to start the tomcat service and enable the service on server restart:

Tomcat will start and listen on port 8080. You can check with the following command:

Run the following command to check the status of the tomcat service:

Now you open your browser and go to http://your_ip_address:8080 . You will see the Apache Tomcat homepage as follows:

install apache tomcat 9 on centos 7 8

Configure Tomcat User

You modify tomcat-users.xml to use nano:

Add the following content:

The final file is as follows:

install apache tomcat 9 on centos 7 9

Default manager gui and admin gui only access from localhost. So you need to edit the context file for remote access.

First you edit the context.xml file in webapps:

Comment the code as below:

 

install apache tomcat 9 on centos 7 10

Edit the remaining context.xml file with the following command:

Comment code similar to above:

Save the file and restart tomcat:

Firewall configuration Firewalld

In case you have not installed the Firewalld firewall you can install it using the following command:

Turn on the firewall and let it run on reboot:

Now you add tomcat port 8080 to the firewall with the following command:

You will see the result: success.

Now you reload the firewall using the following command:

You can check all the ports and services that are open with the following command:

So far you have learned how to install Apache Tomcat 9 on Centos 7.

How to start Tomcat at port 80

To start Tomcat at port 80 so you no longer enter the port when accessing, there is only one way as follows. Type in the following 2 commands. These commands will forward port 80 to port 8080 that Tomcat is listening on:

Note: Changing the server.xml file from port 8080 to 80 will not work because we are starting Tomcat not the root user. Users who are not root will not have permission to open the port. See discussion HERE

If you have problems with the installation, leave a comment below.

Bài viết liên quan