Open a terminal and login as root user
su -
Tomcat requires having Java 8 installed on your machine. You can either use Oracle JDK or OpenJDK.
It is quite easy to install OpenJDK on RedHat
yum install -y java-1.8.0 wget
For installing OraclE JDK, please refer to the following post at Oracle JDK.
Check the Java version
java -version
Output:
oopenjdk version "1.8.0_141" OpenJDK Runtime Environment (build 1.8.0_141-b16) OpenJDK 64-Bit Server VM (build 25.141-b16, mixed mode)
Next step, we will create a user for running Tomcat service.
getent group | grep webservice groupadd webservice
# add user tomcat to group webservice useradd -g webservice -d /opt/tomcat -s /bin/nologin tomcat
# delete user tomcat userdel -r tomcat
To list all local users:
cut -d: -f1 /etc/passwd
Now we can download the latest version of Tomcat and start the installation.
wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.28/bin/apache-tomcat-8.5.28.tar.gz
Download the checksum file and verify the release integrity of the downloaded files.
wget --no-check-certificate https://www.apache.org/dist/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz.md5.sha512
# sha512sum apache-tomcat-*.tar.gz
Output: 8d8fd487bbe4a3c936fb91edf674a5b3775fd83b5ce3bbb383b942d25770dd5e7f32ebc2f1deafacdc14c8241ca658711103a161490dfb9fe3287276ddf2a5de apache-tomcat-8.5.28.tar.gz
cat apache-tomcat-*.sha512
Output: 8d8fd487bbe4a3c936fb91edf674a5b3775fd83b5ce3bbb383b942d25770dd5e7f32ebc2f1deafacdc14c8241ca658711103a161490dfb9fe3287276ddf2a5de *apache-tomcat-8.5.28.tar.gz
If both matches then extract the Tomcat archive and move it to your desired (e.g. /opt/tomcat) directory.
tar -zxvf apache-tomcat-*.tar.gz mv apache-tomcat-8.*/* /opt/tomcat/
Change the ownership of the directory so that tomcat user can write files in it.
chown -R tomcat:webservice /opt/tomcat/
Make sure that the ownership is granted accordingly.
# cd /opt/tomcat/ # ll total 92 drwxr-x---. 2 tomcat webservice 4096 Mar 8 05:27 bin drwx------. 3 tomcat webservice 254 Mar 8 05:28 conf drwxr-x---. 2 tomcat webservice 4096 Mar 8 05:27 lib -rw-r-----. 1 tomcat webservice 57092 Feb 6 18:12 LICENSE drwxr-x---. 2 tomcat webservice 197 Mar 8 05:28 logs -rw-r-----. 1 tomcat webservice 1723 Feb 6 18:12 NOTICE -rw-r-----. 1 tomcat webservice 7138 Feb 6 18:12 RELEASE-NOTES -rw-r-----. 1 tomcat webservice 16246 Feb 6 18:12 RUNNING.txt drwxr-x---. 2 tomcat webservice 48 Mar 8 06:25 temp drwxr-x---. 7 tomcat webservice 81 Feb 6 18:10 webapps drwxr-x---. 3 tomcat webservice 22 Mar 8 05:28 work
Start/stop Tomcat web server
Using command line
cd /opt/tomcat/bin
# start the Tomcat Web server sh startup.sh
# shutdown the Tomcat Web server sh shutdown.sh
Using the systemd
Create a systemd Tomcat service file
vi /etc/systemd/system/tomcat.service
# or emacs /etc/systemd/system/tomcat.service
# Systemd unit file for tomcat [Unit] Description=Apache Tomcat Web Application Container After=syslog.target network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/jre Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid Environment=CATALINA_HOME=/opt/tomcat Environment=CATALINA_BASE=/opt/tomcat Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC' Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom' ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/bin/kill -15 $MAINPID User=tomcat Group=webservice UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
Reload systemd daemon
systemctl daemon-reload
In order to start the Tomcat service, run
systemctl start tomcat
In order to stop the Tomcat service, run
systemctrl stop tomcat
Enable the auto start of Tomcat service on system start, run:
systemctrl enable tomcat
Verify Tomcat status
curl localhost:8080
netstat -antup | grep 8080
Firewall
Don't forget to allow port 8080 in the firewall so that we can access Tomcat from external networks
firewall-cmd --permanent --add-port=8080/tcp firewall-cmd --reload
In the next post, we will learn how to configure and manage the Tomcat Web Server via the Web interface.
References:
https://www.itzgeek.com/how-tos/linux/centos-how-tos/install-apache-tomcat-8-on-centos-7-rhel-7.html