[ Django ] Nginx+uWSGI+Django方法部署Django程序

@lianst  September 2, 2018

基础环境

对于以下基础环境的安装、配置,本文不在过多讲解。

  • Centos7
  • Python3
  • Nginx(Openresry)

安装uwsgi

就我个人而言,我对uwsgi的理解是:uwsgi相当于php中的php-fpm,是负责处理php脚本的网关。同样,uwsgi就是用来处理Python脚本的网关。

uwsgi的安装很简单,因为它是python的一个模块,所以我们可以使用pip命令来安装。

[root@django software]# pip install uwsgi # 安装
[root@django software]# uwsgi  # 运行uwsgi产看是否安装完成
*** Starting uWSGI 2.0.17 (64bit) on [Sun Sep  2 09:11:33 2018] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-28) on 21 May 2018 13:15:30
os: Linux-3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017
nodename: django
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /data/software
detected binary path: /usr/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 7860
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
The -s/--socket option is missing and stdin is not a socket.

命令启动uwsgi

首先进入示例项目中,新建一个测试页面test.py。

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

使用命令启动uwsgi。

[root@django wwwroot]# uwsgi --http 192.168.3.2:8899 --file test.py 
*** Starting uWSGI 2.0.17 (64bit) on [Sun Sep  2 09:14:52 2018] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-28) on 21 May 2018 13:15:30
os: Linux-3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017
nodename: django
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /data/wwwroot
detected binary path: /usr/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 7860
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on 192.168.3.2:8899 fd 4
spawned uWSGI http 1 (pid: 1163)
uwsgi socket 0 bound to TCP address 127.0.0.1:46416 (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: 2.7.5 (default, Apr 11 2018, 07:36:10)  [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 0x16dc060
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 0x16dc060 pid: 1162 (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: 1162, cores: 1)
[pid: 1162|app: 0|req: 1/1] 192.168.3.8 () {38 vars in 629 bytes} [Sun Sep  2 09:15:05 2018] GET / => generated 11 bytes in 0 msecs (HTTP/1.1 200) 1 headers in 44 bytes (0 switches on core 0)
[pid: 1162|app: 0|req: 2/2] 192.168.3.8 () {38 vars in 610 bytes} [Sun Sep  2 09:15:05 2018] GET /favicon.ico => generated 11 bytes in 0 msecs (HTTP/1.1 200) 1 headers in 44 bytes (0 switches on core 0)

参数说明:

--http 这个就和runserver一样指定IP 端口
--file 这个文件就里有一个反射,如果你在调用他的时候没有指定Web Server就使用默认的
-- static 做一个映射,指定静态文件

此时,访问http://192.168.3.2:8899,如下图所示,表示项目启动成功,uwsgi配置正确。
avatar

使用配置文件启动django

一般情况下,我们需要在django项目的scripts目录里面创建uwsgi.ini配置文件。

[root@django Scripts]# pwd
/data/wwwroot/hello/Scripts

在Script目录,创建项目所需要的uwsgi.ini文件。我个人习惯使用uwsgi+port来命名配置文件,比如本项目使用8888端口,哪么文件名就是uwsgi8888.ini。

[uwsgi]
socket = 127.0.0.1:8888
# 项目目录
chdir=/data/wwwroot/hello
# 指定项目的application
module=hello.wsgi
# 指定静态文件
static-map=/static=/data/wwwroot/hello/static
# 启用主进程
master = true
processes=2
threads=2
max-requests=2000
chmod-socket=664
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 设置日志目录
daemonize = /data/wwwlogs/uwsgi8888.log

配置文件写好之后就可以来启动uwsgi网关了。

[root@django Scripts]# uwsgi --ini uwsgi8888.ini  # 启动uwsgi
[uWSGI] getting INI configuration from uwsgi8888.ini
[root@django Scripts]# !ps  # 查看进程是否运行
ps aux | grep uwsgi
root       1501  2.5  1.2 249208 25744 ?        S    18:05   0:00 uwsgi --ini uwsgi8888.ini
root       1506  0.0  1.1 322940 22352 ?        Sl   18:05   0:00 uwsgi --ini uwsgi8888.ini
root       1508  0.0  1.1 322940 22352 ?        Sl   18:05   0:00 uwsgi --ini uwsgi8888.ini
root       1511  0.0  0.0 112704   964 pts/0    S+   18:05   0:00 grep --color=auto uwsgi
[root@django Scripts]# ss -anptu | grep 8888 # 查看端口是否在监听
tcp    LISTEN     0      100    127.0.0.1:8888                  *:*                   users:(("uwsgi",pid=1508,fd=3),("uwsgi",pid=1506,fd=3),("uwsgi",pid=1501,fd=3))

配置nginx vhost

在nginx配置目录下面,新建hello.conf虚拟主机配置文件。

[root@django vhost]# vim hello.conf 
server {
        listen       80;
        server_name  hello.lianst.com 192.168.3.2;

        location / {
            include  /usr/local/nginx/conf/uwsgi_params;
            uwsgi_pass 127.0.0.1:8888;
        }

    }

检查配置文件是否有错误,没有错误的话reload下,使其重新加载我们刚刚创建的vhost配置文件。

[root@django vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@django vhost]# /usr/local/nginx/sbin/nginx -s reload

avatar

至此,Django + Uwsgi + Nginx 的环境就部署就完成了。


添加新评论