Install Apache Tomcat 8.5 on CentOS 7

Scenario: Install Apache Tomcat 8.5 on CentOS/RHEL 7

STEPS:
1) This scenario using Oracle Java. Login as root user then install Oracle Java.

[root@centos7ht ~]# cd /tmp/
[root@centos7ht tmp]# curl -v -j -k -L -H "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.rpm > /opt/jdk-8u181-linux-x64.rpm
[root@centos7ht tmp]# rpm -ivh jdk-8u181-linux-x64.rpm
warning: jdk-8u181-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing... ################################# [100%]
Updating / installing...
 1:jdk1.8-2000:1.8.0_181-fcs ################################# [100%]
Unpacking JAR files...
 tools.jar...
 plugin.jar...
 javaws.jar...
 deploy.jar...
 rt.jar...
 jsse.jar...
 charsets.jar...
 localedata.jar...
[root@centos7ht tmp]# alternatives --config java

There is 1 program that provides 'java'.

 Selection Command
-----------------------------------------------
*+ 1 /usr/java/jdk1.8.0_181-amd64/jre/bin/java

Enter to keep the current selection[+], or type selection number: 1
[root@centos7ht tmp]# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
[root@centos7ht tmp]#

2) Create tomcat service account

# groupadd tomcat
# useradd -g tomcat -d /opt/tomcat -s /bin/nologin tomcat

3) Download Apache Tomcat 8.5.33

# cd /tmp/
# curl -LO http://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.33/bin/apache-tomcat-8.5.33.tar.gz

4) Extract and set chown to /opt/tomcat8.5

# tar -zxvf apache-tomcat-8.5.33.tar.gz -C /opt/
# mv apache-tomcat-8.5.33/ tomcat8.5
# chown -R tomcat:tomcat /opt/tomcat8.5/

5) Controlling Apache Tomcat
– Manual
Apache Tomcat can be started and stopped by the script which comes with the package.

Start the Apache Tomcat. You will get the following output.

[root@horse /]# cd /opt/tomcat8.5/bin/
[root@horse bin]# ./startup.sh
Using CATALINA_BASE: /opt/tomcat8.5
Using CATALINA_HOME: /opt/tomcat8.5
Using CATALINA_TMPDIR: /opt/tomcat8.5/temp
Using JRE_HOME: /
Using CLASSPATH: /opt/tomcat8.5/bin/bootstrap.jar:/opt/tomcat8.5/bin/tomcat-juli.jar
Tomcat started.
[root@horse bin]#

Stop the Apache Tomcat.

[root@horse /]# cd /opt/tomcat8.5/bin/
[root@horse bin]# ./shutdown.sh
Using CATALINA_BASE: /opt/tomcat8.5
Using CATALINA_HOME: /opt/tomcat8.5
Using CATALINA_TMPDIR: /opt/tomcat8.5/temp
Using JRE_HOME: /
Using CLASSPATH: /opt/tomcat8.5/bin/bootstrap.jar:/opt/tomcat8.5/bin/tomcat-juli.jar
[root@horse bin]#

– Setup Tomcat service Systemd
We can also configure systemd to start the Tomcat service. Skip the below step in case you do not want to use systemd for managing Tomcat service.

Tomcat’s systemd service file requires java location. So, run the following command to list the java versions available on your system.

[root@horse bin]# alternatives --list | grep ^java
java manual /usr/java/jdk1.8.0_181-amd64/jre/bin/java
javac auto /usr/java/jdk1.8.0_181-amd64/bin/javac
[root@horse bin]#

Create a tomcat systemd service file like below,
# vi /etc/systemd/system/tomcat.service

[Unit]
Description=Apache Tomcat Web Application Container
Wants=network.target
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/java/jdk1.8.0_181-amd64/jre/
Environment=CATALINA_PID=/opt/tomcat8.5/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat8.5
Environment='CATALINA_OPTS=-Xms512M -Xmx1G -Djava.net.preferIPv4Stack=true'
Environment='JAVA_OPTS=-Djava.awt.headless=true'

ExecStart=/opt/tomcat8.5/bin/startup.sh
ExecStop=/opt/tomcat8.5/bin/shutdown.sh
SuccessExitStatus=143

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Reload systemd daemon.
# systemctl daemon-reload

To start the Tomcat service; run:
# systemctl start tomcat

Check the status of Tomcat, run:
# systemctl status tomcat

Enable the auto start of Tomcat service on system start, run:
# systemctl enable tomcat

To stop the Tomcat service; run:
# systemctl stop tomcat

To disable the auto start of Tomcat service on system start, run:
# systemctl disable tomcat

Setup Firewall to allow port 8080
# firewall-cmd --permanent --add-port=8080/tcp
# firewall-cmd --reload

Configure admin password to login Tomcat manager.
# vi /opt/tomcat/conf/tomcat-users.xml

<role rolename="admin-gui,manager-gui"/>
<user username="admin" password="tomcat" roles="manager-gui,admin-gui"/>

Configure to allowed login to Tomcat manager from remote hosts.
For security reason, Web and Host Manager is accessible only from localhost, i.e., from the server itself.
If you want to access Web and Host manager from remote systems, then you need to add your source network in allow list. To do that, edit the below two files.

# vi /opt/tomcat8.5/webapps/manager/META-INF/context.xml
# vi /opt/tomcat8.5/webapps/host-manager/META-INF/context.xml

Update the below line on both files with source IP from which you’re accessing the Web and Host Manager. .* will allow everyone to have access to Web and Host manager.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|.*" />

OR

You can also allow part of your network only. For example: To allow 10.20.0.0/24 network only, you can use the below values.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|10.20.*" />

Restart the Tomcat service.
# systemctl restart tomcat

Access Tomcat

http://ip.add.re.ss:8080

Reference:
https://www.itzgeek.com/how-tos/linux/centos-how-tos/install-apache-tomcat-8-on-centos-7-rhel-7.html


Leave a Reply

Your email address will not be published. Required fields are marked *