How to install tomcat as service in linux


To install Apache Tomcat as a service in Linux, you can follow these general steps:

Create a Systemd Service Unit:

  • Open a terminal.
  • Navigate to the systemd system service directory:
            cd /etc/systemd/system/
  • Create a new service unit file with a .service extension. For example, create a file called tomcat.service:
             sudo nano tomcat.service
  • In the editor, add the following content to the file:
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
User=tomcat
Group=tomcat
Type=forking
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • Save and exit the editor.

Adjust Permissions:

Set the appropriate ownership and permissions on the Tomcat installation directory:

sudo chown -R tomcat:tomcat /opt/tomcat
sudo chmod +x /opt/tomcat/bin/*.sh

Reload Systemd and Start Tomcat Service:

Reload the systemd daemon to load the new service unit:

sudo systemctl daemon-reload

Start the Tomcat service:

sudo systemctl start tomcat

Enable Autostart:

If you want Tomcat to start automatically on system boot, enable the service:

sudo systemctl enable tomcat

Now, Tomcat will run as a service managed by Systemd in your Linux system. You can use the systemctl command to start, stop, restart, and check the status of the Tomcat service. For example:

sudo systemctl start tomcat   # Start the Tomcat service
sudo systemctl stop tomcat    # Stop the Tomcat service
sudo systemctl restart tomcat # Restart the Tomcat service
sudo systemctl status tomcat  # Check the status of the Tomcat service

Please note that the above steps are a general guideline, and the specifics may vary depending on your Linux distribution and version of Tomcat. It’s recommended to refer to the official Apache Tomcat documentation and any specific documentation provided by your Linux distribution for detailed instructions and considerations.