Skip to content

Instantly share code, notes, and snippets.

@willemarcel
Last active September 5, 2019 10:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willemarcel/a07917d21b7f3cce92da to your computer and use it in GitHub Desktop.
Save willemarcel/a07917d21b7f3cce92da to your computer and use it in GitHub Desktop.
Running Django apps with nginx and uwsgi

How to configure nginx and uwsgi to serve a Django application

Install

You need to install nginx, uwsgi and uwsgi-plugin-python (or uwsgi-plugin-python3)

Create nginx files

Create /etc/nginx/sites-available/project and create a link to it in /etc/nginx/sites-enabled/project

server {
    listen 80;

    server_name domain.com;
    client_max_body_size 5m;

    access_log  /var/log/nginx/project.access.log;
    error_log   /var/log/nginx/project.error.log;

        location /static/ {
                gzip_static on;
                alias   /home/project/static/;
        }

        location /media/ {
                gzip_static on;
                alias   /home/project/media/;
        }

        location / {
                gzip_static on;
                include uwsgi_params;
                uwsgi_pass unix:/var/run/uwsgi/app/project/socket;
        }
}

Create uwsgi files

Create /etc/uwsgi/apps-available/project.ini and create a link to it in /etc/uwsgi/apps-enabled/project.ini

[uwsgi]
plugins = python
home = /home/wille/.virtualenvs/projectenv/
chdir = /home/project/
pythonpath = ..
env = DJANGO_SETTINGS_MODULE=project.settings
module = project.wsgi:application
touch-reload = /home/project/uwsgi_reload
LANG = en_US.UTF-8
max-requests=5000
socket=127.0.0.1:8084
uid = wille
gui = users
processes = 4
threads = 2

Execute touch /home/project/uwsgi_reload and remember to change the port in the socket parameter to each application you deploy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment