Apache Tomcat and Systemctl

Apache Tomcat and Systemctl | We will learn about systemctl by using Apache Tomcat. We have previously seen httpd service and we know we can manage the service with systemctl command. But there are some services where we don’t get systemctl by default like Apache Tomcat, so you should know how to build it for them.

Tomcat is a service from Apache. Apache httpd and Apache Tomcat are two different services. Tomcat is an open-source Java servlet container. That means you can host Java-based web applications. by using Tomcat servers.

First, we are going to download and start Tomcat servers manually. We’ll see what are the problems with this. Then we will see systemd file which is the configuration file for systemctl command. We’ll see the requirements for it.

The users we need to create the folders and finally we will write our own systemd file for Tomcat servers. The main focus will be on systemctl and not Apache Tomcat.

We will use the centos for this.

$ cd /c/install/vagrant-vms/centos/
$ vagrant up
$ vagrant ssh
$ sudo -i
$ dnf install httpd -y
$ systemctl status httpd
$ ls /usr/lib/systemd/system/

In the /usr/lib/systemd/system/ directory you can find so many files, including httpd.service which is the configuration file for httpd. Let us see what it contains:-

$ cat /usr/lib/systemd/system/httpd.service

# See httpd.service(8) for more information on using the httpd service.

# Modifying this file in-place is not recommended, because changes
# will be overwritten during package upgrades.  To customize the
# behaviour, run "systemctl edit httpd" to create an override unit.

# For example, to pass additional options (such as -D definitions) to
# the httpd binary at startup, create an override unit (as is done by
# systemctl edit) and enter the following:

#       [Service]
#       Environment=OPTIONS=-DMY_DEFINE

[Unit]
Description=The Apache HTTP Server
Wants=httpd-init.service
After=network.target remote-fs.target nss-lookup.target httpd-init.service
Documentation=man:httpd.service(8)

[Service]
Type=notify
Environment=LANG=C

ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
# Send SIGWINCH for graceful stop
KillSignal=SIGWINCH
KillMode=mixed
PrivateTmp=true
OOMPolicy=continue

[Install]
WantedBy=multi-user.target

So in this file, there are 3 directives:-

  1. Unit
  2. Service
  3. Install

In the file we can find this line:- ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND. So this is the command to start httpd service. When we say systemctl start httpd it’s going to execute this command.

Now let us download the Tomcat server and create systemctl for it.

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.34/bin/apache-tomcat-10.1.34.tar.gz
ls
tar xzvf apache-tomcat-10.1.34.tar.gz
ls apache-tomcat-10.1.34

Apache Tomcat needs a dependency and that is Java. So we are going to first install Java for it.

dnf install java-17-openjdk -y
java -version
ls apache-tomcat-10.1.34/bin/

In the Apache Tomcat /bin/ directory we can find the startup.sh file which is a script a shell script to start the Tomcat service.

cd apache-tomcat-10.1.34/
bin/startup.sh

It says:- Tomcat started. We can verify that.

ps -ef | grep tomcat
hostname -i

There is a default page for the Tomcat server running on port 8080. Use the hostname -i to find the IP of the guest machine, which could be something like 192.168.56.99, visit this IP:port (192.168.56.99:8080) on the Brower of the host machine.

Whenever we reboot/restart the guest system, Tomcat won’t start automatically. We have to run this script startup.sh to start the Tomcat service each time. Similarly, to stop we have a separate script.

The other problem is in DevOps there are so many automation tools, and these tools will execute commands remotely to start a service. We don’t mention the command in these tools. We just started a particular service. So in the background, it will execute systemctl start Tomcat. And since it’s not available, we won’t be able to start the Tomcat service or manage it.

So as a DevOps engineer, it is definitely not recommended to run a service like this. Okay, let’s get the process ID of Tomcat and we have to stop it.

Find the process ID and kill them.

ps -ef | grep tomcat

ps -ef | grep tomcat | grep -v grep | awk '{print $2}' | xargs kill -9

Like any other service, Tomcat should be run as non-root user. Therefore we need a user that is going to run the Tomcat processes. And we also need a home directory where we can keep our Tomcat data. So let us create a user.

useradd --home-dir /opt/tomcat --shell /sbin/nologin tomcat
  • useradd: The command to add a new user.
  • --home-dir /opt/tomcat: Specifies /opt/tomcat as the home directory for the tomcat user.
  • --shell /sbin/nologin: Sets the login shell to /sbin/nologin, which means the user cannot log in interactively.
  • tomcat: The username for the new account.

Copy the files, and change the file permissions:-

cd ..
ls
cp -r apache-tomcat-10.1.34/* /opt/tomcat
chown -R tomcat:tomcat /opt/tomcat/
ls -l /opt/tomcat/

Let us create the systemctl file.

vim /etc/systemd/system/tomcat.service

Place the below content:-

[Unit]
Description=Tomcat
After=network.target

[Service]
Type = forking

User=tomcat
Group=tomcat

workingDirectory=/opt/tomcat

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

Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat

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

[Install]
wantedBy=multi-user.target

Here’s a brief recap of the purpose of each section:

[Unit] Section

  • Description=Tomcat: Brief description of the service.
  • After=network.target: Ensures the service starts after the network is up.

[Service] Section

  • Type=forking: Indicates that the service forks a child process.
  • User=tomcat: Runs the service as the tomcat user.
  • Group=tomcat: Runs the service as the tomcat group.
  • workingDirectory=/opt/tomcat: Sets the working directory to /opt/tomcat.
  • Environment Variables: Sets JAVA_HOME, CATALINA_HOME, and CATALINA_BASE.
  • ExecStart=/opt/tomcat/bin/startup.sh: Command to start the Tomcat server.
  • ExecStop=/opt/tomcat/bin/shutdown.sh: Command to stop the Tomcat server.

[Install] Section

  • wantedBy=multi-user.target: Starts the service in multi-user mode.

Whenever we add or make any kind of change in the systemd file, we have to run this command:-

systemctl daemon-reload

Just make sure no Tomcat service is already running. If it’s running, just kill those processes.

ps -ef | grep tomcat

ps -ef | grep tomcat | grep -v grep | awk '{print $2}' | xargs kill -9

Now start the tomcat server:-

systemctl start tomcat
systemctl status tomcat
systemctl enable tomcat

Others:-

  • Infrastructure as code (IaC) is the process of managing and provisioning infrastructure (networks, virtual machines, load balancers, and connection topology) through CODE/Config Files. E:g:- Vagrant for local, Terraform for Cloud, Ansible for Servers, Cloudformation for AWS, etc.
  • Provisioning is the process of configuring and deploying an information technology (IT) system resource either locally or in the cloud. In enterprise computing, the term is often associated with virtual machines (VMs) and cloud resource instances.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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