天天看點

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 . from /usr/local/lib/python2./dist-packages (python )
           

接下來安裝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 .:
           

如果能夠正常通路,那麼可以測試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 : --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      ;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN
charset     utf-;

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

# max upload size
client_max_body_size ;   # 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 ;
    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  root root    月   : 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 
your memory page size is  bytes
detected max file descriptor number: 
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket  bound to TCP address : fd 
Python version:  (default, Mar  , ::)  [GCC ]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 
your server socket listen backlog is limited to  connections
your mercy for graceful operations on workers is  seconds
mapped  bytes ( KB) for  cores
*** Operational MODE: preforking ***
WSGI app  (mountpoint='') ready in  seconds on interpreter  pid:  (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: )
spawned uWSGI worker  (pid: , cores: )
spawned uWSGI worker  (pid: , cores: )
                

如何設定uwsgi背景運作:

需要在

mysite_uwsgi.ini

配置檔案中添加

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

這樣就會吧日志列印到uwsgi.log中。

通過查

nginx

的access_log 和 error_log 進行調試錯誤。