Django第二篇 Django+uWSGI+nginx
Django是一个开源的Web应用框架,由Python写成,基于MTV的框架模式。
uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口,可用于uWSGI和Django进行交互。
uwsgi是一种线路协议,用于uWSGI与其它网络服务器(比如nginx)之间进行交互。
1.安装uWSGI
[[email protected] syztoo]# pip3 install uwsgi
2.使用uWSGI启动项目
[[email protected] syztoo]# uwsgi --http :8000 --module syztoo.wsgi
*** Starting uWSGI 2.0.18 (64bit) on [Wed Apr 17 11:06:55 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 11 April 2019 11:41:12
os: Linux-3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019
nodename: syztoo
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /home/syztoo
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3882
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 5398)
uwsgi socket 0 bound to TCP address 127.0.0.1:37089 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.7.0 (default, Apr 10 2019, 21:48:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x29a29e0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x29a29e0 pid: 5397 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 5397, cores: 1)
测试能否正常访问,请求模型类似于:
the web client <-> uWSGI <-> Django ;
3.使用socket替代端口
[[email protected] syztoo]# uwsgi --socket syztoo.sock --module syztoo.wsgi --chmod-socket=666
*** Starting uWSGI 2.0.18 (64bit) on [Wed Apr 17 11:16:53 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 11 April 2019 11:41:12
os: Linux-3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019
nodename: syztoo
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /home/syztoo
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3882
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address syztoo.sock fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.7.0 (default, Apr 10 2019, 21:48:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xec5410
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0xec5410 pid: 5404 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 5404, cores: 1)
4.加入nginx
安装过程略,nginx.conf文件如下:
[[email protected] syztoo]# cat /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream django {
server unix:///home/syztoo/syztoo.sock;
}
server {
listen 80;
server_name localhost;
location /static {
alias /home/syztoo/static;
}
location / {
uwsgi_pass django;
include /usr/local/nginx/conf/uwsgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
启动nginx,测试能否正常访问,现在的请求模型类似于:
the web client <-> nginx <-> uWSGI <-> socket <-> Django ;
5. 使用.ini文件来配置uwsgi
[[email protected] syztoo]# cat syztoo.ini
[uwsgi]
chdir=/home/syztoo
module=syztoo.wsgi
master=true
processes=2
socket=/home/syztoo/syztoo.sock
chmod-socket=666
vacuum=true
[[email protected] syztoo]# nohup uwsgi --ini syztoo.ini &
[1] 5422
[[email protected] syztoo]# nohup: ignoring input and appending output to ‘nohup.out’
[[email protected] syztoo]# ps -ef | grep uwsgi
root 5422 5248 0 11:30 pts/0 00:00:00 uwsgi --ini syztoo.ini
root 5423 5422 0 11:30 pts/0 00:00:00 uwsgi --ini syztoo.ini
root 5424 5422 0 11:30 pts/0 00:00:00 uwsgi --ini syztoo.ini
root 5426 5248 0 11:30 pts/0 00:00:00 grep --color=auto uwsgi
6.项目目录下文件
[[email protected] syztoo]# ls
db.sqlite3 manage.py nohup.out static syztoo syztoo.ini syztoo.sock