


Five steps to teach you how to deploy Django programs using Nginx+uWSGI+Django method
NOTE: WEB services can also be implemented using only uwsgi+django without using nginx. uwsgi can also handle WEB requests directly.
The first step is to solve the bridge between uwsgi and django. Solve the problem of how to use uwsgi+DJANGO to implement a simple Web server without nginx.
The second step is to solve the bridge between uwsgi and Nginx. Through the bridge between nginx and uwsgi, the connection between nginx and django is opened, so as to realize the deployment of django more perfectly.
Environment introduction
Install uwsgi
<spanpalatino linotype new roman color:rgb>$ <spanpalatino linotype new roman color:rgb>export <spanpalatino linotype new roman color:rgb>LDFLAGS<spanpalatino linotype new roman color:rgb>=<spanpalatino linotype new roman color:rgb>"-Xlinker --no-as-needed"<spanpalatino linotype new roman color:rgb>$ pip install uwsgi </spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino>
Test uwsgi
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
uwsgi --http :8001 --wsgi-file test.py
Configure django
NOTE:
Please ensure that your django project is in normal use. You can use
python manage.py runserver 0.0.0.0:8002
to test whether your django project can run normally.
Note: When writing files, you need to pay attention to the statement os.environ.setdefault. For example, if your project is mysite, your statement should be
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<spanpalatino linotype new roman color:rgb font-style:italic>#!/usr/bin/env python<spanpalatino linotype new roman color:rgb font-style:italic># coding: utf-8<spanpalatino linotype new roman color:rgb font-weight:bold>import<spanpalatino linotype new roman color:rgb font-weight:bold>os<spanpalatino linotype new roman color:rgb font-weight:bold>import<spanpalatino linotype new roman color:rgb font-weight:bold>sys<spanpalatino linotype new roman color:rgb font-style:italic># 将系统的编码设置为UTF8<spanpalatino linotype new roman color:rgb>reload<spanpalatino linotype new roman>(<spanpalatino linotype new roman>sys<spanpalatino linotype new roman>)<spanpalatino linotype new roman>sys<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>setdefaultencoding<spanpalatino linotype new roman>(<spanpalatino linotype new roman color:rgb>'utf8'<spanpalatino linotype new roman>)<spanpalatino linotype new roman>os<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>environ<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>setdefault<spanpalatino linotype new roman>(<spanpalatino linotype new roman color:rgb>"DJANGO_SETTINGS_MODULE"<spanpalatino linotype new roman>,<spanpalatino linotype new roman color:rgb>"mysite.settings"<spanpalatino linotype new roman>)<spanpalatino linotype new roman color:rgb font-weight:bold>from<spanpalatino linotype new roman color:rgb font-weight:bold>django.core.handlers.wsgi<spanpalatino linotype new roman color:rgb font-weight:bold>import<spanpalatino linotype new roman>WSGIHandler<spanpalatino linotype new roman>application<spanpalatino linotype new roman color:rgb>=<spanpalatino linotype new roman>WSGIHandler<spanpalatino linotype new roman>()</spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino>
连接django和uwsgi,实现简单的Web服务器。
uwsgi --http :8000 --chdir /home/work/src/sites/testdjango1/testdjango/mysite --module django_wsgi
最后:
参考、解释及其它
wsgi: WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx)与应用服务器(如uWSGI服务器)通信的一种规范。
关于WSGI协议看这里:WSGI
uWSGI: http://uwsgi-docs.readthedocs.org/en/latest/index.html uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。 Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
uwsgi: uwsgi同WSGI一样是一种通信协议,而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
关于uwsgi协议看这里:The uwsgi protocol
有了uWSGI为什么还需要nginx?
nginx具备优秀的静态内容处理能力,然后将动态内容转发给uWSGI服务器,这样可以达到很好的客户端响应。
参考文献:http://heipark.iteye.com/blog/1750970
环境介绍
配置uWSGI
<spanpalatino linotype new roman color:rgb font-weight:bold><uwsgi><spanpalatino linotype new roman color:rgb font-weight:bold><socket>:8077<spanpalatino linotype new roman color:rgb font-weight:bold></spanpalatino></socket><spanpalatino linotype new roman color:rgb font-weight:bold><chdir>/home/work/src/sites/testdjango1/testdjango/mysite<spanpalatino linotype new roman color:rgb font-weight:bold></spanpalatino></chdir><spanpalatino linotype new roman color:rgb font-weight:bold><module>django_wsgi<spanpalatino linotype new roman color:rgb font-weight:bold></spanpalatino></module><spanpalatino linotype new roman color:rgb font-weight:bold><processes>4<spanpalatino linotype new roman color:rgb font-weight:bold></spanpalatino></processes><spanpalatino linotype new roman color:rgb font-style:italic><!-- 进程数 --><spanpalatino linotype new roman color:rgb font-weight:bold><daemonize>uwsgi.log<spanpalatino linotype new roman color:rgb font-weight:bold></spanpalatino></daemonize><spanpalatino linotype new roman color:rgb font-weight:bold></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></uwsgi></spanpalatino>
配置Nginx
<spanpalatino linotype new roman color:rgb font-weight:bold>server<spanpalatino linotype new roman>{<spanpalatino linotype new roman>listen<spanpalatino linotype new roman color:rgb>80<spanpalatino linotype new roman>;<spanpalatino linotype new roman>server_name<spanpalatino linotype new roman>www<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>you<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>com<spanpalatino linotype new roman>;<spanpalatino linotype new roman>access_log<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>home<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>work<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>var<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>test<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>logs<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>access<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>log<spanpalatino linotype new roman>;<spanpalatino linotype new roman>error_log<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>home<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>work<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>var<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>test<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>logs<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>error<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>log<spanpalatino linotype new roman>;<spanpalatino linotype new roman color:rgb>#charse<spanpalatino linotype new roman>t<spanpalatino linotype new roman>koi8<spanpalatino linotype new roman color:rgb>-<spanpalatino linotype new roman>r<spanpalatino linotype new roman>;<spanpalatino linotype new roman color:rgb>#access<spanpalatino linotype new roman>_log<spanpalatino linotype new roman>logs<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>host<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>access<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>log<spanpalatino linotype new roman>main<spanpalatino linotype new roman>;<spanpalatino linotype new roman>location<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>{<spanpalatino linotype new roman><strong>include</strong><spanpalatino linotype new roman>uwsgi_params<spanpalatino linotype new roman>;<spanpalatino linotype new roman>uwsgi_pass<spanpalatino linotype new roman color:rgb>127<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman color:rgb>0<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman color:rgb>0<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman color:rgb>1<spanpalatino linotype new roman color:rgb>:<spanpalatino linotype new roman color:rgb>8077<spanpalatino linotype new roman>;<spanpalatino linotype new roman>}<spanpalatino linotype new roman color:rgb>#error_page<spanpalatino linotype new roman color:rgb font-weight:bold>404<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman color:rgb font-weight:bold>404<spanpalatino linotype new roman color:rgb font-weight:bold>.html<spanpalatino linotype new roman color:rgb>;<spanpalatino linotype new roman>#<spanpalatino linotype new roman color:rgb font-weight:bold>redirect<spanpalatino linotype new roman color:rgb font-weight:bold>server<spanpalatino linotype new roman color:rgb font-weight:bold>error<spanpalatino linotype new roman color:rgb font-weight:bold>pages<spanpalatino linotype new roman color:rgb font-weight:bold>to<spanpalatino linotype new roman color:rgb font-weight:bold>the<spanpalatino linotype new roman color:rgb font-weight:bold>static<spanpalatino linotype new roman color:rgb font-weight:bold>page<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman color:rgb font-weight:bold>50x<spanpalatino linotype new roman color:rgb font-weight:bold>.html<spanpalatino linotype new roman>#<spanpalatino linotype new roman color:rgb font-weight:bold>error_page<spanpalatino linotype new roman color:rgb font-weight:bold>500<spanpalatino linotype new roman color:rgb font-weight:bold>502<spanpalatino linotype new roman color:rgb font-weight:bold>503<spanpalatino linotype new roman color:rgb font-weight:bold>504<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman color:rgb font-weight:bold>50x<spanpalatino linotype new roman color:rgb font-weight:bold>.html<spanpalatino linotype new roman color:rgb>;<spanpalatino linotype new roman color:rgb font-weight:bold>location<spanpalatino linotype new roman color:rgb>=<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman color:rgb font-weight:bold>50x<spanpalatino linotype new roman color:rgb font-weight:bold>.html<spanpalatino linotype new roman>{<spanpalatino linotype new roman>root<spanpalatino linotype new roman>html<spanpalatino linotype new roman>;<spanpalatino linotype new roman>}<spanpalatino linotype new roman color:rgb font-weight:bold>location<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman color:rgb font-weight:bold>static<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>{<spanpalatino linotype new roman>alias<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>home<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>work<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>src<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>sites<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>testdjango1<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>testdjango<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>collectedstatic<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>;<spanpalatino linotype new roman>index<spanpalatino linotype new roman>index<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>html<spanpalatino linotype new roman>index<spanpalatino linotype new roman color:rgb>.<spanpalatino linotype new roman>htm<spanpalatino linotype new roman>;<spanpalatino linotype new roman>}<spanpalatino linotype new roman color:rgb font-weight:bold>location<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman color:rgb font-weight:bold>media<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>{<spanpalatino linotype new roman>alias<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>home<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>work<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>src<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>sites<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>testdjango1<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>testdjango<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>public<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>media<spanpalatino linotype new roman color:rgb>/<spanpalatino linotype new roman>;<spanpalatino linotype new roman>}<spanpalatino linotype new roman>}</spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino></spanpalatino>
Nginx+uWSGI+Django的实现方式
重启Nginx服务器,以使Nginx的配置生效。
nginx -s reload
重启后检查Nginx日志是否有异常。
启动uWSGI服务器
cd /home/work/src/sites/testdjango1/testdjango/mysite uwsgi -x djangochina_socket.xml
检查日志 uwsgi.log 是否有异常发现。
访问服务
基于上面的假设你的域名是www.you.com
因此,我们访问 www.you.com,如果发现程序与 单独使用Django启动的程序一模一样时,就说明成功啦!
关闭服务的方法
将uWSGi进程杀死即可。
一些建议
uWSG配置文件的进程数,可以根据实际情况分配。不要开得太大,否则机器可能会内存耗用太高。一般来说,对于一个小社区来说,4个进程已经足够了。
一般情况下,可以编写一下 stop.sh 脚本 来关闭uWSGI。
以上就介绍了五步教你实现使用Nginx+uWSGI+Django方法部署Django程序,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software