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>
.
└── 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模块,网上很多都是错误的,直接用自动生成的就行。
Then you need to add it to the ngix configuration file
location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8630; }
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 done
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.