Resolve all issues with nginx+uwsgi deployment of Django of summary

  • 2020-05-14 06:02:01
  • OfStack

Recently, I finished the small project I wrote in the summer vacation. Thinking of putting it on my cloud server, I thought I could just open the port and run python3 manager runserver 0.0.0.0:80. Finally, I realized that it only applies to the development mode of Django and only supports single user access. I used nginx

nginx?

Why nginx?

First of all, I think it's small, lightweight, easy to use, and not as big or complicated as apache, and nginx is recommended to deploy Django on the Internet.

The installation

Here skip directly, say 1 point Linux users recommend you to install the source code, because the command installation may be installed into a taobao twice the development of nginx, personal or recommended to use the original.

uwsgi

Why do you need this

Simply put,nginx is a reverse proxy server. What can it do? Listen to a port, say 80, and configure a reverse proxy port, say 8000, so that all external user access to port 80 is actually a request for data on port 8000, but the user is not really communicating with port 8000, but through the bridge of 80. At present, I only think that this can hide their real port, you have any advice please point out in the comments.
In this case, it is still only possible for a single user to access it, so we need a tool that allows multiple users to access it concurrently, so uwsgi is it.

How do I install it?


pip install uwsgi

The configuration file

Let me first show you the file status of my project:


FlyCold
 ├ ─ ─  FlyCold
 │    ├ ─ ─  settings.py
 │    ├ ─ ─  urls.py
 │    └ ─ ─  wsgi.py
 ├ ─ ─  manage.py
 ├ ─ ─  SchoolBuy
 │    ├ ─ ─  admin.py
 │    ├ ─ ─  forms.py
 │    ├ ─ ─  __init__.py
 │    ├ ─ ─  models.py
 │    ├ ─ ─  urls.py
 │    └ ─ ─  views.py
 └ ─ ─  templates

As explained below, this is a simplified directory tree. The project created is called FlyCold, and the generated FlyCold subdirectory and SchoolBuy subdirectory are FlyCold. My main code is in SchoolBuy, setting.py in the Flycold subdirectory, manager.py in the FlyCold root directory.

After the installation comes a configuration file, the content is as follows


# myweb_uwsgi.ini file
[uwsgi]

# Django-related settings

socket = :8080
# The port of the real service 

# Django Project root directory  ( An absolute path )
chdir      = /home/lyt/FlyCold

# wsgi.py The location of the file in the project 
module     = FlyCold.wsgi

# process-related settings
# master
master     = true

#  Number of processes running 
processes    = 4

# ... with appropriate permissions - may be needed
# chmod-socket  = 664
# clear environment on exit
vacuum     = true

This.ini file can be put anywhere, at startup uwsgi --ini ***.ini

Configuration nginx

Find nginx.conf and write the following


  server {
    # Here is the port used for access 
  listen    80;
    server_name localhost;

    charset UTF-8;
    # This saves the log file 
    access_log /var/log/nginx/SchoolBuy_access.log;
    error_log  /var/log/nginx/SchoolBuy_error.log;


    client_max_body_size 75M;
    location / {
        include uwsgi_params;
        # with uwsgi content 
        uwsgi_pass 127.0.0.1:8001;
        # Link timeout 
        uwsgi_read_timeout 30;
    }
  }

This way, restart your nginx, access port 80, and you will see the effect.

Other questions?

You may have found that static resources on your web page are not accessible!! The admin page, for example, can be extremely crude because Django's handling of static resources cannot be represented (possibly) by nginx+uwsgi+Django. Anyway, this shouldn't be done by Django, because nginx is much better at handling static resources. For static resources, let nginx handle it.

In general, you'll have two static resources that start with /media/ and /static/. static is used to handle some original pictures, videos, js,css files, Django supports this kind of link. So how do you close a file that starts with Django processing /static/? It's very simple. In setting.py, change the DEBUG value to False, then Django will not process /static/ file.

What about /media/? 1 generally speaking, we will save the pictures uploaded by users and use /media/ when they are displayed on the web page, and set them in setting.py


MEDIA_URL = '/media/' # Access the prefix link 
MEDIA_ROOT = os.path.join(BASE_DIR, '../media') # Where to store documents 

Add it to url.py


from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
  urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This means that when DEBUG=True, the /media/ file will be parsed, and the location of the file is the second parameter.

In this way, when the DEBUG is immediately deployed for production, it is only necessary to change DEBUG to False, and Django will not handle static and media.

Collecting static files

Django has a tool to collect all the static files used in the application for nginx parsing. Specific:

In setting.py, set STATIC_ROOT = os.path.join (BASE_DIR, '.. / collectedstatic ')

The collected static files are then placed in the directory above. How do I run this tool? python3 manager. py collectstatic

Configure nginx to parse static files

Similarly, nginx conf

First, add user root to the top of the file

Declare root user to run nginx, otherwise access to static files may prompt no permissions

Second, add the following before the configuration file location/mentioned above


    location /static/ {
      autoindex on;
      alias /root/SchoolBuyWeb/collectedstatic/;
    }

    location /media/ {
      autoindex on;
      alias /root/SchoolBuyWeb/media/;
    }

Note that after alias corresponding to their own Settings of the directory can be!

Restart nginx, it is now ok ~ ~


Related articles: