[PYTHON] The road to fighting the connection between Nginx, Django and uwsgi and winning a little

I decided to do Django and Nginx in earnest, and after two weeks, I finally succeeded in connecting to the server!

I've connected with flask, wsgi and apache2, but I've always hated Django. However, I decided to introduce Django and nginx because I wanted to introduce WebSocket and so on.

Even now, it is not displayed unless I enter ** "http://example.com:9000" **, but I have reached the point where uwsgi is started in the background.

I will write the part that I struggled with below

environment

First of all, installing the nginx server is confusing

It was good until I started the nginx server

$sudo /etc/init.d/nginx restart

When I typed it, it sometimes failed to start. I got the following display

[....] Restarting nginx (via systemctl): nginx.serviceJob for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
 failed!

This was due to the wrong configuration file in ** / etc / nginx / sites-available / ** in my Ubuntu environment. (For example, the port was duplicated with another server)

I don't understand how uwsgi works and it's a big mess

This was the beginning of suffering. I was suffering from uwsgi and had a hard time. Above all, it is a failure to think that something can be done with glue.

Think of uwsgi as middleware that acts as an intermediary with settings to connect nginx and Django apps. How to paste my crappy ini file. By the way, I will also introduce the sites that were very helpful in uwsgi. (English)

This is the site

Below are my ini files and their commentary.

# open_dashboard_uwsgi.ini file
[uwsgi]
project = open_dashboard
base = /home/user/Python #← Path of my Python project

chdir = %(base)/%(project)
#home = %(base)/Env/%(project)
home = %(base)/env #← Path of my Python virtual environment
module = %(project).wsgi:application

#uid = root
uid = user #← My user name
master = true
processes = 2

socket = %(base)/%(project)/%(project).sock #← Do not try to make this socket yourself
chmod-socket = 666
vacuum = true #← It is quite important that the socket is automatically deleted when the server is cut off.

Based on this, the emperor mode is set to the mode that runs uwsgi in the background (actually, it is a great mode that can start multiple apps), but in most cases it stumbles with the following error.

unix:///home/user/Python/open_dashboard/open_dashboard.sock failed (13: Permission denied)

This is annoying, but it can be resolved by using the emperor mode uid and gid as your username. Also the log file ** sudo chown user: user /var/log/uwsgi-emperor.log ** Don't forget to set such as.

Set .bashrc as follows. (/etc/rc.local seems to be good, but for the time being, the sure one)

.bashrc


/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log

The *** --emperor / etc / uwsgi / vassals *** part of the above code is set as follows.

# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/

-(Updated January 1, 2020) For users, www-data is good. Specify / tmp if no socket is created -(Updated January 1, 2020) If uwsgi of Python2, an error will occur in Python3, so you need to reinstall uwsgi with pip3 etc.

You may not understand it because you are in a hurry, but for the time being ** So that I don't get lost again ** I wrote this article.

[I was spending dark days with only 502 errors like this article](https://www.linode.com/community/questions/222/how-do-i-fix-this-502-bad- gateway-error-when-setting-up-django-project-on-ubuntu)

_ Next, I'm going to challenge Docker! _

Revision history

--2019/12/16 Newly created --2020/1/1 Added how to install and reconfigure uwsgi for Python3 again when doing it with Python3

Recommended Posts

The road to fighting the connection between Nginx, Django and uwsgi and winning a little
[Introduction to Python] What is the difference between a list and a tuple?
How to count the number of elements in Django and output to a template
Django REST framework A little useful to know.
How to set up a VPN gateway to establish a connection between Alibaba Cloud and AWS
[Django] Test to send a file by POST and check the returned context [TDD]
From installing Flask on CentOS to making it a service with Nginx and uWSGI
Communication between uWSGI and Nginx using Unix Domain Socket
Visualization of the connection between malware and the callback server
How to use argparse and the difference between optparse
Transit to the update screen with the Django a tag
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
The road to Pythonista
The road to Djangoist
What is the difference between a symbolic link and a hard link?
Understand the difference between cumulative assignment to variables and cumulative assignment to objects
How to generate a query using the IN operator in Django
Build a Python environment and transfer data to the server
A rough summary of the differences between Windows and Linux
I tried to enumerate the differences between java and python
Road to the Wizard: Step 8 (Interpret How to become a Hacker)
I want to get information from fstab at the ssh connection destination and execute a command
I tried to find out the difference between A + = B and A = A + B in Python, so make a note