Hi everyone, today we'll be learning how to install Tomcat 8.0.18 on Linux machine. This tutorial includes short description of Apache Tomcat and some significant features of Tomcat 8.
Apache Tomcat is an open source web server and servlet container developed by the Apache Software Foundation. It implements the Java Servlet, JavaServer Pages (JSP), Java Unified Expression Language and Java WebSocket specifications from Sun Microsystems and provides a web server environment for Java code to run in.
Tomcat 8.0.18 is the latest version of the 8th generation, it’s aligned with Java EE 7. In addition to supporting updated versions of the Java EE specifications, Tomcat 8 includes a number of improvements compared to Tomcat 7 , here are some few details about Tomcat 8.
- Tomcat 8 requires JAVA 7 to work.
- Tomcat 8 supports Java Servlet 3.1
- Tomcat 8 supports JavaServer Pages 2.3
- Tomcat 8 supports Java Unified Expression Language 3.0
- Tomcat 8 supports Java WebSocket 1.0
So, here are the steps that we'll need to follow to setup and configure Apache Tomcat 8.0.18 on our Linux Machine which includes RHEL/CentOS, Ubuntu and their derivatives.
1. Installing and Configuring Java
First of all, we'll need to make sure that we have properly installed JAVA in our Linux machine before heading up to install Tomcat. We'll need 7 or greater version of JAVA installed in our Linux machine. To check if we have a working JAVA installed in our system, we'll need to run the following command in a shell or a terminal.
# java -version
We have JAVA installed in our system already. But incase if have no JAVA installed in the machine, we'll need to install it. It can be installed by executing the following command in a shell or terminal. Installing JAVA depends upon the distribution of Linux Operating System used currently.
To install on Ubuntu Distribution:
# apt-get install openjdk-7-jdk
To install on CentOS/RHEL 7 Distribution:
# yum install java-1.7.0-openjdk
To install on OpenSuse Distribution:
# zypper install java-1_7_0-openjdk
2. Downloading Tomcat 8
We can download tomcat from the official download site of tomcat. As the latest and stable release of tomcat now is version 8.0.18, we'll gonna download tomcat 8.0.18 and install it on our Linux system. So, we'll gonna download its tarball file ie tar.gz inside /tmp folder and then extract it under /usr/share/ . To do so, we'll need to execute the following commands in a shell or terminal.
# cd /tmp/ # wget http://apache.mirrors.hoobly.com/tomcat/tomcat-8/v8.0.18/bin/apache-tomcat-8.0.18.tar.gz
Now, we'll gonna extract the files of the tarballs to /usr/share/
# cd /usr/share/ # tar -zxvf /tmp/apache-tomcat-8.0.18.tar.gz
3. Adding tomcat user & group
It is highly recommended not to run Tomcat as root. So, we will need to create an unprivileged user for it and set the appropriate owner of the tomcat folder:
# groupadd tomcat # useradd -g tomcat -s /bin/bash -d /usr/share/apache-tomcat-8.0.18 tomcat
# chown -Rf tomcat.tomcat /usr/share/apache-tomcat-8.0.18/
4. Running Tomcat 8
Now, finally we'll start our apache tomcat server. First we'll need to switch to the unprivileged user with the command below.
# su – tomcat
Then, we'll start our Apache Tomcat a shell script called startup.sh which is inside bin folder. So, we'll need to run:
$ cd ~/bin $ ./startup.sh
And yes, we have successfully started tomcat 8.0.18 . Now if everything went according, we must be able to access it in our browser with http://localhost:8080 or http://SERVER-IP:8080 according to the host.
If you are trying to run tomcat 8 on firewall enabled CentOS 7 then we may need to open port 8080 which can be done by running the below commands under root access:
# firewall-cmd --zone=dmz --add-port=2888/tcp --permanent # firewall-cmd --reload
To shutdown it, we can simply run another shell script called shutdown.sh which will automatically shutdown tomcat server.
5. Setup user accounts
Finally we have to configure Tomcat users so they can access admin/manager sections. We can do that by adding the users in the ~/conf/tomcat-users.xml file with our favorite text editor.
$ nano ~/conf/tomcat-users.xml
Now, we'll wanna add a manager-gui role to a user with username as linoxide and password as linoxidepass:
<role rolename="manager-gui"/> <user username="linoxide" password="linoxidepass" roles="manager-gui"/>
Then, we'll wanna add an admin-gui role to an user with username as admin and password as adminpass:
<role rolename="admin-gui"/> <user username="admin" password="adminpass" roles="manager-gui, admin-gui"/>
Note: Please change the username and password according to your desire.
After setting up the admin and managers roles, we'll need restart the Tomcat and then try to access the admin section. To do so, we'll need to run the below commands in a shell or terminal:
$ cd ~/bin $ ./shutdown.sh $ ./startup.sh
To login to the manager app, we'll need to browse to http://ip-address:8080/manager/ or http://localhost:8080/manager according to the host configuration. And if everything went cool, we must get a login window as in which we must enter the username and password which we just added above.
After login, we'll see the below as we have successfully logged in:
6. Running Multiple Instances (Optional)
Sometimes we'll need to run more than one instance of Tomcat on the same server. To do this, we'll need to run as root then we'll need to go back to the /usr/share directory where we first downloaded tomcat and extract it again in a different folder like this:
Note: To exit user tomcat to root, please execute exit in the current shell or terminal.
# cd /usr/share # mkdir apache-tomcat-2
# tar -zxvf /tmp/apache-tomcat-8.0.18.tar.gz -C apache-tomcat-2 --strip-components 1 # chown -Rf tomcat.tomcat /usr/share/apache-tomcat-2/
Now we need switch user to tomcat to open the conf/server.xml file in the new installation folder and change the port numbers like below. First, to switch user to tomcat from root, execute the following:
# su - tomcat
Now, shutdown the server by running the below command:
Then, to edit the file server.xml, we'll need to open it with a text editor as:
$ nano ~/conf/server.xml
The shutdown port from:
<Server port="8005" shutdown="SHUTDOWN">
<Server port="8006" shutdown="SHUTDOWN">
The connector port from:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
And the AJP port from:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
Now, we'll want to save the changes we made to that file. After that, we can just switch back to the tomcat user and start the second instance like this:
$ cd ~/bin $ ./startup.sh
If everything went accordingly, we should now access the new instance of tomcat with your browser at http://localhost:8081/ or http://SERVER-IP:8081/
Hurray! We have successfully installed Tomcat version 8.0.18 in our Linux based Distribution. This tutorial includes installation of Tomcat 8.0.18 in different distributions like Red Hat, CentOS, Ubuntu, OpenSuse and their derivatives. Tomcat 8.0.18 is the latest version of the 8th generation, it’s aligned with Java EE 7. If you have any questions, comments, feedback please do write on the comment box below and let us know what stuffs needs to be added or improved. Thank You! Enjoy your latest Tomcat 8.0.18 :-)