0%

ubantu下使用nginx、uwsgi搭建django环境(一)

  关于环境搭建的具体过程,网上已经有很详尽的文章,不同版本环境搭建也有区别,这里就不再赘述。

  主要分享下对环境搭建的理解以及踩过的坑点:P

  不像 windows 使用 ide 一键启动环境,linux 下的部署更加地复杂,同时排错也很困难

基础知识

Django 首要的部署平台是WSGI,它是Python Web 服务器和应用的标准。

  那么什么是 WSGI 呢,Web服务器网关接口(Web Server Gateway Interface),简单来说,web 服务器向外界提供文件(css,html 这些),但是它无法直接和 Django 应用通讯,WSGI 就是干这个事儿的。

  Django 官方文档提供了 ApacheGunnicornuWSGI三种 WSGI 服务器的入门文档。我采用的方式是 uWSGI 创建一个 Unix socket,通过 WSGI 协议提供响应到 Web 服务器,最后的层次大致如下:

1
the web client <-> the web server <-> the socket <-> uwsgi <-> Django

ngnix 配置

nginx 接收用户请求,选择直接从文件系统中提供文件还是转向通过 socket 和 uwsgi 进行交互:

1
2
3
4
5
6
7
8
9
location /static {
alias /home/str3am/django_blog/static; # your Django project's static files - amend as required
}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass unix:///home/str3am/django_blog/django_blog.sock;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}

Django 项目中 static 里的内容可以通过命令 python manage.py collectstatic 部署。 uwsgi_pass 设置和 WSGI 服务器交互的地址和端口,sock 文件会在之后 uswgi 中配置。

完整配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# nginx.conf
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;

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

location /static {
alias /home/str3am/django_blog/static; # your Django project's static files - amend as required
}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass unix:///home/str3am/django_blog/django_blog.sock;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}

uwsgi 配置

uwsgi 需要配置项目地址 chdir,socket地址,wsgi 文件地址 wsgi-file,及其他设置如 socket 权限 chmod-socket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# mysite.ini
[uwsgi]
# the socket (use the full path to be safe
socket = /home/str3am/django_blog/django_blog.sock
# the base directory (full path)
chdir = /home/str3am/django_blog
# the location of wsgi-file
wsgi-file = /home/str3am/django_blog/django_blog/wsgi.py

processes = 2
threads = 4

chmod-socket = 666

vacuum = true

这里如果用 module 不行可以换用 wsgi-file

1
2
3
4
5
6
7
8
9
10
11
12
uwsgi --chdir=/path/to/your/project \
--module=mysite.wsgi:application \
--env DJANGO_SETTINGS_MODULE=mysite.settings \
--master --pidfile=/tmp/project-master.pid \
--socket=127.0.0.1:49152 \ # can also be a file
--processes=5 \ # number of worker processes
--uid=1000 --gid=2000 \ # if root, uwsgi can drop privileges
--harakiri=20 \ # respawn processes taking more than 20 seconds
--max-requests=5000 \ # respawn processes after serving 5000 requests
--vacuum \ # clear environment on exit
--home=/path/to/virtual/env \ # optional path to a virtualenv
--daemonize=/var/log/uwsgi/yourproject.log # background the process

trick

  • 更新或安装提示被占用

sudo rm /var/lib/dpkg/lock

  • 排错可以查看 ngnix 错误日志 /var/nginx/error.log

  • 更换 pip 源:User-Guide

1
2
3
4
[global] 
index-url = http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com

需设置 trusted-host ,否则会报错

1
2
3
Linux or Mac: ~/.pip/pip.conf
Windows: %appdata%\pip\pip.ini
虚拟环境:%env%\pip\pip.ini(windows)

参考链接:

使用uWSGI和nginx来设置Django和你的web服务器

Django 1.82 如何使用WSGI 部署