systemctl - Starting a Django Application using systemctl

Updated: June 18th, 2023, 02:18:34 IST
Published: June 17th, 2023
systemctl  - Starting a Django Application using systemctl
Title: systemctl - Starting a Django Application using systemctl

We will learn how to launch a Django application using systemctl under systemd in this article. systemd is a Linux system and service manager that allows you to manage and control services on your server. You can quickly start, pause, and control your Django application as a service by setting systemd.

Prerequisites

Before we begin, make sure you have the following:

  • A Django application installed on your server.
  • Basic knowledge of Linux and command-line interface.
  • Root access or sudo privileges on your server.

Create a Systemd Service Unit File:

1. Create Systemd Service file

  • Navigate to the /etc/systemd/system directory in a terminal.
  • Create a new .service file for your Django application. For example, myapp.service.
  • Open the file with a text editor and add the following content:
[Unit]
Description=My Django Application
After=network.target

[Service]
User=your_username
Group=your_group
WorkingDirectory=/path/to/your/django/projectapp
ExecStart=/path/to/your/venv/bin/gunicorn your_project.wsgi:application --bind 0.0.0.0:8000

[Install]
WantedBy=multi-user.target

Make sure to replace your_username, your_group, /path/to/your/django/projectapp, and /path/to/your/venv with the appropriate values for your setup.

2. Save the file and exit the text editor.

3. Enable and Start the Service:

  • Run the following command to enable the service and start it:
sudo systemctl enable myapp
sudo systemctl start myapp

4. Verify the Status:

To check the status of your Django application service, use the following command:

sudo systemctl status myapp

5. Access Your Django Application:

If the service starts successfully, you should be able to access your Django application by opening a web browser and entering the server's IP address or domain name, followed by the port specified in the ExecStart command (e.g., http://your_server_ip:8000).

 

 

Simple systemd unit for running a django app

Command:

# start / stop / restart / status
systemctl start myapp
systemctl stop myapp
systemctl restart myapp
systemctl status myapp

# logs use journalctl: 

# tail the logs for unit `django`
journalctl -f -u django

django.service

[Unit]
Description=Unit for starting a basic Django app 

[Service]
Restart=on-failure
WorkingDirectory=/path/to/your/django/projectapp
ExecStart=/path/to/your/venv/env/bin/gunicorn projectapp.wsgi -b 0.0.0.0:8000

[Install]
WantedBy=multi-user.target

 

To run applications you have to use systemd suite. Systemd configuration files are stored at:

/etc/systemd/system/

In general, I'd note where the systemd files are placed for convenience:

vim /etc/systemd/system/django.service

 

Conclusion

In this tutorial, we've learned how to start a Django application using systemctl under systemd. By creating a service unit file and configuring it with the appropriate settings, you can easily manage and control your Django application as a service on your server. This allows your application to start automatically on system boot and ensures its availability even when you're not logged in.

Remember to customize the service unit file based on your specific Django application's configuration and requirements. With systemctl, you have a powerful tool at your disposal for managing your Django application and other services on your server.