基于ubuntu如何通过Nginx部署Django

40次阅读
没有评论

共计 5631 个字符,预计需要花费 15 分钟才能阅读完成。

这篇文章主要介绍“基于 ubuntu 如何通过 Nginx 部署 Django”的相关知识,丸趣 TV 小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“基于 ubuntu 如何通过 Nginx 部署 Django”文章能帮助大家解决问题。

django 的部署可以有很多方式,采用 nginx+uwsgi 的方式是其中比较常见的一种方式。

在这种方式中,我们的通常做法是,将 nginx 作为服务器最前端,它将接收 web 的所有请求,统一管理请求。nginx 把所有静态请求自己来处理(这是 nginx 的强项)。然后,nginx 将所有非静态请求通过 uwsgi 传递给 django,由 django 来进行处理,从而完成一次 web 请求。可见,uwsgi 的作用就类似一个桥接器。起到桥梁的作用。

一、安装 nginx 

nginx 是一款轻量级的 web 服务器 / 反向代理服务器及电子邮件(imap/pop3)代理服务器,并在一个 bsd-like 协议下发行。其特点是占有内存少,并发能力强,事实上 nginx 的并发能力确实在同类型的网页服务器中表现较好。

nginx 同样为当前非常流行的 web 服务器。利用其部署 django,我们在此也做简单的介绍。

nginx 官网:

打开 ubuntu 控制台(ctrl+alt+t)利用 ubuntu 的仓库安装。

fnngj@ubuntu:~$ sudo apt-get install nginx # 安装 

启动 nginx:

fnngj@ubuntu:~$ /etc/init.d/nginx start # 启动
fnngj@ubuntu:~$ /etc/init.d/nginx stop # 关闭
fnngj@ubuntu:~$ /etc/init.d/nginx restart # 重启 

修改 nginx 默认端口号,打开 /etc/nginx/nginx.conf 文件,修改端口号。

 server {
 listen 8088; #  修改端口号
 server_name localhost;
 #charset koi8-r; 
 #access_log logs/host.access.log main;
 location / {
 root html;
 index index.html index.htm;
 }

大概在文件 36 行的位置,将默认的 80 端口号改成其它端口号,如 8088。因为默认的 80 端口号很容易被其它应用程序占用。

然后,通过上面命令重启 nginx。访问:http://127.0.0.1:8088/

如果出现如上图,说明 nginx 启动成功。 

二、安装 uwsgi 

  通过 pip 安装 uwsgi。

root@ubuntu:/etc# python3 -m pip install uwsgi

测试 uwsgi,创建 test.py 文件:

def application(env, start_response):
 start_response(200 ok , [( content-type , text/html)])
 return [b hello world]

通过 uwsgi 运行该文件。

fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --wsgi-file test.py

接下来配置 django 与 uwsgi 连接。此处,假定的我的 django 项目位置为:/home/fnngj/pydj/myweb

复制代码   代码如下:

uwsgi –http :8001 –chdir /home/fnngj/pydj/myweb/ –wsgi-file myweb/wsgi.py –master –processes 4 –threads 2 –stats 127.0.0.1:9191

常用选项:

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)

三、nginx+uwsgi+django 

接下来,我们要将三者结合起来。首先罗列一下项目的所需要的文件:

myweb/
├── manage.py
├── myweb/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── myweb_uwsgi.ini

在我们通过 django 创建 myweb 项目时,在子目录 myweb 下已经帮我们生成的 wsgi.py 文件。所以,我们只需要再创建 myweb_uwsgi.ini 配置文件即可,当然,uwsgi 支持多种类型的配置文件,如 xml,ini 等。此处,使用 ini 类型的配置。

# myweb_uwsgi.ini file
[uwsgi]
# django-related settings
socket = :8000
# the base directory (full path)
chdir = /home/fnngj/pydj/myweb
# django s wsgi file
module = myweb.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true

这个配置,其实就相当于在上一小节中通过 wsgi 命令,后面跟一堆参数的方式,给文件化了。

socket  指定项目执行的端口号。

chdir  指定项目的目录。

module  myweb.wsgi,可以这么来理解,对于 myweb_uwsgi.ini 文件来说,与它的平级的有一个 myweb 目录,这个目录下有一个 wsgi.py 文件。

其它几个参数,可以参考上一小节中参数的介绍。

接下来,切换到 myweb 项目目录下,通过 uwsgi 命令读取 myweb_uwsgi.ini 文件启动项目。

fnngj@ubuntu:~$ cd /home/fnngj/pydj/myweb/
fnngj@ubuntu:~/pydj/myweb$ uwsgi --ini myweb_uwsgi.ini 
[uwsgi] getting ini configuration from myweb_uwsgi.ini
*** starting uwsgi 2.0.12 (32bit) on [sat mar 12 13:05:06 2016] ***
compiled with version: 4.8.4 on 26 january 2016 06:14:41
os: linux-3.19.0-25-generic #26~14.04.1-ubuntu smp fri jul 24 21:18:00 utc 2015
nodename: ubuntu
machine: i686
clock source: unix
detected number of cpu cores: 2
current working directory: /home/fnngj/pydj/myweb
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/fnngj/pydj/myweb
your processes number limit is 15962
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 socket 0 bound to tcp address :8000 fd 3
python version: 3.4.3 (default, oct 14 2015, 20:37:06) [gcc 4.8.4]
*** python threads support is disabled. you can enable it with --enable-threads ***
python main interpreter initialized at 0x8b52dc0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 319920 bytes (312 kb) for 4 cores
*** operational mode: preforking ***
wsgi app 0 (mountpoint=) ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app)
*** uwsgi is running in multiple interpreter mode ***
spawned uwsgi master process (pid: 7158)
spawned uwsgi worker 1 (pid: 7160, cores: 1)
spawned uwsgi worker 2 (pid: 7161, cores: 1)
spawned uwsgi worker 3 (pid: 7162, cores: 1)
spawned uwsgi worker 4 (pid: 7163, cores: 1)

注意查看 uwsgi 的启动信息,如果有错,就要检查配置文件的参数是否设置有误。

再接下来要做的就是修改 nginx.conf 配置文件。打开 /etc/nginx/nginx.conf 文件,添加如下内容。

……
server {
 listen 8099; 
 server_name 127.0.0.1 
 charset utf-8;
 access_log /var/log/nginx/myweb_access.log;
 error_log /var/log/nginx/myweb_error.log;
 client_max_body_size 75m;
 location / { 
 include uwsgi_params;
 uwsgi_pass 127.0.0.1:8000;
 uwsgi_read_timeout 2;
 } 
 location /static {
 expires 30d;
 autoindex on; 
 add_header cache-control private;
 alias /home/fnngj/pydj/myweb/static/;
 }
 }
……

 listen 指定的是 nginx 代理 uwsgi 对外的端口号。

server_name  网上大多资料都是设置的一个网址(例,www.example.com),我这里如果设置成网址无法访问,所以,指定的到了本机默认 ip。

在进行配置的时候,我有个问题一直想不通。nginx 到底是如何 uwsgi 产生关联。现在看来大概最主要的就是这两行配置。

include uwsgi_params;

uwsgi_pass 127.0.0.1:8000;

include 必须指定为 uwsgi_params;而 uwsgi_pass 指的本机 ip 的端口与 myweb_uwsgi.ini 配置文件中的必须一直。

现在重新启动 nginx,翻看上面重启动 nginx 的命令。然后,访问:http://127.0.0.1:8099/

通过这个 ip 和端口号的指向,请求应该是先到 nginx 的。如果你在页面上执行一些请求,就会看到,这些请求最终会转到 uwsgi 来处理。

关于“基于 ubuntu 如何通过 Nginx 部署 Django”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注丸趣 TV 行业资讯频道,丸趣 TV 小编每天都会为大家更新不同的知识点。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-15发表,共计5631字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)