Skip to main content

Creating a Linux Systemd Service to Deploy a Django Website with Nginx

Published on

  • informatic
  • programming tools
  • Linux
  • Django
  • Nginx

Table of Contents

  1. Abstract
  2. Introduction
  3. System Architecture
  4. Environment Setup
  5. Creating the Django Systemd Service
  6. Nginx Configuration
  7. Running and Enabling the Service
  8. Security and Permissions
  9. Conclusion
  10. References

 

1. Abstract

This article provides a complete guide to deploying a Django web application behind an Nginx server on a Linux system, using systemd to manage the Django application as a persistent background service. This approach ensures automatic startup, improved process supervision, and better logging integration, especially for production-ready environments.

2. Introduction

Django is a high-level Python web framework widely used for rapid web development. For production deployment, it is common to use a reverse proxy such as Nginx in front of a WSGI server like Gunicorn. By creating a systemd service for Django, the application becomes easier to manage, more robust, and can automatically start on system boot.

3. System Architecture

Client ⟶ Nginx (Reverse Proxy) ⟶ Gunicorn (WSGI Server) ⟶ Django Application

  • OS: Linux (Debian, Ubuntu, etc.)
  • Web Server: Nginx
  • App Server: Gunicorn
  • App: Django

4. Environment Setup

1. Install Required Packages:

Install packages
sudo apt update
sudo apt install python3 python3-pip python3-venv nginx

2. Set Up the Django Project:

Set up Django project
mkdir ~/myproject
cd ~/myproject
python3 -m venv venv
source venv/bin/activate
pip install django gunicorn
django-admin startproject myproject .

3. Test Gunicorn Manually:

Test Gunicorn
gunicorn --bind 127.0.0.1:8000 myproject.wsgi:application

5. Creating the Django Systemd Service

1. Create a Gunicorn Service File:

Create systemd service file
sudo nano /etc/systemd/system/myproject.service

2. Paste and Edit the Following Configuration:

/etc/systemd/system/myproject.service
[Unit]
Description=Gunicorn daemon for Django project
After=network.target

[Service]
User=yourusername
Group=www-data
WorkingDirectory=/home/yourusername/myproject
ExecStart=/home/yourusername/myproject/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          myproject.wsgi:application

[Install]
WantedBy=multi-user.target

6. Nginx Configuration

1. Create a New Nginx Configuration:

Create Nginx site config
sudo nano /etc/nginx/sites-available/myproject

2. Add the Following Server Block:

/etc/nginx/sites-available/myproject
server {
    listen 80;
    server_name your_domain.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/yourusername/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

3. Enable the Site and Restart Nginx:

Enable site and restart Nginx
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx

7. Running and Enabling the Service

Start and enable the systemd service so that it automatically restarts on boot:

Start and enable service
sudo systemctl start myproject
sudo systemctl enable myproject
sudo systemctl status myproject

8. Security and Permissions

  • Ensure file permissions for /run/gunicorn.sock allow Nginx to access it.
  • Consider using ufw or firewalld to restrict ports.
  • Use Let's Encrypt for HTTPS via Certbot:
Setup SSL with Certbot
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com

9. Conclusion

Deploying Django with systemd, Gunicorn, and Nginx provides a robust, production-grade deployment pattern. This architecture ensures:

  • A robust and manageable service lifecycle
  • High-performance request handling and reverse proxying via Nginx
  • A secure and scalable production environment

Such practices are essential for professional deployment of web services in modern Linux-based hosting environments.

10. References

Comments 0

No comments yet.

Leave a comment