Sunday, January 7, 2018

Installation of Tomcat9 in Cent OS 7

Update yum with epel:
$ sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Install java8:
$ sudo yum install java-1.8.0-openjdk.x86_64

Check if java was successfully installed.
$ java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

Create a user and a group four tomcat.
sudo groupadd tomcat
sudo mkdir /opt/tomcat
sudo useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat

Install tomcat9.
cd ~
wget http://www.us.apache.org/dist/tomcat/tomcat-9/v9.0.2/bin/apache-tomcat-9.0.2.tar.gz
sudo tar -zxvf apache-tomcat-9.0.2.tar.gz -C /opt/tomcat --strip-components=1

Change the permission of some files. (Note: these commands will change permissions of bin, lib etc, so some softwares might not work after doing these commands.)
cd /opt/tomcat
sudo chgrp -R tomcat conf
sudo chmod g+rwx conf
sudo chmod g+r conf/*
sudo chown -R tomcat logs/ temp/ webapps/ work/

sudo chgrp -R tomcat bin
sudo chgrp -R tomcat lib
sudo chmod g+rwx bin
sudo chmod g+r /bin/*

Create a Systemd unit file of tomcat:
sudo vi /etc/systemd/system/tomcat.service

Write like the following in the file (just copy and paste the following):
[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

[Install]
WantedBy=multi-user.target

And save the file by hitting ":wq".

Install haveged for secutrity.
sudo yum install haveged
sudo systemctl start haveged.service
sudo systemctl enable haveged.service

Start tomcat:
sudo systemctl start tomcat.service
sudo systemctl enable tomcat.service

Now you can see tomcat's top page: http://(your url):8080/
If you can't, change the setting of Firewall (pare-feu):
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

We will create an admin user. Open "tomcat-users.xml":
sudo vi /opt/tomcat/conf/tomcat-users.xml

Add the following between "</tomcat-users ...>" and "</tomcat-users>" in the file.
<user username="admin" password="admin" roles="manager-gui,admin-gui"/>

Restart tomcat.
sudo systemctl restart tomcat.service

Now you can see the top page from a browser: http://(your url):8080/