search
HomeBackend DevelopmentPHP Tutorialngix uwsgi django combination website building

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>

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;
}

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>
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模块,网上很多都是错误的,直接用自动生成的就行。
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;
}
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
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
How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

How often should you regenerate session IDs?How often should you regenerate session IDs?Apr 23, 2025 am 12:03 AM

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version