Django+uWSGI+nginx 水深无声 2022-02-18 09:41 216阅读 0赞 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)之间进行交互。 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjU5NTAxMg_size_16_color_FFFFFF_t_70][] -------------------- **1.安装uWSGI** [root@syztoo syztoo]# pip3 install uwsgi -------------------- **2.使用uWSGI启动项目** [root@syztoo 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 ; ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjU5NTAxMg_size_16_color_FFFFFF_t_70 1][] -------------------- **3.使用socket替代端口** [root@syztoo 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文件如下: [root@syztoo 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 ; ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjU5NTAxMg_size_16_color_FFFFFF_t_70 2][] -------------------- **5. 使用.ini文件来配置uwsgi** [root@syztoo 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 [root@syztoo syztoo]# nohup uwsgi --ini syztoo.ini & [1] 5422 [root@syztoo syztoo]# nohup: ignoring input and appending output to ‘nohup.out’ [root@syztoo 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.项目目录下文件** [root@syztoo syztoo]# ls db.sqlite3 manage.py nohup.out static syztoo syztoo.ini syztoo.sock -------------------- [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjU5NTAxMg_size_16_color_FFFFFF_t_70]: /images/20220218/1bf4b5275b9842bbbc128c2669dd5d44.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjU5NTAxMg_size_16_color_FFFFFF_t_70 1]: /images/20220218/22a9427315bf4276a0c4f8db806c0be8.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjU5NTAxMg_size_16_color_FFFFFF_t_70 2]: /images/20220218/5d42e44a7c09424b896eaa2eb054d083.png
还没有评论,来说两句吧...