Nginx+uWSGI+Django部署

环境:ArchLinux + python2.7.10
首先pip安装uWSGI.#: pip install uwsgi
安装完成后测试以下是否成功安装:

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return ['hello world']

运行:

uwsgi --http :8000 --wsgi-file test.py

访问:localhost:8000 看到 hello world就表示成功啦~
the web client <-> uWSGI <-> Python

接下来把uWSGIDjango app连接起来:
首先确保Django app能正常的跑起来。
运行:uwsgi --http :8000 --module mysite.wsgi(mysite Django项目名称),Django app应该就正常跑起来了。
访问:localhost:8000测试一下。。。
这就连上django了~ the web client <-> uWSGI <-> Django
(这里有个小插曲,我使用了一个uwsgi配置文件。socket = 127.0.0.1:3031用的是socket而不是http。然后就报了一个莫名奇妙的错误 invalid request block size: 21573 (max 4096)...skip,为此折腾了大半天,汗。。。。。)

接下来需要把Nginx安装好。
- 然后到需要一个uwsgi_params文件。可以到这儿拷贝:https://github.com/nginx/nginx/blob/master/conf/uwsgi_params。置于项目的根目录下。
- 接下来为该项目建立一个Nginx的配置文件mysite_nginx.conf,置于/etc/nginx/sites-enabled/目录下,目录不存在可以新建一个,这个随意。。
- 编辑内容:

# 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 127.0.0.1; # 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.访问静态文件以测试~如果遇到403的话,很有可能是权限问题。首先确认Nginx是以哪个用户的身份运行的, `vim /etc/nginx/nginx.conf`查看user(nginx默认user被注释掉)。也可以修改user。或者是给当前nginx的user添加相应访问项目文件的权限。
这里还有一步:在重起Nginx之前需要对Django项目的静态文件进行整理,主要是将admin的静态文件复制到静态文件目录。在Django settings.py文件中添加下面一句:
`STATIC_ROOT = os.path.join(BASE_DIR, 'static/')`  这个static就由项目的静态文件目录决定啦~然后执行`python manage.py collectstatic`。

先用test.py来测试一下Nginx和uWSGI是否连上了。执行:

uwsgi --socket :8001 --wsgi-file test.py
访问localhost:8000测试一下~
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
把port改成unix socket连接,这样更快。修改mysite_nginx.conf文件:

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)

mysite.socket文件暂时还没有,先不管,就假定该项目下有这样的文件。
重起Nginx,然后运行uwsgi --socket mysite.sock --wsgi-file test.py。同样测试一下。
如果都没问题,那就剩最后一步了,连接Django app。运行:
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
这样应该就好了。但是这个用命令行的方式有些不方便,使用一个配置文件:

# 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

哈哈,大
主要参考着:http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html 灰常详细。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值