使用uWSGI部署django项目

先说说什么是uWSGI吧,他是实现了WSGI协议、uwsgi、http等协议的一个web服务器,那什么是WSGI呢?

WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx)与应用服务器(如uWSGI服务器)通信的一种规范(协议)。

还有一种wsgi,uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information)。

 

部署步骤:

1. 安装uWSGI

(lofter) ➜  dj_test git:(master) pip install uwsgi
Collecting uwsgi
/home/wang/.virtualenvs/lofter/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.SNIMissingWarning
/home/wang/.virtualenvs/lofter/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.InsecurePlatformWarningDownloading uwsgi-2.0.13.1.tar.gz (784kB)100% |████████████████████████████████| 788kB 1.4MB/s 
Building wheels for collected packages: uwsgiRunning setup.py bdist_wheel for uwsgi ... doneStored in directory: /home/wang/.cache/pip/wheels/01/e4/de/0b2bbeba234858bd780924d03031a4e817119aafb0cfc4c79e
Successfully built uwsgi
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.13.1

以上命令是安装目前稳定版本的,也可以安装LTS版本

# Or install LTS (long term support).
$ pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz

  

2. 安装之后进行简单的测试:

2.1 创建一个test.py文件:

# test.py
def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello World"] # python3#return ["Hello World"] # python2

2.2 启动uWSGI服务器:

uwsgi --http :8000 --wsgi-file test.py

意思是使用8000端口启动这个文件,效果如下:

 

(lofter) ➜  dj_test git:(master) uwsgi --http :8000 --wsgi-file test.py
*** Starting uWSGI 2.0.12 (64bit) on [Mon May 23 21:15:56 2016] ***
compiled with version: 4.8.4 on 08 April 2016 10:44:49
os: Linux-3.13.0-86-generic #130-Ubuntu SMP Mon Apr 18 18:27:15 UTC 2016
nodename: wang-N46VZ
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /home/wang/Workspace/Git/show-me-the-code/dj_test
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 62795
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 5539)
uwsgi socket 0 bound to TCP address 127.0.0.1:47320 (port auto-assigned) fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x21b8ff0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x21b8ff0 pid: 5538 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 5538, cores: 1)

2.3 访问 localhost:8000

显示 hello world(linux截图太麻烦了,见谅)

这就说明uWSGI启动成功了~可以设置uWSGI启动django了

 

3. 使用uWSGI启动django项目

(lofter) ➜  dj_test git:(master) uwsgi --http :8000 --file dj_test/wsgi.py                                                                                              
*** Starting uWSGI 2.0.12 (64bit) on [Mon May 23 21:19:35 2016] ***
compiled with version: 4.8.4 on 08 April 2016 10:44:49
os: Linux-3.13.0-86-generic #130-Ubuntu SMP Mon Apr 18 18:27:15 UTC 2016
nodename: wang-N46VZ
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /home/wang/Workspace/Git/show-me-the-code/dj_test
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 62795
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 5649)
uwsgi socket 0 bound to TCP address 127.0.0.1:60536 (port auto-assigned) fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x2263ff0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x2263ff0 pid: 5648 (default app)
*** uWSGI is running in multiple interpreter mode ***

访问网址,就可以看见你的项目了~

常用命令:

uwsgi --chdir=/path/to/your/project \--module=mysite.wsgi:application \--env DJANGO_SETTINGS_MODULE=mysite.settings \--master --pidfile=/tmp/project-master.pid \--socket=127.0.0.1:49152 \      # can also be a file--processes=5 \                 # number of worker processes--uid=1000 --gid=2000 \         # if root, uwsgi can drop privileges--harakiri=20 \                 # respawn processes taking more than 20 seconds--max-requests=5000 \           # respawn processes after serving 5000 requests--vacuum \                      # clear environment on exit--home=/path/to/virtual/env \   # optional path to a virtualenv--daemonize=/var/log/uwsgi/yourproject.log      # background the process  

 

 

实际上到这里就算部署成功了,不过现实中一般使用nginx和uWSGI配合使用,使用nginx处理/static/以及/media/的请求,其他的交给uWSGI处理。

(下面基本是翻译,而且还没有翻译完

1. 安装nginx并配置nginx

sudo apt-get install nginx
sudo /etc/init.d/nginx start    # start nginx

配置文件(需要去看一下nginx简单配置)

# mysite_nginx.conf# the upstream component nginx needs to connect to
upstream django {# server unix:///path/to/your/mysite/mysite.sock; # for a file socketserver 127.0.0.1:8001; # for a web port socket (we'll use this first)
}# configuration of the server
server {# the port your site will be served onlisten      8000;# the domain name it will serve forserver_name .example.com; # substitute your machine's IP address or FQDNcharset     utf-8;# max upload sizeclient_max_body_size 75M;   # adjust to taste# Django medialocation /media  {alias /path/to/your/mysite/media;  # your Django project's media files - amend as required}location /static {alias /path/to/your/mysite/static; # your Django project's static files - amend as required}# Finally, send all non-media requests to the Django server.location / {uwsgi_pass  django;include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed}
}

做个链接使得nginx可以访问到配置文件

sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

2. 部署静态文件

python manage.py collectstatic
STATIC_ROOT一定要设置好了

3. 重启nginx(第一次部署需要重启,以后进行了第二步之后只需要重启uWSGI就可以了)

sudo /etc/init.d/nginx restart

4. 启动

uwsgi --socket :8001 --wsgi-file test.py

5. 添加uWSGI配置文件

# mysite_uwsgi.ini file
[uwsgi]# Django-related settings
# the base directory (full path)
chdir           = /path/to/your/project
# Django's wsgi file
module          = project.wsgi
# the virtualenv (full path)
home            = /path/to/virtualenv# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

按照配置文件启动

uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

6. 使用两者共同拉起项目

uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664

  

If that doesn’t work

Check your nginx error log(/var/log/nginx/error.log). If you see something like:

connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
denied)

then probably you need to manage the permissions on the socket so that nginx is allowed to use it.

Try:

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)

or:

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)

You may also have to add your user to nginx’s group (which is probably www-data), or vice-versa, so that nginx can read and write to your socket properly.

It’s worth keeping the output of the nginx log running in a terminal window so you can easily refer to it while troubleshooting.

 

参考:

http://baike.baidu.com/link?url=ClozvG5bxABaSkxztwURPZAOrySGwwBNMJEiPVOWfv7dPvJwEw_ZYPK3mUn0miC5_YNnHFG27tKvv6B3wktL1K

https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/uwsgi/

http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

转载于:https://www.cnblogs.com/wswang/p/5521566.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/395928.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

8 包含min函数的栈

0 引言 题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数。在该栈中,调用min、push及pop的时间复杂度都是O(1). 1 抽象问题具体化 2 具体问题抽象分析 需要解决的两个主要问题如下。 &#x…

《Adobe Illustrator大师班:经典作品与完美技巧赏析》—Svetlana Makarova

本节书摘来自异步社区《Adobe Illustrator大师班:经典作品与完美技巧赏析》一书中的Svetlana Makarova,作者【英】Sharon Milne,更多章节内容可以访问云栖社区“异步社区”公众号查看。 Svetlana MakarovaAdobe Illustrator大师班:经典作品与…

navicat无法连接远程mysql数据库_navicat无法远程连接mysql的解决方法

近日在Ubuntu上安装了一个 MySQL 5.0,因为使用 phpMyAdmin 还必须安装 PHP,所以打算直接使用远程管理工具Navicat for MySQL 来连接。在 Ubuntu 中通过 mysql 命令行创建好一个数据表并分配了权限:代码如下:GRANT ALL ON testdb.* TO usera I…

有关软件测试的证书,软件测试证书有用吗

要想知道证书有什么用,我们就要详细了解软件评测师考试,以及拿到证书的价值。那么下面和小编来看看这篇软件测试证书有用吗,一定会有收获。一、证书考试软件评测师考试是全国计算机技术与软件技术资格考试的一个中级考试。考试不规定学历和资…

计算机科学导论第五版_五月份将开始提供438项免费在线编程和计算机科学课程

计算机科学导论第五版Five years ago, universities like MIT and Stanford first opened up free online courses to the public. Today, more than 700 schools around the world have created thousands of free online courses.五年前,麻省理工学院和斯坦福大学…

python D29 socketserver以及FTB

一、socketserver 基于tcp协议下的socket只能和一个客户端通信,如果用socketserver可以实现和多个客户端通信。 他是在socket的基础上进行封装,也就是说底层还是调用的socket,在py2.7里面叫做SocketServer也就是大写了两个S,在py3…

计算机节电模式不能打开,电脑进入节电模式打不开怎么办

大家好,我是时间财富网智能客服时间君,上述问题将由我为大家进行解答。电脑进入节电模式打不开的原因及解决方法如下:1、显示器和显卡接触不良解决办法:检查显示器和显卡的连接是否正确,接触是否良好;2、显…

sphinx mysql存储引擎_基于Sphinx+MySQL的千万级数据全文检索(搜索引擎)架构设计...

Sphinx,单一索引最大可包含1亿条记录,在1千万条记录情况下的查询速度为0.x秒(毫秒级)。Sphinx创建索引的速度为:创建100万条记录的索引只需3~4分钟,创建1000万条记录的索引可以在50分钟内完成,而只包含最新…

《第一桶金怎么赚——淘宝开店创业致富一册通》一一1.1 创业者需具备的素质...

本节书摘来自异步社区出版社《第一桶金怎么赚——淘宝开店创业致富一册通》一书中的第1章,第1.1节,作者:葛存山,更多章节内容可以访问云栖社区“异步社区”公众号查看。 1.1 创业者需具备的素质 第一桶金怎么赚——淘宝开店创业致…

4-1 线程安全性-原子性-atomic-1

转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/10026627.html

构建了我的第一个React Native应用程序之后,我现在确信这是未来。

by Taylor Milliman泰勒米利曼(Taylor Milliman) 构建了我的第一个React Native应用程序之后,我现在确信这是未来。 (After building my first React Native app, I’m now convinced it’s the future.) After a few weeks of playing around with React Native, …

delphi7 提示注册过期问题

很同情你得经过~ 因为我以前也是经常遇见这个问题~就和你说得一样~ 后来~ 我发现 下载使用的Delphi 7只能使用一个注册码,那就是:6AMD-PKG68E-DB8PP7-9SFE 3QH-9QW所以,你先把C:\Documents and Settings\Administrator\.borland文件夹下的两个文件删除然后用 Progra…

计算机开机引导的结果是,电脑开机显示引导媒体是怎么回事

电脑开机显示引导媒体是怎么回事分类:数据恢复常见问题|最后更新:2020年4月9日开机显示重新启动并选择适当的引导设备或插入1.如果主机上接有可移动存储介质(如光盘、移动硬盘、U盘等),将其拔掉,然后重启。2.如果仍然这样,进入主板设置中,依次检测以下几…

《操作系统真象还原》——0.23 操作系统是如何识别文件系统的

本节书摘来自异步社区《操作系统真象还原》一书中的第0章,第0.23节,作者:郑钢著,更多章节内容可以访问云栖社区“异步社区”公众号查看 0.23 操作系统是如何识别文件系统的 我们知道,一个硬盘上可以有很多分区&#xf…

mysql怎样修改my ini_mysql修改my.ini报错怎么办

mysql修改my.ini报错的解决办法:首先将mysql默认编码改成utf8mb4,并修改【my.ini】配置;然后修改变量,并检查是否设置成功即可。更多相关免费学习推荐:mysql教程(视频)mysql修改my.ini报错的解决办法:将mys…

golang---map类型

map 类似其它语言中的哈希表或字典,以key-value形式存储数据key必须是支持或!比较运算的类型,不可以是函数、map或sliceMap查找比线性搜索快很多,但比使用索引访问数据的类型慢100倍 Map使用make()创建,支持:这种简写方式 make([k…

易语言程序应用程序错误退出_为什么我退出Google并构建了一个向孩子们教授个人理财的应用程序

易语言程序应用程序错误退出Many of my friends thought I was crazy to leave a great position at Google to help parents and kids learn about money. Maybe they’re right.我的许多朋友都认为我为在谷歌上任职以帮助父母和孩子了解金钱而感到疯狂。 也许他们是对的。 B…

蓝叠 正在检查服务器最新,Windows update一直停留在正在检查更新

Windows update一直停留在正在检查更新,为什么啊?一、查看相关服务是否开始:请您根据以下步骤,确认windows update & BITS服务设置1. 按下WinR键输入“services.msc”(输入时不要打引号),并按下回车。如果此时弹出…

spring-DataSource

spring支持的dataSource有好多, 如:自带的org.springframework.jdbc.datasource.DriverManagerDataSource ibatis、c3p0、JDBC、hibernate等等; 首先看第一种,使用自带的datasource: 1、项目结构 提示:spri…

《Nmap渗透测试指南》—第7章7.8节后台打印机服务漏洞

本节书摘来自异步社区《Nmap渗透测试指南》一书中的第7章7.8节后台打印机服务漏洞,作者 商广明,更多章节内容可以访问云栖社区“异步社区”公众号查看。 7.8 后台打印机服务漏洞表7.8所示为本章节所需Nmap命令表,表中加粗命令为本小节所需命令——后台打…