Home Backend Development PHP Tutorial ngix uwsgi django combination website building

ngix uwsgi django combination website building

Aug 08, 2016 am 09:23 AM
django nbsp uwsgi

Since I recently had to do an innovative project that required python web development, I studied django, a very versatile python web framework.

Why do you need ngix?

First of all, let’s talk about why it is necessary to combine these three to build a website. If you only use Django, you cannot achieve load balancing. For example, if you need to request some static resources, you need Django to process them all, and the requests cannot be distributed reasonably, and ngix It can just solve this problem (it seems that this project does not need to consider load balancing~~ but improving performance is always necessary). ngix can send some dynamic requests to django for processing through configuration, and handle static requests by itself. In addition, if you still need to run PHP pages on the website, then ngix is ​​definitely a good choice. It distributes PHP requests to apache for processing. ngix and apache communicate through the socket port, and then return the processed results to the client.

Then why do you need uwsgi?

WSGI is a web server gateway interface. It is a specification for communication between a web server (such as nginx) and an application server (such as uWSGI server). Then uwsgi is a web server that implements both uwsgi and WSGI protocols. To put it simply, using uwsgi we can start django from uwsgi, and then ngix communicates with uwsgi through the port. In this process, gjango realizes the function of wsgi server, and ngix realizes the function of wsgi client, but in the web, ngix plays the role of a server (Many software communications in computers are in client-server mode. For example, file resources are a service, and then the application implements the corresponding protocol to call this service). This realizes the transfer of uwsgi between ngix and django.

Why not let ngix communicate with django directly?

uWSGI does not use either the wsgi protocol or the fcgi protocol. Instead, it creates its own uwsgi protocol, which is said to be about 10 times faster than the fcgi protocol.
The main features of uWSGI are as follows:
◆Ultra-fast performance.
◆Low memory usage (measured to be about half of apache2’s mod_wsgi).
◆Multiple app management.
◆Detailed log function (can be used to analyze app performance and bottlenecks).
◆Highly customizable (memory size limit, restart after a certain number of services, etc.).

The principles between the three are as follows,

<span><code>the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django</code></span>
Copy after login

Start deployment

1. Install ngix sudo apt-get install ngix

Thanks to my machine There is also apache on it. I need to modify the listening port of ngix to 8080

The modification is as follows

server {
	listen 8080 default_server;
	listen [::]:8080 default_server ipv6

	root /var/ngix;  #将根目录改为/var/ngix
	index index.html index.htm;
}
Copy after login

2. Install django

Use python’s own package manager easy_install to installeasy_install django Automatically install the latest version

3. Install uwsgi

<code>apt-get install python-dev #不安装这个,下面的安装可能会失败
pip install uwsgi
如果是apt-get安装就需要     sudo apt-get install uwsgi-plugin-python
</code>
Copy after login
After the tool is installed, you can start configuring the project. The development environment I use is pycharm, which can automatically generate django projects. If you do not need to run django admin. py generates the corresponding project file. The following is the structure of my project

.
└── myproject
├── app
│ ├── admin.py
│ ├── __init__.py
│ ├─ migration─s
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├ ── manage.py
├── myproject
│ ├── django.xml
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings .pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── templates
└── test.py
django.xml file content is:

<uwsgi>
    <socket>127.0.0.1:8630</socket>
    <chdir>/var/ngix/myproject/myproject</chdir>
    <pythonpath>..</pythonpath>
    <module>wsgi</module>
</uwsgi>
这里需要注意的是wsgi模块,网上很多都是错误的,直接用自动生成的就行。
Copy after login
use The socket communicates with ngix. The port number is the 8630 port of the local machine

Then you need to add it to the ngix configuration file

location / {
		include uwsgi_params;
		uwsgi_pass 127.0.0.1:8630;
}
Copy after login
uwsgi is the module that comes with ngix

Restart the ngix service, and then start the uwsgi service

$ uwsgi -x django.xml --plugin python

needs to be executed in the directory where django. Got it~~

After a day of tossing, it’s donengix uwsgi django combination website building

Let’s summarize other things learned: deleting the corresponding installation files under /etc/dpkg/info/ can solve the problem of apt sub-process startup errors. After deleting it, you need to autoremove it

Finally, distribution is really a big pitfall! ! ! ! ! ! ! !

Data sharing: wsgi concept uwsgi concept Deploying Django on Ubuntu based on nginx and uWSGI

How to hand over uwsgi to supervisor management unavailable-modifier-requested-0 solution

uwsgi manual Summary of frequently asked questions about deploying django with uwsgi Django Chinese tutorial It is recommended to read the English tutorial, which is more complete

The above is the information I refer to

The above introduces the ngix uwsgi django combination website building, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to check django version How to check django version Dec 01, 2023 pm 02:25 PM

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.

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

Django vs. Flask: A comparative analysis of Python web frameworks Django vs. Flask: A comparative analysis of Python web frameworks Jan 19, 2024 am 08:36 AM

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 Framework Pros and Cons: Everything You Need to Know Django Framework Pros and Cons: Everything You Need to Know Jan 19, 2024 am 09:09 AM

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 check django version How to check django version Nov 30, 2023 pm 03:08 PM

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.

See all articles