Skip to content
DBDeependra Bhatta~/notes
Linux#technology · #linux · #ubuntu · #security · #cybersecurity

To create a service file for Tomcat Server

/etc/systemd/system/multi-user.target.wants/apache2.service First we need to create a file inside /etc/systemd/system. For now i have created a file with name tomcat1.service. After that write a…

· updated · 2 min read

To create a service file for Tomcat Server screenshot 1

  • systemctl works based on the configuration file. For example.

    • /etc/systemd/system/multi-user.target.wants/apache2.service
  • First we need to create a file inside /etc/systemd/system. For now i have created a file with name tomcat1.service.

  • After that write a service file to start and stop the tomcat.

[Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking Environment=CATALINA_PID=/opt/apache-tomcat2/temp/tomcat.pid Environment=CATALINA_HOME=/opt/apache-tomcat2 Environment=CATALINA_BASE=/opt/apache-tomcat2 ExecStart=/opt/apache-tomcat2/bin/startup.sh ExecStop=/opt/apache-tomcat2/bin/shutdown.sh User=tomcat Group=tomcat RestartSec=10 Restart=always [Install] WantedBy=multi-user.target

Description

Description of service file: Core Service Configuration Type=forking : Tomcat launches as a background daemon (parent process exits after starting Java). Environment=CATALINA_PID=... : Specifies where Tomcat writes its Process ID (PID) so systemd can track it. Environment=CATALINA_HOME=...: Sets Tomcat’s installation directory (where binaries like startup.sh live). Environment=CATALINA_BASE=...: Defines where configs/logs/webapps are stored (usually same as CATALINA_HOME). Startup/Shutdown ExecStart=.../startup.sh: Command to launch Tomcat (internally calls catalina.sh start). ExecStop=.../shutdown.sh :Gracefully stops Tomcat (sends SIGTERM to the Java process). Security User=tomcat: Runs Tomcat as a non-root user (improves security). Group=tomcat: Assigns group ownership to the tomcat group. Reliability RestartSec=10: Waits 10 seconds before restarting after a crash (avoids aggressive respawns). Restart=always: Auto-restarts Tomcat if it crashes or is killed (ensures uptime). Boot Behavior ([Install]) WantedBy=multi-user.target: Starts Tomcat automatically when the server boots into multi-user mode.
  • In this service file the things that we need to notice are
    • check the path for CATALINA_PID,CATILINA_HOMW,CATILINA_BASE. In this example i have installed my tomcat inside /opt/apache-tomcat1. That’s why i am using this path.
    • And also make sure your ExecStart and ExecStop path are correct.
    • And also make sure temp file is located in that path.

To create a service file for Tomcat Server screenshot 2

  • After setting up all this now we need to run few commands to start this service.

    • Create the tomcat user: **useradd -r -m -d /opt/apache-tomcat1 -s /bin/false tomcat**
    • Set ownership: chown -R tomcat:tomcat /opt/apache-tomcat1
  • After creating the user and changing ownership of the file. Now we need to restart the daemon and enable the service.

    • sudo systemctl daemon-reload :To reload the daemon service
    • sudo systemctl restart tomcat1: To restart the tomcat service
    • sudo systemctl status tomcat1: To see the status of the service
  • Apache web server

    The Apache HTTP server, commonly known as Apache is a free and open-source web server, that delivers web content through the internet. As open-source software, its original source code is freely…

  • Shell Scripting

    It is a way to automate tasks by writing a series of commands in a text file called script that Linux shell can execute. It’s like creating a ‘to do list’ for your computer to follow step by step…

  • Linux commands[2]

    Linux Links Soft links Aka Symbolic link It is a pointer to the original file Just like a shortcut in windows and smaller file size Different inode number If we delete the original file, the soft…