Run Tomcat Server as a Service in CentOS 7

May 06, 2018 | 356 views | Comments

To make it more easier to start and stop an Apache Tomcat Server, run it as a service which can be performed by a "sudoer". To do this, a systemd service file must be created and installed. This is a follow-up tutorial of Install JAVA 1.8 and Tomcat Server 9 in CentOS 7.

 

Step 1. Create a "service" file inside the "system" folder.

Go inside the "systemd/system" folder.

$ cd /etc/systemd/system

Create the file using vi, you can use other linux-based text editor you like.

$ sudo vi tomcat.service

Copy/paste the following code into the tomcat.service file.

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre

Environment=CATALINA_PID=/opt/tomcat/apache-tomcat-9.0.7/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat/apache-tomcat-9.0.7
Environment=CATALINA_BASE=/opt/tomcat/apache-tomcat-9.0.7

ExecStart=/opt/tomcat/apache-tomcat-9.0.7/bin/startup.sh
ExecStop=/opt/tomcat/apache-tomcat-9.0.7/bin/shutdown.sh

User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target

 

Step 2. Restart the "systemctl" to have the new service take effect.

$ sudo systemctl daemon-reload

 

That's all!

Tomcat can now run as a service.

 

Start tomcat. Requires a "sudoer".

$ sudo service tomcat start

 

Stop tomcat. Requires a "sudoer".

$ sudo service tomcat stop

 

Note.

If you encounter a "Permission denied" issue, repeat "Step 7" of the tutorial Install JAVA 1.8 and Tomcat Server 9 in CentOS 7.

Step 7. Change group and ownership of the utarred folder "apache-tomcat-9.0.7" to both user tomcat, and group tomcat.

$ sudo chown tomcat:tomcat -R /opt/tomcat/apache-tomcat-9.0.7

 

Related Tutorials


Must Read