先说说什么是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.
InsecurePlatformWarning
Downloading uwsgi-2.0.13.1.tar.gz (784kB)
100% |████████████████████████████████| 788kB 1.4MB/s
Building wheels for collected packages: uwsgi
Running setup.py bdist_wheel for uwsgi ... done
Stored 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 socket
server 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 on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8; # max upload size
client_max_body_size 75M; # adjust to taste # Django media
location /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

使用uWSGI部署django项目的更多相关文章

  1. 使用Nginx+uWSGI部署Django项目

    1.linux安装python3环境 参考链接:https://www.cnblogs.com/zzqit/p/10087680.html 2.安装uwsgi pip3 install uwsgi l ...

  2. nginx + uwsgi 部署django项目

    因项目需求,需要部署django项目,这里是基础的nginx配合uwsgi部署django,后续会采用docker部署的方式 环境: centos7 python3.5.4 django2.1.4 u ...

  3. nginx+uwsgi部署Django项目到Ubuntu服务器全过程,以及那些坑!!!

    前言:自己在windows上用PyCharm编写的Django项目,编写完后在windows上运行一点问题都没有,但是部署到服务器上时却Bug百出.百度,CSDN,sf,各种搜索寻求解决方案在历时3天 ...

  4. linux上使用nginx、uwsgi部署django项目

    参考:CentOS7下部署Django项目详细操作步骤 注意事项: 在虚拟环境中操作,虚拟环境中安装nginx.uwsgi,虚拟环境外需安装uwsgi -- 临时关闭防火墙:systemctl sto ...

  5. nginx+uwsgi部署django项目

    1.django项目部署前需要生成admin的静态资源文件 (1)生成admin的静态资源文件 # 关闭debug模型 DEBUG = False # 允许所有域名访问 ALLOWED_HOSTS = ...

  6. Nginx + uWSGI 部署Django 项目,并实现负载均衡

    一.uWSGI服务器 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI ...

  7. CENTOS7 使用 Nginx + Uwsgi 部署 Django 项目

    写在前面的话 最近总是见到有新学 Django 的朋友在部署自己的项目到 Linux 上面的时候运行不起来,所以就动手写了这篇博客. 对于不会搭建 Python 3 环境的朋友可以参考前面的博客[CE ...

  8. ubuntu服务器上用Nginx和Uwsgi部署django项目

    开发环境:ubuntu系统,python3环境 django项目目录: fast_foot 为项目根目录,app为项目应用 现在,我们登陆远程服务器 安装Nginx 安装好了,我们看一下nginx的配 ...

  9. Ubuntu+uWSGI部署Django项目【鸿篇巨制,事无巨细】

    背景 任务: 视频翻译项目需要在两个服务器上进行通信(国内&海外的阿里服务器). 因为python是主语言,选用了Django 来快速部署API. 注:Django中文文档:https://d ...

随机推荐

  1. Java 基础【07】线程同步锁的选择

    在需要线程同步的时候如何选择合适的线程锁? 例:选择可以存入到常量池当中的对象,String对象等 public class SyncTest { private String name = &quo ...

  2. Web Audio API 实现音频可视化

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 一转眼就已经有三个月没写博客了,毕业季事情确实多,现在也终于完全毕业了,博客还是不能落下.偶尔还是要写一下. 玩HTML5的Audio A ...

  3. 备忘:maven 错误信息: Plugin execution not covered by lifecycle configuration

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  4. 维特比算法(Viterbi Algorithm)

      寻找最可能的隐藏状态序列(Finding most probable sequence of hidden states) 对于一个特殊的隐马尔科夫模型(HMM)及一个相应的观察序列,我们常常希望 ...

  5. Oracle学习——安装系列

    简介:Oracle Database,又名Oracle RDBMS,或简称Oracle.是甲骨文公司的一款关系数据库管理系统.它是在数据库领域一直处于领先地位的产品.可以说Oracle数据库系统是目前 ...

  6. C# 面试的“区别”

    1.静态变量与非静态变量的区别 静态变量--static.直接类名+变量名.静态函数里不能用非静态变量. 访问同一类中所有实例同一静态变量都是同一值.非静态变量则不是. 2.const与readonl ...

  7. 十天冲刺---Day4

    站立式会议 站立式会议内容总结: git上Issues新增内容: 燃尽图 照片 队伍度过了一次难关,刚开始学习的难关. 但还是存在进度较慢的问题. 队伍内相互理解是关键. 要时刻了解队友的情况.

  8. 50ms延时程序

    12MHz晶振 一个机器周期2us, DEL:  MOV  R7,#200D DEL1: MOV R6,#125 DEL2: DJNZ R6,DEL2 ;125*2=250us DJNZ  R7,DE ...

  9. Shell脚本编程中的几个问题

    条件语句 正确的写法: if [ $1 = "-f" ] #注意这里,前后方括号和中间的内容之间必须有空格! then commands fi 错误的写法: if [$1 == & ...

  10. 【BZOJ 2693】jzptab

    莫名其妙地又卡在long long上了,我果然又在同一个地方犯逗. 在“在路上,同梦行”群里闹了个大笑话QAQ #include<cstdio> #include<cstring&g ...