Deploy Django with UWSGI + NGINX + Systemd service

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


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

server {
   listen 80;
   return 301 https://$host$request_uri;

server {

    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/ 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;


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

logto = /var/log/proj/uwsgi.log
module = proj.wsgi
disable-logging = true

env = LANG=en_US.UTF-8


Description=uWSGI instance to serve proj

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'


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
menu 1
Ivan Borshchov profile picture
Jul 02, 2017
by Ivan Borshchov
Did it help you?
Yes !

Best related

Other by Ivan Borshchov