django + uwsgi 配置

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 进行调试错误。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值