In the beginning was Init. Then came Systemd. If you’re wondering how to switch your Django apps from Init to Systemd, here we go!
I build web apps, some of which might make your life easier. For instance, Custom MaterializeCSS Download allows you to customize MaterializeCSS in the browser before download.
Previously
With Django, you probably have something like this in your /etc/init/gunicorn.py
file :
"""gunicorn WSGI server configuration.""" from multiprocessing import cpu_count from os import environ def max_workers(): return cpu_count() * 2 + 1 max_requests = 1000 worker_class = 'gevent' workers = max_workers()
Then you followed that with independent configuration files for each of your Django project, like so:
description "Gunicorn daemon for Django project" start on (local-filesystems and net-device-up IFACE=eth0) stop on runlevel [!12345] # If the process quits unexpectadly trigger a respawn respawn setuid django setgid django chdir /home/django_project exec gunicorn \ --name=ProjectName \ --pythonpath=ProjectName \ --bind=0.0.0.0:9003 \ --config /etc/gunicorn.d/gunicorn.py \ Placeholder.wsgi:application
Then you went ahead to map a proxy pass to the upstream server of the above gunicorn.conf of the respective project.
For the time, the process appears simple, but with Systemd, it becomes even simpler. You only need a service file for the Django project, then simply point to the sock file created by the Systemd process from your Nginx server block.
Now with SystemD
Your Nginx file might look like this (snippet taken from Django Hold It project):
location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://unix:/home/django/hold/hold.sock; }
A related Systemd service file responsible for creating the sock file will be like this:
[Unit] Description=gunicorn daemon After=network.target [Service] User=user Group=www-data WorkingDirectory=/home/user/hold ExecStart=/home/user/.virtualenvs/hold/bin/gunicorn --workers 3 --bind unix:/home/user/hold/hold.sock Placeholder.w$ [Install] WantedBy=multi-user.target
Bring everything up nicely
$ sudo systemctl start gunicorn $ sudo systemctl enable gunicorn $ sudo systemctl status myproject.service $ sudo systemctl daemon-reload // use this to reload daemon after changes
Your server should be up and running like my LogECG and Django Hold It.
Thanks and hope to see you in the next one.