Web development and Tech news Blog site. WEBISFREE.com

HOME > linux

Setting Up a New Service in Linux's systemctl

Last Modified : 15 Oct, 2023 / Created : 15 Oct, 2023
156
View Count



In Linux, you can add a new service to run an app on a web server. For instance, the code below utilizes uwsgi to operate a Python app on the server. Let’s explore how to set up a new service (Service) like this and a few things to keep in mind.



Registering and Configuring a New Service
First, let’s examine the code. It's the /etc/systemd/system/myapp.service file.

@ myapp.service
[Unit]
Description=uWSGI instance to serve my app
After=network.target

[Service]
User=root
Group=www-data
Environment="PATH=/home/myapp"
WorkingDirectory=/home/myapp
ExecStart=/usr/local/bin/uwsgi --ini /home/myapp/app.ini

[Install]
WantedBy=multi-user.target

The above code is a typical new service configuration file. Here, the code in the [Service] block must be set up correctly. Especially, if the path is not correct, the service may not operate properly.

Especially, issues often arise where the file cannot be found. This is because the area (space) created and run by the service file is always different from the environment executing the command.

If we look again at the [Service] block, it is as follows:
[Service]
...
Environment="PATH=/home/myapp"
WorkingDirectory=/home/myapp
ExecStart=/usr/local/bin/uwsgi --ini /home/myapp/app.ini

Let’s first examine the WorkingDirectory and ExecStart settings here.

- Environment // Set up the environment variable to use
- WorkingDirectory // The location of the area where work is executed after the app is launched, path
- ExecStart // The command to execute when the service is launched

Let’s delve into each.

Environment allows you to declare environment variables within the Service file, providing usability throughout the service environment. In other words, the code below declares a variable named PATH, which can be used anywhere within the service environment.
Environment="PATH=/home/myapp"
ExecStart=/usr/local/bin/uwsgi --ini $PATH/app.ini

When using, it is possible to call the variable using $PATH, ${PATH}, etc.

Next, WorkingDirectory is the location where work will be executed after launching. That is, it's different from the path info needed to launch the app, and when the app is operational, it sets the value to configure the workspace for loading or reading files from that location.

Lastly, ExecStart is the command that will be run when the service is launched. When directly using installed commands like uwsgi or python, you need the absolute path where they are installed. For this reason, rather than using such execution commands directly, it is common to use a script file like myapp.sh and call it.


So far, we have briefly examined values to declare when setting up a Service.
Perhaps you're looking for the following text as well?

    Previous

    Learn about the history command in Linux

    Previous

    [Linux] Checking storage capacity and other information using df