这是indexloc提供的服务,不要输入任何密码
Skip to content

Vgarcan/create-gunicon-service-for-django

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Django + Gunicorn Systemd Service Installer

This script automates the setup of a systemd service for a Django project running on Gunicorn. It's ideal for deploying Django applications in a production Linux environment with minimal manual setup.


Features

  • Scans user home directories for Django projects (manage.py)
  • Detects virtual environments and WSGI modules automatically
  • Prompts for safe defaults with validation
  • Installs Gunicorn if not already installed
  • Creates a systemd service file under /etc/systemd/system/
  • Enables and starts the service with proper permissions
  • Colour-coded prompts and messages for clarity

Requirements

  • Linux-based system (tested on Ubuntu/Debian)

  • Python 3.6+

  • Root privileges (to write to /etc/systemd/system/)

  • Django project with:

    • A manage.py file
    • A valid wsgi.py file
    • A working virtual environment

What It Does

  1. Asks you to choose a system user and group to run the service
  2. Detects Django projects in /home/<user>/
  3. Detects or prompts for the virtual environment path
  4. Detects or prompts for the WSGI module
  5. Asks for IP and port to bind Gunicorn
  6. Installs Gunicorn inside the virtual environment if needed
  7. Writes a .service file for systemd
  8. Reloads systemd, enables, and starts the service

Example Usage

sudo ./create_gunicorn_service.py

⚠️ Must be run as root (or using sudo) to write systemd service files.


Example Service File Output

[Unit]
Description=Gunicorn daemon for myapp
After=network.target

[Service]
User=myuser
Group=mygroup
WorkingDirectory=/home/myuser/myproject
ExecStart=/home/myuser/myproject/venv/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 myproject.wsgi:application
Restart=on-failure
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Common Commands

sudo systemctl restart <service_name>     # Restart service
sudo systemctl status <service_name>      # Check service status
journalctl -u <service_name> -f           # Follow logs

Notes

  • For public access, bind to 0.0.0.0, but it's recommended to use 127.0.0.1 with a reverse proxy like Nginx.
  • Avoid running as root. Choose a dedicated non-root user.
  • You can re-run the script to overwrite or update an existing service.

Security Recommendation

Always keep your virtual environment isolated and make sure only trusted users have access to modify Django code or systemd service files.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages