The Apache Software Foundation, also known as ASF recently released Apache Tomcat server 9 on December 8, 2016. Tomcat server implements Java servlet and Java servlet technologies like JSP Java WebSocket. Apache Tomcat is an open-source web server and servlet container that is used to serve Java applications.
It is developed by the Apache Software Foundation, written in Java and released under Apache License 2.0.
Requirements
Tomcat 9 does not require any special kind of hardware it will need a CentOS server, IP Address of your server and root privileges on the server.
Installing Java
Before installing Tomcat we will have to install Java Development Kit (JDK) on our system but it is recommended that you update the system and available packages before installing JDK. Run the following command to update system.
yum -y update
Now once the system is updated install JDK and to do so run following command:
yum -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
The above command will take some time to install JDK so you should wait until the installation process has finished.
When Java is installed check the java version with the following command:
java -version
You’ll see a result similar to this :
[root@ip-172-31-16-36 ~]# java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-b15)
OpenJDK 64-Bit Server VM (build 25.111-b15, mixed mode)
[root@ip-172-31-16-36 ~]#
Java Home Environment
Before you configure Java Home Environment we recommend you to check where the Java directory is and to do so run following command:
sudo update-alternatives --config java
Now edit the environment file with any text editor here we are using nano editor but you can choose anyone you wish.
nano /etc/environment
Now add the Java Home Environment variable as shown below :
JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64/jre"
yYou can simply copy/paste this to your file.
After adding Java Home Environment save and exit from the editor.
Now we’ll have to edit the .bash_profile so run the command given below:
nano ~/.bash_profile
Now add the Home Environment variable as shown below:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64/jre
export PATH=$JAVA_HOME/bin:$PATH
Save file and exit from the editor.
We recommend you reload the .bash_profile, to do so run following command:
source ~/.bash_profile
We recommend you make sure there is no error in configuring Home Environment variable and also check the Java Home environment variable :
echo $JAVA_HOME
You will see output similar to this containing the path to java directory :
[root@ip-172-31-16-36 ~]# nano ~/.bash_profile
[root@ip-172-31-16-36 ~]# source ~/.bash_profile
[root@ip-172-31-16-36 ~]# echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64/jre
[root@ip-172-31-16-36 ~]#
Installing Apache Tomcat 9
We’ll have to create a system user and group before proceeding with the installation process of Tomcat so create a user and group named tomcat to run below-given command to do so:
groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
The options used in the above command perform the following:
-s /bin/false will disable shell access
-g tomcat will assign a new user to the group tomcat
-d /opt/tomcat will define the home directory for the user
Downloading and Installing Apache Tomcat
First, go to the directory /opt and download Apache Tomcat server using wget command from http://tomcat.apache.org as shown below:
cd /opt/
This will change your current directory to directory /opt/
If in any case you haven’t installed wget command on your system then first of all install it using this command:
yum install wget
wget http://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.0.M15/bin/apache-tomcat-9.0.0.M15.tar.gz
Now extract using tar command as below :
tar -xzvf apache-tomcat-9.0.0.M15.tar.gz
Move all the files that are in the apache-tomcat-9.0.0.M15 directory to the tomcat directory to do so run following command :
mv apache-tomcat-9.0.0.M15/* tomcat/
Now change the proper ownership using the following command:
chown -hR tomcat:tomcat tomcat
Testing Tomcat
We recommend you to run a short testing on Tomcat to make sure that there is no error and it is installed successfully so follow the below-given process.
Run below given command :
cd /opt/tomcat/bin/
./startup.sh
Currently, Tomcat is using port number 8080 so check the open port using netstat command as shown:
netstat -plntu
You can check this using a web browser with http://yourserver_IP:8080 then you’ll see the homepage of Apache Tomcat Server but we recommend you to use browser later at final testing because we will run Tomcat with a systemd service file in the final configuration.
Now run following commands :
cd /opt/tomcat/bin/
./shutdown.sh
chown -hR tomcat:tomcat /opt/tomcat/
Creating a systemd Service File
Now you’ll need to create a systemd service file to run Apache Tomcat as a tomcat user. Creation of a systemd service file is recommended for easy starting and stopping the service.
Run following command :
nano /etc/systemd/system/tomcat.service
Add the content below into nano text editor, you can simply copy/paste this into the file:
[Unit]
Description=Apache Tomcat 8 Servlet Container
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
Save and close the file then run the following commands to start the Tomcat service and enable Tomcat service to start on boot:
systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat
Once again check for the open port using netstat command shown below:
netstat -plntu
Now run the command below and make sure that service is active as shown below:
systemctl status tomcat
Configure Tomcat Users
Run following command to edit tomcat-users.xml file using any text editor here we are using nano :
cd /opt/tomcat/conf/
nano tomcat-users.xml
Create a new line under line 43 and add following content, again you can simply copy-paste this into file like you’ve done before:
<role rolename="manager-gui"/>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>
Save the file and exit text editor.
Now you’ll have to edit context.xml file using following commands:
cd /opt/tomcat/webapps/manager/META-INF/
nano context.xml
Now you’ll have to comment out line 19 and 20 as shown below:
<Context antiResourceLocking="false" privileged="true" >
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
</Context>
Save the file and exit nano text editor .
Now once again edit the context.xml file but this time it is in host-manager directory so run below given commands :
cd /opt/tomcat/webapps/host-manager/META-INF/
nano context.xml
Comment out again line 19 and 20.
<Context antiResourceLocking="false" privileged="true" >
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
</Context>
Save the file and exit nano text editor .
Now you’ll have to restart the tomcat and to do so run following command:
systemctl restart tomcat
Configure Firewalld
If in any case you haven’t installed firewalld on your system then install it first using yum command as shown below:
yum install firewalld
You’ll have to start firewalld and add it to start at boot time, to do so run following command:
systemctl start firewalld
systemctl enable firewalld
Now you’ll have to use firewall-cmd command to add apache tomcat port number 8080 to the firewall so simply enter below-given command:
firewall-cmd --zone=public --permanent --add-port=8080/tcp
You’ll see a result as “success”.
Now reload the firewall service using the following command:
firewall-cmd --reload
We recommend you to check for all service available and port 8080 is open and there is no error, to do so run following command:
firewall-cmd --list-ports
firewall-cmd --list-services
Web Access
Open your web browser and visit server with port number 8080 http://YourserverIPaddress:8080 then you’ll see the homepage of Apache Tomcat like shown below:
HP_NO_IMG/data/uploads/users/744ed2f1-2baa-42b2-b09d-ad15a2ae3934/241207610.png” alt=” ” />
Click on the Manager app on the top right side of your screen then you’ll be asked to username and password for authentication enter your username and password and you will see manager dashboard like this:
HP_NO_IMG/data/uploads/users/744ed2f1-2baa-42b2-b09d-ad15a2ae3934/282114080.png” alt=” ” />
You can manage your Java applications using the Tomcat Web Application Manager. It is also used to Start, Stop, Reload, Deploy. You can also diagnostics on your apps using Tomcat Web Application Manager.
Now go to the host manager and here you’ll be again asked to enter a username and password so repeat the above procedure and click on login you’ll see a virtual host manager dashboard like this:
HP_NO_IMG/data/uploads/users/744ed2f1-2baa-42b2-b09d-ad15a2ae3934/1377653970.png” alt=” ” />
Conclusion
In this tutorial, we have learned how to install and configure Apache Tomcat 9 server on a CentOS system very easily and we hope that now after this guide you have enough knowledge to work with Tomcat 9.