Django 应用部署继续填坑

2017-05-18

实践证明,部署 Django 应用最方便的发行版是 Ubuntu,我一直以为是 Debian,最近这次部署开了两台服务器对比,还是 Ubuntu 最方便,环境问题最少。

最早部署一个简单的 Django 应用只要让 Nginx 把非静态文件的所有请求传给 8001 端口,uwsgi 作为 Nginx 和 Django 服务的桥接,最近有仔细看了一边 uwsgi 的示例文档,uwsgi 是推荐用 sock 套接字文件来传递,而且也大部分使用 ini 作为配置文件,uwsgi 的启动命令是 uwsgi --ini dangann/conf/production/uwsgi.ini .

也贴上我的 Nginx 配置和 uwsgi.ini 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
upstream django {
server unix:///var/log/dangann/dangann.sock; # for a file socket
}

server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name dangann.com; # substitute your machine's IP address or FQDN
charset utf-8;

# Let Encrypt
#ssl on;
#ssl_certificate /etc/letsencrypt/live/dangann.com/fullchain.pem;
#ssl_certificate_key /etc/letsencrypt/live/dangann.com/privkey.pem;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_prefer_server_ciphers on;
#ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS";
#allow all;

# gzip
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript image/jpeg image/png;
gzip_disable "MSIE [1-6]\.";

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

# logs
access_log /root/dangann_logs/access.log;
error_log /root/dangann_logs/error.log;

# Django media
location /media {
alias /root/dangann/media; # your Django project's media files - amend as required
}

location /static {
alias /root/dangann/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 /root/dangann/conf/production/uwsgi_params; # the uwsgi_params file you installed
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir = /root/dangann/
# Django's wsgi file
module = dangann.wsgi

# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# the socket (use the full path to be safe
socket = /var/log/dangann/dangann.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
# clear environment on exit
vacuum = true

# use python thread
enable-threads = true

# set request block time
harakiri = 600
# body size
# buffer-size
buffer-size=32768

# uwsgi.log
daemonize=/root/dangann_logs/uwsgi.log