UbuViz
    Home About Services Contact Blog

Deploying a Django based app on Systemd

Written by Allan on June 9, 2021 in Best Practices.

Last update on June 25, 2021.


How to configure Celery to run as systemd service with a Django application served by Gunicorn?

Deploy a Django app under systemd

 

This blog is the first part of a series of articles intended for interns inside UbuViz but also anyone interested in programming as we do it at UbuVuzi.

First of all, you need to know that I’m Allan Stockman Rugano. I have been building Open Source softwares since 2013 in Bujumbura (Burundi) and I picked, back then, Linux as my main operating system for daily usage and; since then, I haven’t looked back.  Linux helped me, as an African dude, to understand the quirks and complexity of building something so efficient as an OS. And more importantly, its open sourced model allowed me to realize that there is nothing so special about building an OS. Even you and I can build this stuff. 

That's why I made it my goal to share knowledge with my peers and today let us discuss how we build some of the softwares at UbuViz.

Backend logic

We normally deploy many servers on DigitalOcean running Linux based OS. And as I mentioned back in 2014, we have been influenced by the stack of technologies that were back then behind a platform I was running for UNICEF in Burundi named U-report:  

  • Django, Gunicorn for web processes, 

  • PostgreSQL for the DB, 

  • Redis for caching, 

  • Celery as workers for asynchronous tasks … 

  • and a few other services such as CloudFlare for network traffic, Let's Encrypt for SSL, fail2ban, Wireguard and StrongSwan for VPN …

The beauty of this is … all of those technologies help you launch your business and they are free to use and you can actually look into how they were made and adapt them to your needs, without paying any fee. 

We mainly like to deploy our Django apps using a better version of the famous tutorial on DigitalOcean on How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 20.04 . Very handy and you should follow how they deploy a web app using that tutorial as reference. But we like to make some modifications to the way Gunicorn and Redis are handled by Systemd. Previously we used Supervisord but this has been lately less used in favor of Systemd, because mainly, the new way allows us to centralize all the logs under a common system of the OS.

Gunicorn process under systemd 

We usually create an file at /home/sammy/myprojectdir/myprojectenv/bin/gunicorn_execute  that the user www-data can execute and put the following lines:

 

#!/bin/bash

NAME="myproject"                                  # Name of the application DJANGODIR=/home/sammy/myprojectdir                    # Django project directory SOCKFILE=/home/sammy/myprojectdir/myprojectenv/myproject.sock       # we will communicate using this unix socket USER=www-data                                       # the user to run as GROUP=www-data                                      # the group to run as

NUM_WORKERS=3                                       # how many worker processes should Gunicorn spawn

DJANGO_SETTINGS_MODULE=myproject.settings               # which settings file should Django use

DJANGO_WSGI_MODULE=myproject.wsgi                       # WSGI module name

VIRTUALENV=/home/sammy/myprojectdir/myprojectenv

echo "Starting $NAME as `whoami`"

# Activate the virtual environment

cd $DJANGODIR

source $VIRTUALENV/bin/activate

export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE

export PYTHONPATH=$DJANGODIR:$PYTHONPATH

 

# Create the run directory if it doesn't exist

RUNDIR=$(dirname $SOCKFILE)

test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn

# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)

exec $VIRTUALENV/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \

  --name $NAME \

  --workers $NUM_WORKERS \

  --user=$USER --group=$GROUP \

  --bind=unix:$SOCKFILE \

  --log-level=debug \

  --timeout 300


 

And we put that in under systemd by creating a file named /etc/systemd/system/myproject.service and enter the code bellow:

#!/bin/sh

[Unit]

Description=myproject

After=network.target

 

[Service]

PIDFile=/var/run/cric.pid

User=www-data

Group=www-data

ExecStart=/bin/bash /home/sammy/myprojectdir/myprojectenv/bin/gunicorn_execute

Restart=on-abort

 

# make sure log directory exists and owned by syslog

PermissionsStartOnly=true

ExecStartPre=/bin/mkdir -p /var/log/myproject

ExecStartPre=/bin/chown syslog:adm /var/log/myproject

ExecStartPre=/bin/chmod 755 /var/log/myproject

StandardOutput=syslog

StandardError=syslog

SyslogIdentifier=myproject

 

[Install]

WantedBy=multi-user.target

 

It is really important that we separate the logs of a particular process from the logs of all the OS system, thus we can easily monitor our process without hassle. Notice that we have instructed systemd to run the process as the user “www-data”, who is created whenever we install Nginx/Apache2 web servers.

 

We then run :

sudo systemctl start myproject.service

to start the process, and :

sudo systemctl enable myproject.service

to enable the process to be started whenever we restart our server. You should then start monitoring the logs by tailing the systemd journal:

sudo journalctl -f -u myproject

 

The journal is stored as a binary file, so it cannot be tailed directly.  But we have syslog forwarding enabled on the systemd side, so now it is just a matter of configuring our syslog server. While ‘journalctl’ does provide the logs, what we really want is to have the logs available in the standard “/var/log/<service>” location.  So we will tell systemd to send it to syslog, and then have syslog write our files out to disk. Finally, the service should be part of the boot process, so that it automatically starts after reboot.

To do that, first modify “/etc/rsyslog.conf” and uncomment the lines below which tell the server to listen for syslog messages on port 514/TCP.

 

module(load="imtcp")

input(type="imtcp" port="514")

 

Then, create “/etc/rsyslog.d/30-myproject.conf” with the following content:

if $programname == 'myproject' or $syslogtag == 'myproject' then /var/log/myproject/myproject.log

& stop

Now restart the rsyslog service and you should see the syslog listener on port 514, restart the myproject.service, and now you should see log events being sent to the file every few seconds.

$ sudo systemctl restart rsyslog

$ netstat -an | grep "LISTEN "

$ sudo systemctl restart myproject

$ tail -f /var/log/myproject/myproject.log

 

You should see the logs coming in. 

 

Documents

Tags : django systemd

Short url : http://ubuviz.com/blog/1/

Next entry

Privacy Policy for UbuViz


Similar entries
Privacy Policy for UbuViz

Written by Allan on June 15, 2021 .

Pingbacks

Pingbacks are open.

Trackbacks

  1. med support on 11/09/2021 3:38 p.m. #

    UbuViz | IT solutions

  2. michael kors on 11/09/2021 3:39 p.m. #

    UbuViz | IT solutions

  3. firming eye cream on 11/09/2021 5:20 p.m. #

    UbuViz | IT solutions

  4. foaming cleanser on 11/09/2021 5:32 p.m. #

    UbuViz | IT solutions

  5. magento agency on 11/09/2021 6:38 p.m. #

    UbuViz | IT solutions

  6. thread lift london on 11/11/2021 8:29 a.m. #

    UbuViz | IT solutions

  7. massage therapy dorchester on 11/30/2021 3 a.m. #

    UbuViz | IT solutions

  8. podiatry dorchester on 12/04/2021 10:33 p.m. #

    UbuViz | IT solutions

  9. magento agency on 12/04/2021 11:57 p.m. #

    UbuViz | IT solutions

  10. seo southampton on 12/07/2021 11:19 a.m. #

    UbuViz | IT solutions

  11. osteopathy bridport on 12/09/2021 6:17 a.m. #

    UbuViz | IT solutions

  12. osteopathy weymouth on 12/12/2021 8:46 p.m. #

    UbuViz | IT solutions

  13. teaching walls on 01/07/2022 7:37 a.m. #

    UbuViz | IT solutions

  14. exercise books on 01/07/2022 10:11 a.m. #

    UbuViz | IT solutions

  15. lighting design on 01/15/2022 11:09 a.m. #

    UbuViz | IT solutions

  16. Kitchen lighting on 01/21/2022 4:36 a.m. #

    UbuViz | IT solutions

  17. steel stockholder on 01/22/2022 12:41 a.m. #

    UbuViz | IT solutions

  18. magento liverpool on 01/22/2022 8:15 a.m. #

    UbuViz | IT solutions

  19. educational furniture on 01/23/2022 2:43 p.m. #

    UbuViz | IT solutions

  20. fit out office on 01/26/2022 11:28 p.m. #

    UbuViz | IT solutions

  21. office refurbishment companies on 01/27/2022 1:19 p.m. #

    UbuViz | IT solutions

  22. builders beams on 01/27/2022 3:45 p.m. #

    UbuViz | IT solutions

  23. seo dorchester on 02/01/2022 1:04 a.m. #

    UbuViz | IT solutions

  24. harithaweli.lk on 02/09/2022 9:37 a.m. #

    UbuViz | IT solutions

  25. encyclopedia.dev.genetica.Asia on 02/17/2022 11:38 p.m. #

    UbuViz | IT solutions

  26. Tops y camisetas on 03/06/2022 4:51 a.m. #

    UbuViz | IT solutions

  27. togli duroni on 03/19/2022 1:46 p.m. #

    UbuViz | IT solutions

  28. best binary options robots on 03/24/2022 10:11 p.m. #

    UbuViz | IT solutions

  29. ratujudi on 04/08/2022 8:35 a.m. #

    UbuViz | IT solutions

  30. mexico on 04/12/2022 9:17 a.m. #

    UbuViz | IT solutions

  31. her response on 04/12/2022 11:54 a.m. #

    UbuViz | IT solutions

  32. piala Dunia on 04/20/2022 5:23 p.m. #

    UbuViz | IT solutions

  33. online games on 04/20/2022 10:34 p.m. #

    UbuViz | IT solutions

  34. binance futures delivery on 07/06/2022 12:47 a.m. #

    UbuViz | IT solutions

  35. weed posters on 07/21/2022 3:23 a.m. #

    UbuViz | IT solutions

  36. budmail on 07/22/2022 1:06 a.m. #

    UbuViz | IT solutions

  37. Successful Online on 07/22/2022 12:43 p.m. #

    UbuViz | IT solutions

  38. that site on 07/22/2022 7:30 p.m. #

    UbuViz | IT solutions

  39. pedro felipe on 07/23/2022 7:49 a.m. #

    UbuViz | IT solutions

  40. how to build Backlinks for a new website on 07/24/2022 11:10 a.m. #

    UbuViz | IT solutions

  41. instagram wall on 07/25/2022 12:10 a.m. #

    UbuViz | IT solutions

  42. this on 07/25/2022 12:19 a.m. #

    UbuViz | IT solutions

  43. apple music promotions on 08/02/2022 8:46 p.m. #

    UbuViz | IT solutions

  44. her response on 08/05/2022 5 p.m. #

    UbuViz | IT solutions

  45. binance futures 3commas on 08/10/2022 3:43 a.m. #

    UbuViz | IT solutions

  46. aron govil on 08/17/2022 7:21 a.m. #

    UbuViz | IT solutions

  47. auto parts on 08/21/2022 7:16 p.m. #

    UbuViz | IT solutions

  48. Miami Drug Rehabilitation Centers on 08/23/2022 5:11 a.m. #

    UbuViz | IT solutions

  49. education on 08/28/2022 4:17 a.m. #

    UbuViz | IT solutions

  50. báN Nhà mặt tiền q1 on 08/31/2022 7:51 p.m. #

    UbuViz | IT solutions

  51. mua bán nhà đất quận 6 on 08/31/2022 8:39 p.m. #

    UbuViz | IT solutions

  52. báN Nhà lý chiêu hoàNg quận 6 on 08/31/2022 10:14 p.m. #

    UbuViz | IT solutions

  53. diadiemnhaban on 08/31/2022 10:34 p.m. #

    UbuViz | IT solutions

  54. địa điểm nhà bán on 09/02/2022 5:33 p.m. #

    UbuViz | IT solutions

  55. More Signup bonuses on 09/11/2022 2:30 p.m. #

    UbuViz | IT solutions

  56. forex on 09/29/2022 7:49 p.m. #

    UbuViz | IT solutions

  57. cheap car loans on 10/22/2022 8:51 p.m. #

    UbuViz | IT solutions

  58. binary options trading system on 01/01/2023 11:14 a.m. #

    UbuViz | IT solutions

  59. smm panel murah on 01/22/2023 8:19 a.m. #

    UbuViz | IT solutions

  60. blog on 01/28/2023 12:41 a.m. #

    UbuViz | IT solutions

  61. pruduct review on 03/06/2023 2:58 a.m. #

    UbuViz | IT solutions

  62. pruduct review on 03/06/2023 4:46 a.m. #

    UbuViz | IT solutions

Trackback URL

Post your comment

Privacy Policy for UbuViz
This Privacy Policy document contains types of information that is collected and recorded by UbuViz…

Share on

Tweet

  • Home
  • About
  • Services
  • Contact
  • Blog
  • Get in touch on :
  • Facebook
  • Twitter
  • LinkedIn
  • Instagram

Phone & Mail
+25761200233
info@ubuviz.com
Address
N# 76, Boulevard Melchior Ndadaye (1er Novembre), Asiatique - Mukaza, Bujumbura, Burundi
Office
Peace Corner Building, 2nd floor
Privacy Policy | Terms and Conditions | © UbuViz - 2019