Deploy Django with UWSGI + NGINX + Systemd service

#StandWithUkraine
Today, 25th June 2022, Ukraine is still bravely fighting for democratic values, human rights and peace in whole world. Russians ruthlessly kill all civilians in Ukraine including childs and destroy their cities. We are uniting against Putinโ€™s invasion and violence, in support of the people in Ukraine. You can help by donating to Ukrainian's army.

We will run uwsgi from www-data user (also used by nginx).

/etc/nginx/conf.d/proj.conf:

upstream mtdja {
    server unix:///run/uwsgi/proj.sock;
}

server {
   listen 80;
   server_name proj.org;
   return 301 https://$host$request_uri;
}

server {
    server_name proj.net;

    listen 443 ssl;
    ssl on;
    ssl_certificate /ssl/proj/ssl-bundle.crt;
    ssl_certificate_key /ssl/proj/proj.key;

    charset utf-8;
    client_max_body_size 75M;
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 8;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

    location /static {
      alias /srv/proj/serve;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }

    location /media {
        root /srv/proj/media;
    }

    location /robots.txt {
       root /srv/proj/folder_with_managepy/;
    }

    location / {
        uwsgi_pass mtdja;
        include /srv/proj/deploy/wsgi_params;
    }
}

/srv/proj/deploy/uwsgi.ini#

[uwsgi]
chdir = /srv/proj/folder_with_managepy
socket = /run/uwsgi/proj.sock
chown-socket = www-data
chmod-socket = 664
uid = www-data
gid = www-data

processes=1
harakiri=30
vacuum=True
max-requests=5000
logto = /var/log/proj/uwsgi.log
module = proj.wsgi
disable-logging = true

env = LANG=en_US.UTF-8

/etc/systemd/system/proj.service#

[Unit]
Description=uWSGI instance to serve proj

[Service]
ExecStartPre=-/bin/bash -c 'mkdir -p /var/log/proj/; mkdir -p /run/uwsgi; chown www-data /run/uwsgi'
ExecStart=/bin/bash -c 'cd /srv/proj/folder_with_managepy; uwsgi --ini /srv/proj/deploy/uwsgi.ini'
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

Managing access permissions

Your app should be able to write into media, so we will change it user www-data. If your app will create subfolders, they will have a user that runs your app (also www-data)

mkdir -p /srv/proj/media/
chown -R www-data /srv/proj/media/ 
#systemd #nginx #deploy #django
1
Ivan Borshchov profile picture
Jul 02, 2017
by Ivan Borshchov
Did it help you?
Yes !
No

Best related