搜索
首页运维Nginx基于ubuntu怎么通过Nginx部署Django

一、安装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/

基于ubuntu怎么通过Nginx部署Django  

如果出现如上图,说明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

基于ubuntu怎么通过Nginx部署Django

接下来配置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

以上是基于ubuntu怎么通过Nginx部署Django的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
NGINX单元:支持不同的编程语言NGINX单元:支持不同的编程语言Apr 16, 2025 am 12:15 AM

NGINXUnit支持多种编程语言,通过模块化设计实现。1.加载语言模块:根据配置文件加载相应模块。2.应用启动:调用语言运行时执行应用代码。3.请求处理:将请求转发给应用实例。4.响应返回:将处理后的响应返回给客户端。

在Nginx和Apache之间进行选择:适合您的需求在Nginx和Apache之间进行选择:适合您的需求Apr 15, 2025 am 12:04 AM

NGINX和Apache各有优劣,适合不同场景。1.NGINX适合高并发和低资源消耗场景。2.Apache适合需要复杂配置和丰富模块的场景。通过比较它们的核心特性、性能差异和最佳实践,可以帮助你选择最适合需求的服务器软件。

nginx怎么启动nginx怎么启动Apr 14, 2025 pm 01:06 PM

问题:如何启动 Nginx?答案:安装 Nginx启动 Nginx验证 Nginx 是否已启动探索其他启动选项自动启动 Nginx

怎么查看nginx是否启动怎么查看nginx是否启动Apr 14, 2025 pm 01:03 PM

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎么关闭nginx怎么关闭Apr 14, 2025 pm 01:00 PM

要关闭 Nginx 服务,请按以下步骤操作:确定安装类型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服务:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自动启动(可选):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

nginx在windows中怎么配置nginx在windows中怎么配置Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

nginx403错误怎么解决nginx403错误怎么解决Apr 14, 2025 pm 12:54 PM

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

linux怎么启动nginxlinux怎么启动nginxApr 14, 2025 pm 12:51 PM

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器