Apache Tomcat is a web server and servlet container that is used to serve Java applications. Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, released by the Apache Software Foundation. This tutorial covers the basic installation and some configuration of the latest release of Apache Tomcat 9 on CentOS 7 or RHEL 7 server.
Step 1./ Install Java 8
Apache Tomcat 9 requires Java 8 or newer to be installed on the server. Java 8 packages are available in the default CentOS 7 repositories. Run the following command to install Java 8
# yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
– Once the installation is completed, you can check the installed version using the following command:
# java -version openjdk version "1.8.0_161" OpenJDK Runtime Environment (build 1.8.0_161-b14) OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)
Step 2./ Install Tomcat 9
– Go to the official Apache Tomcat website and download the most recent version of the software to your server. At the moment the most recent release is version 9.0.34, here
# VERSION=9.0.34 # wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz
– untar the downloaded Apache Tomcat archive using the following command:
# tar -xzvf apache-tomcat-${VERSION}.tar.gz -C /opt
– Let’s rename the directory to something simpler like below:
# cd /opt # mv apache-tomcat-${VERSION} tomcat
– It is not recommended to run Apache Tomcat as user root, so we will create a new system user which will run the Apache Tomcat server
# groupadd tomcat # useradd -g tomcat -d /opt/tomcat -s /bin/nologin tomcat useradd: warning: the home directory already exists. Not copying any file from skel directory into it.
– After creating the Apache Tomcat user, let’s change the ownership of all Apache Tomcat files as below:
# chown -R tomcat:tomcat /opt/tomcat/
– Create the a systemd file with the following content:
# vi /etc/systemd/system/tomcat.service [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
– Now you can start the Apache Tomcat 9 server and enable it to start on boot time using the following command:
# systemctl daemon-reload # systemctl enable tomcat Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /etc/systemd/system/tomcat.service. # systemctl start tomcat
– Apache Tomcat by default is listening on port 8080. Before accessing the Apache Tomcat server, we need to adjust the firewall to allow our requests to get to the service. Use the following commands:
# firewall-cmd --zone=public --permanent --add-port=8080/tcp # firewall-cmd --reload
– You should now be able to access the Apache Tomcat server in your favorite web browser. Open the browser and navigate to http://IP_address:8080 and you will see the home page of Apache Tomcat as below.
Step 3./ Configure Apache Tomcat Web Management Interface
– By default, we can not access the Apache Tomcat web management interface because we have not created a user yet. Apache Tomcat users and their roles are defined in the tomcat-users.xml file. Open the tomcat-users.xml file and add the following lines:
# vi /opt/tomcat/conf/tomcat-users.xml
<tomcat-users>
<!--
Comments
-->
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="admin" password="PASSWORD_CHANGE_ME" roles="admin-gui,manager-gui"/>
</tomcat-users>
– By default Apache Tomcat web management interface is configured to allow access only from the localhost. If you want to be able to access the web interface from a remote IP or from anywhere which is not recommended because it is a security risk you can open the following files and make the following changes.
# vi /opt/tomcat/webapps/manager/META-INF/context.xml <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.1.*" /> # vi /opt/tomcat/webapps/host-manager/META-INF/context.xml <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.1.*" />
– Finaly restart the Apache Tomcat Server daemon:
# systemctl restart tomcat
Conclusion
You have successfully installed Apache Tomcat 9 Server on your CentOS 7 / RHEL 7 Server.
Read also:
- How To Configure Nginx as a Reverse Proxy for Apache Tomcat Server
- How To Configure Apache as a Reverse Proxy for Apache Tomcat Server
We hope this tutorial was enough helpful. If you need more information, or have any questions, just comment below and we will be glad to assist you!
2 comments
Great guide! This is one of the most complete guides out there to getting Tomcat 9 setup that I could find. I did run into one small issue I wanted to make mention of, listed below:
# cd /opt
# mv apache-tomcat-9.0.14/* tomcat
to
# cd /opt
# mv apache-tomcat-9.0.14 tomcat
Hi Anon,
Thank you for the comment, we already updated the tutorial with the correction .