通过Nginx部署Django

编译安装Nginx

下载Nginx源码包进行编译,编译如下

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
./configure --prefix=/nginx \
--error-log-path=/nginx/log/error.log \
--http-log-path=/nginx/log/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/nginx/temp/client_temp \
--http-proxy-temp-path=/nginx/temp/proxy_temp \
--http-fastcgi-temp-path=/nginx/temp/fastcgi_temp \
--http-uwsgi-temp-path=/nginx/temp/uwsgi_temp \
--http-scgi-temp-path=/nginx/temp/scgi_temp \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-ipv6

make
make install

安装uwsgi

pip install uwsgi

测试uwsgi

测试脚本
cat test.py

1
2
3
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]

运行脚本
uwsgi --http :8000 --wsgi-file test.py
浏览器打开http://ip:8000

会出现
Hello World

Django 配置

我的Django目录在/opt/PoMo

1
2
[root@ruyi PoMo]# ls
db.sqlite3 manage.py PoMo pomo.ini PoMo.log requirement.txt robots statics templates upload utils

常用的选项

  • http : 协议类型和端口号
  • processes : 开启的进程数量
  • workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
  • chdir : 指定运行目录(chdir to specified directory before apps loading)
  • wsgi-file : 载入wsgi-file(load .wsgi file)
  • stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
  • threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
  • master : 允许主进程存在(enable master process)
  • daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
  • pidfile : 指定pid文件的位置,记录主进程的pid号。
  • vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)

pomo.ini文件内容

[root@ruyi PoMo]# cat pomo.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[uwsgi]

socket = :8100 # 端口号
uid=nginx # 运行的用户
gid=nginx # 运行的用户组
thunder-lock=true
chdir = /opt/PoMo/ # 运行目录
module = PoMo.wsgi # Django的wgsi module

master = true
processes = 4 # 进程数

vacuum = true

buffer-size = 32768

socket = /opt/PoMo/PoMo/PoMo.sock # socket 路径
pidfile = /opt/PoMo/PoMo/PoMo.pid # pid路径


chmod-socket = 664 # socket 文件权限
daemonize=/opt/PoMo/PoMo.log # 日志文件

使用配置文件启动
uwsgi --ini pomo.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
*** Starting uWSGI 2.0.17 (64bit) on [Wed Feb 20 16:38:56 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-28) on 19 June 2018 08:38:38
os: Linux-3.10.0-693.11.1.el7.x86_64 #1 SMP Mon Dec 4 23:52:40 UTC 2017
nodename: ruyi.novalocal
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /opt/PoMo
writing pidfile to /opt/PoMo/PoMo/PoMo.pid
detected binary path: /usr/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
setgid() to 995
setuid() to 998
chdir() to /opt/PoMo/
your processes number limit is 3893
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: enabled
uwsgi socket 0 bound to TCP address :8100 fd 3
uwsgi socket 1 bound to UNIX address /opt/PoMo/PoMo/PoMo.sock fd 4
Python version: 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x185f670
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 507880 bytes (495 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x185f670 pid: 14047 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 14047)
spawned uWSGI worker 1 (pid: 14052, cores: 1)
spawned uWSGI worker 2 (pid: 14053, cores: 1)
spawned uWSGI worker 3 (pid: 14054, cores: 1)
spawned uWSGI worker 4 (pid: 14055, cores: 1)

配置Nginx,内容如下

我这里用的https, 证书

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
upstream pomo{
server 127.0.0.1:8100;
}
server {
listen 80;
server_name XXXXXXX;

#charset koi8-r;
access_log /var/log/nginx/pomo_access.log main;
error_log /var/log/nginx/pomo_error.log;
client_max_body_size 75M;
location / {
uwsgi_pass pomo;
include /etc/nginx/uwsgi_params;
uwsgi_read_timeout 2;
}
location /robots.txt {
alias /opt/PoMo/robots/robots.txt;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /opt/PoMo/statics;
}
location /upload {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /opt/PoMo/upload;
}
}

坚持原创技术分享,您的支持将鼓励我继续创作!