Total Pageviews

Monday, 9 December 2013

nginx + fastcgi 部署django项目


安装依赖

安装 flup:
pip install flup

编辑 settings.py:
TEMPLATE_DIRS = (
    '/var/djblog/templates',  # 这里要是绝对路径
)
STATIC_ROOT = '/var/djblog/static'

(djblog这个程序使用的是它自己的local_settings.py,所以需要编辑的是local_settings.py而非

settings.py,其他的django项目一般需编辑settings.py)

编辑wsgi.py:
#!/usr/bin/env python
# coding: utf-8
import os
import sys

sys.path.append('/var/djblog/')  # 项目目录的绝对路径,此行是加上去的。

os.environ['DJANGO_SETTINGS_MODULE'] = 'djblog.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

编辑nginx.conf:
server {
listen 81;
server_name dj.brite.biz;
root /var/djblog;
location / {
        # host and port to fastcgi server
        fastcgi_pass 127.0.0.1:9081;  # host:port
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;
        }
    location /static/ {  # STATIC_URL
        alias /var/djblog/static/;  # STATIC_ROOT
    }
}

以 fastcgi 的形式运行 django 项目(注意:这里的 host 及 port 要与上面配置的一样):
as3:/var/djblog# python manage.py runfcgi host=127.0.0.1 port=9081
as3:/var/djblog# /usr/local/nginx/sbin/nginx -s reload
终止 fastcgi 话,查看相关进程然后结束掉即可.

这样即可访问http://dj.brite.biz:81/,而这个http://as3.brite.biz:9003/是development server地址。
感觉使用 nginx + fastcgi 部署 django项目可靠多了;而用nginx + uwsgi部署 django项目,感觉效果并没有那么好,经常莫名其妙的打不开网站http://dj.brite.biz:81
---------------------------------------
这里以youflog为例

安装 flup:
pip install flup

其实settings.py和wsgi.py不必编辑,只需编辑nginx.conf:

server {
listen 81;
server_name yl.brite.biz;
root /root/youflog-0.3/youflog/;
location / {
        # host and port to fastcgi server
        fastcgi_pass 127.0.0.1:9082;  # host:port,注意要跟上面已用的9081端口不同
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;
        }
}

以 fastcgi 的形式运行django项目(注意:这里的 host 及 port 要与上面配置的一样):
(youflog-0.3)as3:~/youflog-0.3/youflog# python manage.py runfcgi host=127.0.0.1 port=9082
(youflog-0.3)as3:~/youflog-0.3/youflog# /usr/local/nginx/sbin/nginx -s reload
终止 fastcgi的话,查看相关进程然后结束掉即可.

这样即可访问http://yl.brite.biz:81/,而这个http://as3.brite.biz:10003/是development server地址。
感觉使用 nginx + fastcgi 部署 django项目可靠多了;而用nginx + uwsgi部署 django项目,感觉效果并没有那么好,经常莫名其妙的打不开网站http://yl.brite.biz:81

相关帖子:
http://briteming.blogspot.com/2013/12/virtualenvpythondjango123-youflog.html
http://briteming.blogspot.co.uk/2012/11/pythondjango-djblog.html