The Apache Software Foundation also known as ASF recently released the latest Apache Tomacat server 8.5 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 8.5 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 the 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 finished completely.
When Java is installed check the java version with following command:
java -version
You’ll see a result similar to this :
HP_NO_IMG/data/uploads/users/3b85855d-36a1-4010-b06d-1cae5485254b/1720938978.png” alt=”” />
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 vim 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"
You can simply copy/paste this to your file.
After adding the Java Home Environment, save and exit from the editor.
Now we’ll have to edit .bash_profile so run below given command :
nano ~/.bash_profile
Now add 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 the 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 :
HP_NO_IMG/data/uploads/users/3b85855d-36a1-4010-b06d-1cae5485254b/1030856940.png” alt=” ” />
HP_NO_IMG/data/uploads/users/3b85855d-36a1-4010-b06d-1cae5485254b/1460376166.png” alt=” ” />
Installing Apache Tomcat 8.5
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 run below command to do so:
groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
The above commands will do the following:
-s /bin/false will disable shell access
-g tomcat will assign new user to the group tomcat
-d /opt/tomcat will define the home directory for the user
Downloading and Installing Apache Tomcat
First goto 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://apache.mirror.rafal.ca/tomcat/tomcat-8/v8.5.9/bin/apache-tomcat-8.5.9.tar.gz
Now extract using tar command as below :
tar -xzvf apache-tomcat-8.5.9.tar.gz
Move all the files that are in the apache-tomcat-8.5.9 directory to the tomcat directory to do so run following command :
mv apache-tomcat-8.5.9/* tomcat/
Now change the proper ownership using following command :
chown -hR tomcat:tomcat tomcat
Testing Tomcat
We recommend you run a short test on Tomcat to make sure that there is no error and it is installed successfully so follow the below given process .
Run the command below:
cd /opt/tomcat/bin/
./startup.sh
You’ll see a result stating that tomcat started like this:
HP_NO_IMG/data/uploads/users/3b85855d-36a1-4010-b06d-1cae5485254b/1898085672.png” alt=” ” />
Currently Tomcat is using port number 8080 so check the open port using netstat command as shown :
netstat -plntu
You’ll see something similar to this on your screen:
HP_NO_IMG/data/uploads/users/3b85855d-36a1-4010-b06d-1cae5485254b/1053736415.png” alt=” ” />
You can check this using a web browser with http://yourserver_IP:8080 then you’ll 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 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 as the service.
Run the following command :
nano /etc/systemd/system/tomcat.service
Add the below given content 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
HP_NO_IMG/data/uploads/users/3b85855d-36a1-4010-b06d-1cae5485254b/1527027223.png” alt=” ” />
Configure Tomcat Users
Run the 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 an result as “success”.
Now reload the firewall service using 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
You’ll see something similar to this :
HP_NO_IMG/data/uploads/users/3b85855d-36a1-4010-b06d-1cae5485254b/264944589.png” alt=” ” />
Web Access
Open you web browser and visit server with port number 8080 http://YourserverIPaddress:8080 then you’ll see homepage of Apache Tomcat like shown below :
HP_NO_IMG/data/uploads/users/3b85855d-36a1-4010-b06d-1cae5485254b/799599246.png” alt=” ” />
Click on 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/3b85855d-36a1-4010-b06d-1cae5485254b/2106685910.png” alt=” ” />
You can manage your Java applications using the Tomcat Web Application Manager. It is also used to Start, Stop, Reload, Deplo . 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 username and password so repeat the above procedure and click on log in you’ll see a virtual host manager dashboard like this:
HP_NO_IMG/data/uploads/users/3b85855d-36a1-4010-b06d-1cae5485254b/606669769.png” alt=” ” />
Conclusion
In this tutorial we have learned how to install and configure Apache Tomcat 8.5 server on a CentOS system very easily and we hope that now after this guide you have enough knowledge to work with Tomcat 8.5 .