Deploying Standalone app as a Service

It is possible to deploy Linux Standalone apps as a Linux Service. This ensures the web application server starts with the OS and runs continuously while the server is up.

This example uses the FishFacts demo and deploys it as a systemd service.

Prerequisite: open a terminal on your Linux server. The example assumes you are root; if not, prepend sudo to commands.

1

1. Create the service file

Change to the systemd system folder and create a new service file named <your_service_name>.service (example uses fishfacts.service):

cd /etc/systemd/system
nano fishfacts.service

Use an editor (here nano) to create the file.

2

2. Example service file contents

Paste the following into fishfacts.service:

[Unit]

Description=FishFacts

After=network.target

[Service]

Type=simple

Restart=always

WorkingDirectory=/var/www/webapp

ExecStart=/var/www/webapp/fishfacts

ExecStop=/usr/bin/curl --max-time 10 http://127.0.0.1:8077/?action=terminate

ExecStop=/bin/sleep 5

[Install]

WantedBy=multi-user.target

Notes (explanatory lines from the original example):

  • Description=FishFacts β€” Describes the name of your service

  • After=network.target β€” Indicates the service should run after the network subsystem is ready

  • Type=simple β€” It is a simple service

  • Restart=always β€” Service will always restart after a failure or unwanted termination

  • WorkingDirectory=/var/www/webapp β€” Working directory of your service

  • ExecStart=/var/www/webapp/fishfacts β€” Path to your service executable binary. In this example the demo binary named fishfacts was deployed to /var/www/webapp. Linux is case-sensitive.

  • ExecStop=/usr/bin/curl --max-time 10 http://127.0.0.1:8077/?action=terminate β€” Ensures the web app server will terminate correctly. 8077 is the port your application runs on; change if your app listens on a different port.

  • ExecStop=/bin/sleep 5 β€” Waits 5 seconds after stop

3

3. Save and exit editor

In nano: save with CTRL+S, exit with CTRL+X.

4

4. Reload systemd so it recognizes the new service

systemctl daemon-reload
5

5. Enable the service at boot

systemctl enable fishfacts.service
6

6. Start the service

systemctl start fishfacts
7

7. Check the service status

service fishfacts status

If everything is OK you should get a message similar to:

● fishfacts.service - FishFacts

     Loaded: loaded (/etc/systemd/system/fishfacts.service; enabled; vendor preset: enabled)

     Active: active (running) since Mon 2020-11-0218:52:03 CET;1h 19min ago

Main PID:512(fishfacts)

      Tasks:7(limit:4657)

     Memory:32.5M

     CGroup:/system.slice/fishfacts.service

└─512/var/www/webapp/fishfacts

Nov 0218:52:03 vm.server.net systemd[1]: Started FishFacts.
8

8. Test the web app

Open in a web browser:

http://server-address:8077

9

9. Stop the service

systemctl stop fishfacts
10

10. Disable and remove the service

systemctl disable fishfacts.service
rm /etc/systemd/system/fishfacts.service
systemctl daemon-reload