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 8 server.
Step 1./ Install Java 8
Step 2./ Install Tomcat 9 Centos 8
01- First, 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
$ sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
02- Now, go to the official Apache Tomcat website and download the most recent version of the software to your server.
$ VERSION=9.0.34 $ wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz
03- Once the download is complete, extract the tar file to the /opt/tomcat
directory:
$ sudo tar -xf /tmp/apache-tomcat-${VERSION}.tar.gz -C /opt/tomcat/
04- Apache Tomcat is updated regulary. So, to have more control over versions and updates, we’ll create a symbolic link as below:
$ sudo ln -s /opt/tomcat/apache-tomcat-${VERSION} /opt/tomcat/latest
05- Now, let’s change the ownership of all Apache Tomcat files as below:
$ chown -R tomcat:tomcat /opt/tomcat/
06- Make the shell scripts inside the bin directory executable:
$ sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'
07- Create the a systemd file with the following content:
$ vi /etc/systemd/system/tomcat.service [Unit] Description=Tomcat 9 server After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/jre" Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom" Environment="CATALINA_BASE=/opt/tomcat/latest" Environment="CATALINA_HOME=/opt/tomcat/latest" Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid" Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC" ExecStart=/opt/tomcat/latest/bin/startup.sh ExecStop=/opt/tomcat/latest/bin/shutdown.sh [Install] WantedBy=multi-user.target
08 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
09- 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
10- 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
01- 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>
02- 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.*" />
03- Finaly restart the Apache Tomcat Server daemon:
$ systemctl restart tomcat
Conclusion
You have successfully installed Apache Tomcat 9 Server on your CentOS 8 Server and you have learnt how create a user to access the Apache Tomcat web management interface. For more information about Apache Tomcat, visit the official documentation page.
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!