


Solve all problems of deploying Django with nginx+uwsgi (summary)_nginx
This article mainly introduces all the problems of deploying Django with nginx+uwsgi (summary). I think it is quite good. Now I will share it with you and give you a reference. Let’s take a look together
Recently, the small project I wrote during the summer vacation has been completed. I thought about putting it on my own cloud server. I originally thought that I just need to open the port and run python3 manager runserver 0.0.0.0:80 and it will be done. , and finally I learned that this only applies to Django's development mode and only supports single-user access. In this case, a web server is required for deployment. I used nginx
nginx?
Why nginx?
First of all, I think it is small, very lightweight, easy to use, not as complicated as apache, and online It is recommended to use nginx to deploy Django.
Installation
Skip it directly here, let’s talk about it. Linux users recommend you to install from source code, because the command installation may pretend to be a Taobao II. For the newly developed nginx, I personally recommend using the original version.
uwsgi
Why do you need this?
Simply put, nginx is a reverse proxy server, it can do What's going on? To listen to a port, such as 80, you can configure a reverse proxy port, such as 8000. In this way, all external users' access to port 80 is actually requesting data from port 8000, but the user is not actually communicating with port 8000. , but passed 80 this bridge. At present, I only think that this can hide my real port. If you have any suggestions, please leave a message.
In this case, it can actually only be accessed by a single user, so we need a tool that can be accessed by multiple users concurrently, then it is uwsgi.
how to install?
pip install uwsgi
Configuration file
Let me take a look at it first 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
Explanation below, this is a simplified directory tree, the created project is named FlyCold, the generated FlyCold subdirectory and SchoolBuy subdirectory. My main code is in SchoolBuy, setting.py is in the Flycold subdirectory, and manager.py is in the Flycold root directory.
After installation, create a configuration file with the following content
# myweb_uwsgi.ini file [uwsgi] # Django-related settings socket = :8080 #真实服务的端口 # Django项目根目录 (绝对路径) chdir = /home/lyt/FlyCold # wsgi.py文件在项目中的位置 module = FlyCold.wsgi # process-related settings # master master = true # 运行的进程数 processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
This .ini file can be placed anywhere, when starting uwsgi --ini ***.ini
Configure nginx
Find nginx.conf and write the following content
server { #这里是访问时用到的端口 listen 80; server_name localhost; charset UTF-8; #这块存让日志文件 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; #同uwsgi内容 uwsgi_pass 127.0.0.1:8001; #链接超时时间 uwsgi_read_timeout 30; } }
In this way, restart your nginx, access port 80, and you will see the effect.
Still have questions?
You may have discovered that the static resources on your web page cannot be accessed! ! For example, the admin page will be very simple. This is because when nginx+uwsgi+Django, nginx cannot be used as a proxy for Django's processing of static resources (maybe). In short, Django should not be allowed to do this kind of thing, because nginx is more capable of handling static resources. For static resources, let nginx handle it.
Generally speaking, you will have two types of static resources: links starting with /media/ and links starting with /static/. Static is used to process some website original images, videos, js, and css files. Django itself supports this kind of link. So how to turn off Django to process files starting with /static/? It is very simple. Change the DEBUG value to False in setting.py. At this time, Django will not process the /static/ file.
What about /media/? Generally speaking, we will save the pictures uploaded by users, use /media/ when displaying them on the web page, and set
MEDIA_URL = '/media/' #访问的前缀链接 MEDIA_ROOT = os.path.join(BASE_DIR, '../media') #存放文件的具体位置
in setting.py Then add
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)
in url.py. This means that when DEBUG=True, the /media/ file will be parsed, and the file storage location is the second parameters.
In this way, when deploying to a production environment, you only need to change DEBUG to False, and Django will not process static and media.
Collect static files
Django has a tool that can collect all static files used in the application to facilitate nginx parsing. Specific:
Set STATIC_ROOT = os.path.join(BASE_DIR, '../collectedstatic')
In this way, all static files collected will be into the directory above. How to run this tool? python3 manager.py collectstatic
Configure nginx to parse static files
Similarly, nginx.conf
First, Add the user root
statement at the top of the file to let the root user run nginx, otherwise access to static files may prompt that there is no permission
Secondly, before the configuration file location / mentioned above Add the following content
location /static/ { autoindex on; alias /root/SchoolBuyWeb/collectedstatic/; } location /media/ { autoindex on; alias /root/SchoolBuyWeb/media/; }
Pay attention to the alias and then correspond to the directory you set!
Restart nginx, now it’s ok~~
The above is the detailed content of Solve all problems of deploying Django with nginx+uwsgi (summary)_nginx. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Steps to check the Django version: 1. Open a terminal or command prompt window; 2. Make sure Django has been installed. If Django is not installed, you can use the package management tool to install it and enter the pip install django command; 3. After the installation is complete , you can use python -m django --version to check the Django version.

Django and Flask are both leaders in Python Web frameworks, and they both have their own advantages and applicable scenarios. This article will conduct a comparative analysis of these two frameworks and provide specific code examples. Development Introduction Django is a full-featured Web framework, its main purpose is to quickly develop complex Web applications. Django provides many built-in functions, such as ORM (Object Relational Mapping), forms, authentication, management backend, etc. These features allow Django to handle large

Django is a complete development framework that covers all aspects of the web development life cycle. Currently, this framework is one of the most popular web frameworks worldwide. If you plan to use Django to build your own web applications, then you need to understand the advantages and disadvantages of the Django framework. Here's everything you need to know, including specific code examples. Django advantages: 1. Rapid development-Djang can quickly develop web applications. It provides a rich library and internal

How to upgrade Django version: steps and considerations, specific code examples required Introduction: Django is a powerful Python Web framework that is continuously updated and upgraded to provide better performance and more features. However, for developers using older versions of Django, upgrading Django may face some challenges. This article will introduce the steps and precautions on how to upgrade the Django version, and provide specific code examples. 1. Back up project files before upgrading Djan

django is the backend. Details: Although Django is primarily a backend framework, it is closely related to front-end development. Through features such as Django's template engine, static file management, and RESTful API, front-end developers can collaborate with back-end developers to build powerful, scalable web applications.

The differences are: 1. Django 1.x series: This is an early version of Django, including versions 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8 and 1.9. These versions mainly provide basic web development functions; 2. Django 2.x series: This is the mid-term version of Django, including 2.0, 2.1, 2.2 and other versions; 3. Django 3.x series: This is the latest version series of Django. Including versions 3.0, 3, etc.

How to check the django version: 1. To check through the command line, enter the "python -m django --version" command in the terminal or command line window; 2. To check in the Python interactive environment, enter "import django print(django. get_version())" code; 3. Check the settings file of the Django project and find a list named INSTALLED_APPS, which contains installed application information.

Django is a web application framework written in Python that emphasizes rapid development and clean methods. Although Django is a web framework, to answer the question whether Django is a front-end or a back-end, you need to have a deep understanding of the concepts of front-end and back-end. The front end refers to the interface that users directly interact with, and the back end refers to server-side programs. They interact with data through the HTTP protocol. When the front-end and back-end are separated, the front-end and back-end programs can be developed independently to implement business logic and interactive effects respectively, and data exchange.
