We need a system running on CentOS 7 Linux, and having a user to login on the system with sudo privileges to execute installation command without any issue.
Tomcat application server needs Java to installed on the system. Here, we will install OpenJDK, which is default Java Development and runtime environment provided by CentOS.
You can install any java in your system ether OpenJDK or Oracle Java, follow this to install Java into CentOS.
The installation of OpenJDK is straightforward, first update the CentOS package repository index:
$ sudo yum update
Now, install OpenJDK using following command:
$ sudo yum install java-1.8.0-openjdk-devel
Create Tomcat User
$ sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
Install Tomcat Application Server
At the time of writing this tutorial, the latest version of Tomcat is 9.0.27. Before continuing the next step to install Tomcat, you should check the Tomcat 9 download page for the latest release. If there is a new version of Tomcat is available, copy the link od the core “tar.gz” file from the Binary distribution section.
$ cd /tmp $ wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz
After completing the download of Tomcat binary package, extract the package and move it to the “/opt/tomcat” directory using the following command:
$ tar -xf apache-tomcat-9.0.27.tar.gz $ sudo mv apache-tomcat-9.0.27 /opt/tomcat/
We can control Tomcat versions and updates by creating a symbolic link of tomcat installation directory with the name of “latest” as shown below:
$ sudo ln -s /opt/tomcat/apache-tomcat-9.0.27 /opt/tomcat/latest
$ sudo chown -R tomcat: /opt/tomcat
The scripts available inside “bin” directory must be having executable permission:
$ sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'
Create a system Unit File
$ sudo vim /etc/systemd/system/tomcat.service
[Unit] Description=Tomcat 9 servlet container 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
“you should modify the “JAVA_HOME” path as per your installation path”.
After saving the above service file, notify the system that we have a new unit file using the following command:
$ sudo systemctl daemon-reload
Now, start Tomcat service by executing the below command:
$ sudo systemctl start tomcat
We can use the following command to check the service status:
$ sudo systemctl status tomcat
● tomcat.service - Tomcat 9 servlet container Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2019-09-15 20:47:50 UTC; 4s ago Process: 1729 ExecStart=/opt/tomcat/latest/bin/startup.sh (code=exited, status=0/SUCCESS) Main PID: 1727 (java) CGroup: /system.slice/tomcat.service
If we are not getting any error in the service file, we will enable the Tomcat service to start it automatically on system boot time:
$ sudo systemctl enable tomcat
Adjust the Firewall
If a firewall protects your CentOS 7 Operating system, and you want to access your Tomcat from the outside of your local network, need to open port 8080 on the firewall as tomcat service is running on default port 8080.
$ sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp $ sudo firewall-cmd --reload
Generally, we are using the Tomcat application server with a load balancer or reverse proxy server. It is the best practice to restrict access on port 8080 for the local network and use the application on a proxy server.
Configure Tomcat Web Management Interface
Now the Tomcat is successfully installed and running into the CentOS 7 machine. In the next step, we will check how to create a user to access the Tomcat’s web management interface.
$ sudo vim /opt/tomcat/latest/conf/tomcat-users.xml
<tomcat-users> <!-- Comments --> <role rolename="admin-gui"/> <role rolename="manager-gui"/> <user username="admin" password="admin_password" roles="admin-gui,manager-gui"/> </tomcat-users>
<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>
<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|220.127.116.11" /> </Context>
The allowed IP addresses separated by vertical bar “|”. You can use it to add a single IP address or use a regular expression. To take effect of the above configuration, always restart Tomcat services when you edit Tomcat configuration files:
$ sudo systemctl restart tomcat
Test the Tomcat Installation
The Tomcat web application manager dashboard is available on url “http://<your_domain_or_IP_address>:8080/manager/html”. You can deploy, undeploy, start, stop and reload application from this manager window.
Similarly, Tomcat virtual host manager dashboard is available on url “http://<your_domain_or_IP_address>:8080/host-manager/html”. This dashboard you can use to create, delete and manage Tomcat virtual hosts.
To sign in above both panel, you can use username and password which have created for tomcat web interface access in the above steps.
You have installed Tomcat 9 on your CentOS 7 machine. You have learned to access tomcat applications, manager and host-manager dashboard. You lean to configure tomcat user to access web interfaces to manage tomcat services and virtual hosts.