Install Java
For this installation we will use Java 11 OpenJDK.
Install JDK from https://jdk.java.net/11 or on Centos terminal execute
sudo yum install java-11-openjdk-devel
Check your version by typing
java --version
If you have multiple Java versions installed on the server you can change the default version using the alternatives system utility:
sudo alternatives --config java
[[email protected] ~]# sudo alternatives --config java There are 2 programs which provide 'java'.
Selection Command -----------------------------------------------
1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.1.13-3.el7_6.x86_64-debug/bin/java)
*+ 2 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.1.13-3.el7_6.x86_64/bin/java)
At the time of writing last Tomcat version was 9.0.16 at http://ftp.carnet.hr/misc/apache/tomcat/tomcat-9/v9.0.16/bin/apache-tomcat-9.0.16.tar.gz
Set JAVA_HOME
Tomcat setup
Create group tomcat:
[[email protected] ~]# sudo groupadd tomcat
Create user tomcat and add it to group tomcat :
[[email protected] ~]# sudo useradd -M -s /bin/nologin -g tomcat -d /opt/tomcat tomcat
This will also disable anyone from logging in as tomcat user.
Create the directory where tomcat will be installed and unpack the archive :
[[email protected] ~]# mkdir /opt/tomcat
[[email protected] architech]# sudo tar xvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1
Change the ownership of the tomcat files
# cd /opt/tomcat
# sudo chgrp -R tomcat /opt/tomcat
Next, give the tomcat group read access to the conf directory and all of its contents, and execute access to the directory itself:
# sudo chmod -R g+r conf
# sudo chmod g+x conf
Then make the tomcat user the owner of the webapps, work, temp, and logs directories:
sudo chown -R tomcat webapps/ work/ temp/ logs/
Install systemd unit file
To be able to run Tomcat as a service we will create systemd unit file
#nano /etc/systemd/system/tomcat.service
Contest of this file is as follows :
# 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/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=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
Now we need to reload daemon and start the service
# sudo systemctl daemon-reload
To start and check status of Tomcat run :
# sudo systemctl start tomcat
# sudo systemctl status tomcat
Enable Tomcat service by running :
sudo systemctl enable tomcat