Lazy deploy Django project UWSGI + NGINX (UBUNTU 20.04)

Hello!

I want to present you step by step instructions for deploying a django project.

I must say right away that using my brief instructions, you will not understand the mechanics of deployment. In fact, this is just a list of commands for deployment. There will be no details about the work of UWSGI, NGINX and Django itself. I’ll just help you quickly reach the goal, and we have one goal – to finally deploy this **** project!

(Instruction tested on Ubuntu 20.04 VPS with Django 3.2.8, python3.8, NGINX 1.18.0, UWSGI 2.0.20)

We go into the console of the Linux server and go!

sudo apt update
sudo apt upgrade
sudo apt-get install nginx uwsgi
sudo apt-get install uwsgi-plugin-python3

Go to the /var directory

cd /var

Create a folder inside for further work

mkdir work
cd work

Create a folder for statics and give the user www-data rights to it. (UWSGI and NGINX will run as www-data)

mkdir static
chown www-data /var/work/static

Check the version of Python, install the package for Wirth. environment and create it
python3 -V
sudo apt install python3.8-venv
python3 -m venv venv

Activate the environment

. ./venv/bin/activate

pip install django (and others or download later from requirements.txt)

git clone https://github.com/nick_name/project.git (or move your project to the current folder in any other way)

(In the instructions, I use a project with a standard view architecture:

projectname/
...
manage.py
projectname/
...
settings.py
wsgi.py
...

)

Go to the settings.py file of your django project and edit it.

You need to add your server ip to ALLOWED_HOSTS, you should get something like [‘1.1.1.1’, ‘127.0.0.1’]
add STATIC_ROOT=’/var/work/static’

Go back to the folder with manage.py
cd /var/work/имя вашего проекта

Next, let’s prepare the Django project itself. Let’s carry out migrations and collect statics into the folder that we previously created in work.

python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py collectstatic
python3 manage.py runserver

If there is an error in the project – fix it, if there are not enough libraries – install via pip install

If there are no errors, your Django project is fine, if not, then fix the errors in it. I advise you to check whether you have installed the correct version of Django on your server.

We are nearing the end. The virtual environment can be turned off. Let’s do it with the command:

deactivate

Next, open the nginx.conf file and check the user parameter in it, it should be www-data.

user www-data;

nano /etc/nginx/nginx.conf

next, create and fill in the my_app config (for nginx)

nano /etc/nginx/sites-enabled/my_app

server {
    listen 81;
    server_tokens off;
    server_name 0.0.0.0;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///run/uwsgi/app/myapp/socket;
    }

   
    location /static/ {
        alias /var/work/static/;
    }

}

Next, create and fill in the myapp.ini config (for uwsgi)

[uwsgi]
chdir = /var/work/projectname
env = DJANGO_SETTINGS_MODULE = projectname.settings
wsgi-file = /var/work/projectname/projectname/wsgi.py
workers = 1
max-requests = 5000
plugins = python3
home = /var/work/venv
pythonpath = /var/work/venv/lib/python3.8/site-packages
processes = 5
threads = 2
master = true
die-on-term = true
socket = /run/uwsgi/app/myapp/socket
chmod-socket = 664
vacuum = true
uid = www-data
gui = www-data

!!! I am using python3 (3.8) if yours is different then change the python related items
Or use version 3.8)) !!!

projectname EVERYWHERE change to the name of your project

then restart uwsgi and nginx

service uwsgi restart
service nginx restart

After restarting nginx and uwsgi, the site should be accessible at http://your server IP:81

WHERE TO SEE THE LOGS?
/var/log/nginx
/var/log/uwsgi/app

I hope this helped you. On the Internet, nothing that I looked at worked out of the box. Each time there was an error here and there .. Well, or it was a huge “sheet” of text, which took too long to read. And after so many mistakes, there was no trust in one or another tutorial.

After compiling this tutorial, I created several new servers on Ubuntu and tested each step.

I will be glad to comments. Write if you have any errors!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *