django + uwsgi 配置

转载 2015年07月07日 15:17:42

http://segmentfault.com/q/1010000002523354/a-1020000002526934

最好通过python pip安装uwsgi.

$sudo apt-get install python-dev
$sudo apt-get install python-pip
$sudo pip install pip --upgrade
$sudo apt-get install libpcre3 libpcre3-dev
$sudo apt-get install zlib1g-dev
$sudo apt-get install nginx-full

如果安装版本错误,先卸载:

$pip uninstall uwsgi
$sudo apt-get remove uwsgi

python 版本最好是python 2.7.*
pip的版本应该是最新版本。
查看pip 版本:

$pip --version
pip 6.0.7 from /usr/local/lib/python2.7/dist-packages (python 2.7)

接下来安装uwsgi。

$sudo pip install uwsgi

输出配置:

################# uWSGI configuration #################
pcre = True
kernel = Linux
malloc = libc
execinfo = False
ifaddrs = True
ssl = False
zlib = True
locking = pthread_mutex
plugin_dir = .
timer = timerfd
yaml = embedded
json = False
filemonitor = inotify
routing = True
debug = False
capabilities = False
xml = expat
event = epoll
############## end of uWSGI configuration #############

安装成功后看成uwsgi版本:

$uwsgi --version
2.0.9

这样就确保uwsgi的版本是最新版本了。

举例:
django进行配置:

$django-admin startproject hello
$python manage.py syncdb
$python manage.py runserver 0.0.0.0:8000

如果能够正常访问,那么可以测试uwsgi.
这里要通过django的wsgi启动,wsgi.py 在hello目录下面。

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
$uwsgi --http :8000 --module hello.wsgi

显示出itworks!

配置django static目录,
在django settings,

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

然后运行

$python manage.py collectstatic

接下来进行配置nginx。
启动nginx:

$sudo /etc/init.d/nginx start

首先要确保nginx配置路径下面有uwsgi_params
地址:https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

$cat /etc/nginx/uwsgi_params

uwsgi_param QUERY_STRING        $query_string;
uwsgi_param REQUEST_METHOD      $request_method;
uwsgi_param CONTENT_TYPE        $content_type;
uwsgi_param CONTENT_LENGTH      $content_length;

uwsgi_param REQUEST_URI     $request_uri;
uwsgi_param PATH_INFO       $document_uri;
uwsgi_param DOCUMENT_ROOT       $document_root;
uwsgi_param SERVER_PROTOCOL     $server_protocol;
uwsgi_param UWSGI_SCHEME        $scheme;

uwsgi_param REMOTE_ADDR     $remote_addr;
uwsgi_param REMOTE_PORT     $remote_port;
uwsgi_param SERVER_PORT     $server_port;
uwsgi_param SERVER_NAME     $server_name;

在工程目录下新建一个mysite.conf.
nginx配置文件:

server {
# the port your site will be served on
listen      8000;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN
charset     utf-8;

access_log /root/project/hello/access_log;
error_log  /root/project/hello/error_log;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
#location /media  {
#    alias /to/your/mysite/media;  # your Django project's media files - amend as required
#}

location /static {

alias /root/project/hello/static; # your Django project's static files - amend as required
}

# Finally, send all non-media requests to the Django server.
location / {

    uwsgi_pass 127.0.0.1:3400;
    include    /etc/nginx/uwsgi_params; # the uwsgi_params file you installed

}

}

注意,由于django settings 里面会配置STATIC_URL

这样在nginx里面只能设置成,

location /static {

alias /root/project/hello/static; # your Django project's static files - amend as required
}

TIPS:

如果静态文件目录用户权限是root

drwsr-xr-x 3 root root  4096  21 00:13 static/  

则需要更改nginx.conf,添加

user root;  

通过软连接:

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

重启nginx

$sudo /etc/init.d/nginx restart

接下来配置uwsgi

新建一个hello_uwsgi.ini文件。

    # mysite_uwsgi.ini file
    [uwsgi]

    socket = 127.0.0.1:3400
    # Django-related settings
    # the django project directory (full path)
    chdir           = /root/project/hello
    # Django's wsgi file
    module          = hello.wsgi

    # process-related settings
    # master
    master          = true
    # maximum number of worker processes
    processes       = 2

    threads = 2
    max-requests = 6000

    # ... with appropriate permissions - may be needed
    chmod-socket    = 664
    # clear environment on exit
    vacuum          = true

启动uwsgi

uwsgi --ini hello_uwsgi.ini

正常输出:

[uWSGI] getting INI configuration from hello_uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Sat Jan 31 19:27:23 2015] ***
compiled with version: 4.8.2 on 31 January 2015 19:21:57
os: Linux-2.6.32-042stab092.2 #1 SMP Tue Jul 8 10:35:55 MSK 2014
nodename: localhost.localdomain
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /root/project/hello
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /root/project/hello
your processes number limit is 175957
your memory page size is 4096 bytes
detected max file descriptor number: 4096
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:3400 fd 3
Python version: 2.7.6 (default, Mar 22 2014, 23:03:41)  [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1dc6ef0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 218280 bytes (213 KB) for 2 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x1dc6ef0 pid: 21844 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 21844)
spawned uWSGI worker 1 (pid: 21845, cores: 1)
spawned uWSGI worker 2 (pid: 21846, cores: 1)

如何设置uwsgi后台运行:
需要在mysite_uwsgi.ini配置文件中添加

daemonize = /root/project/hello/uwsgi.log

这样就会吧日志打印到uwsgi.log中。

通过查 nginx 的access_log 和 error_log 进行调试错误。


uWSGI+django+nginx的工作原理流程与部署历程

uWSGI+django+nginx工作原理流程及部署过程 django 一个基于python的开源web框架 uWSGI 一个基于自有的uwsgi协议、wsgi协议和http服务协议的web网关 n...

解决nginx+uwsgi部署Django的所有问题

最近,自己暑假写的小项目也算完毕了,想着投放到自己云服务器上,本来以为只要打开端口运行python3 manager runserver 0.0.0.0:80就搞定了,最后才知道这只适用于Django...

【uWSGI】实战之Django配置经验

uWSGI 是应用服务器,实现了uwsgi, wsgi等协议,可以运行wsgi 应用 uwsgi 是协议名 Django配置下面是自己经常用的一个配置模板,基于1.9以上的版本使用的, 主要基于dja...

uWSGI+Nginx+Django安装和配置

WSGI是为python语言定义的通用网关接口,它承担python web框架(django、flask、web.py等)和web服务器(nginx、apache、lighttpd等)之间的中间层。 ...

python django+uwsgi+nginx 搭建步骤(血一般的教训)

安装Django      没有pip的童鞋可以看http://blog.csdn.net/imphp/article/details/38228619安装python包管理工具pip    先...

Django为什么用uWSGI+Nginx

这两天在用Django做个人博客网站,做起来非常顺手,已经在测试环境中完成了留言板的功能,想着在加上个管理博客和博客编辑的功能就大致可以发布出去了,然后就发现教程中有个服务器部署的环节uWSGI+Ng...

uWSGI的安装与配置(官网摘录)

■ 安装uwsgi: 作为一个严谨(其实就是有强迫症)的程序,为了确保正确安装这个东西,费了我好长时间。首先,查找包里的readme,发现没有安装提示。各种被墙,辗转良久,才查看到uwsgi的官方文档...

uwsgi配置详解

socket : 地址和端口号,例如:socket = 127.0.0.1:50000 processes : 开启的进程数量 workers : 开启的进程数量,等同于processes(官网的...
  • abcd1f2
  • abcd1f2
  • 2016年12月16日 15:14
  • 1529

nginx和uwsgi配置

1.nginx和uwsgi的关系nginx相当于是服务器,负责接收请求uwsgi是服务器和服务端应用程序的通信协议,规定了怎么把请求转发给应用程序和返回2个基本概念: 服务器(接收请求),应用程序(...

UWSGI配置文件---ini和xml示例

一   conf.ini文件: [uwsgi] http = $(HOSTNAME):9033 http-keepalive = 1 pythonpath = ../ module = service...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:django + uwsgi 配置
举报原因:
原因补充:

(最多只允许输入30个字)