Installing Apache Tomcat as a service on RedHat/CentOS 7

Download Apache tomcat 9.0

wget https://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.19/bin/apache-tomcat-9.0.19.tar.gz

Extract the Tomcat archive and move it to your desired (/opt/tomcat) directory.
tar -zxvf apache-tomcat-*.tar.gz
mv apache-tomcat-*/* /opt/tomcat/

Create tomcat user:
groupadd tomcat
useradd -g tomcat -d /opt/tomcat -s /bin/nologin tomcat

Change the ownership of the directory so that tomcat user can write files in it.
chown -R tomcat:tomcat /opt/tomcat/

TO Start Apache Tomcat Manually use below script:

cd /opt/tomcat/bin/
sh startup.sh

Stop the Apache Tomcat.
cd /opt/tomcat/bin/
sh shutdown.sh

Now If you want to to use systemd for managing Tomcat service.

All the following commands have to be executed with super-user privileges.
Procedure

1. Create the service file with the following command:
touch /etc/systemd/system/tomcat.service

2. Assign the relevant rights to the file you created:
chmod 664 /etc/systemd/system/tomcat.service

NOTE for JRE Path
[aadmin@host1 ~]$ alternatives --list  | grep ^java
java    auto    /usr/java/jdk1.8.0_201-amd64/jre/bin/java
javac   auto    /usr/java/jdk1.8.0_201-amd64/bin/javac

JRE PATH:
/usr/java/jdk1.8.0_201-amd64/jre/

3. Paste the following content in the file while adapting it to your configuration:

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

[Service]
Type=forking

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

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

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

[Install]
WantedBy=multi-user.target

4. Reload the service daemon:
systemctl daemon-reload
Enable the auto start of Tomcat service on system start, run:
systemctl enable tomcat

5. Start the service:
systemctl start tomcat.service

[aadmin@host1 ~]$ systemctl status tomcat.service
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-03-26 06:30:04 UTC; 1min 11s ago
  Process: 29626 ExecStop=/opt/tomcat/bin/shutdown.sh (code=exited, status=1/FAILURE)
  Process: 29700 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 29711 (catalina.sh)
   CGroup: /system.slice/tomcat.service
           ├─29711 /bin/sh /opt/tomcat/bin/catalina.sh start
           └─29712 /usr/java/jdk1.8.0_201-amd64/jre//bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager...

Verify Apache Tomcat

By default, Tomcat runs on port no 8080. Use netstat command to check whether the service is listening on port 8080 or not.

netstat -antup | grep 8080

tcp6       0      0 :::8080                 :::*                    LISTEN      2428/java

You may need 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

Now open web browser and point to your publicip:8080, you should get the default tomcat page like below:


Web Manager: – Login Required. Username: admin, Password: tomcat


To install Apache Web Server on RedHat/CentOS 7 go here.

Also to install word press go here.

Post a Comment

Thanks for your comment !
I will review your this and will respond you as soon as possible.