Django部署方式有很多种,之前写过一篇部署在Apache上的博文:https://www.cnblogs.com/shenh/p/7552722.html 。下文介绍的是通过Nginx来部署。

  Nginx是一个高性能的HTTP和反向代理服务,运用非常广泛。Django应用可以通过Nginx+uwsgi的方式进行部署。Nginx放置在服务器的最前端来接收所有web请求,统一管理,首先分离出静态请求,自己做处理。然后,Nginx将非静态请求通过uwsgi转发给Django,由Django处理。

一、安装Nginx

安装Nginx命令

apt-get install Nginx

Nginx的默认端口为80,很容易被其他服务占用,因此需要修改成其他端口。打开文件 vi /etc/nginx/nginx.conf ,如果能找到监听的端口,直接修改 。找不到的也没关系 ,找到如下配置 ,说明是引用了其他目录的文件

...
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
...

打开 /etc/nginx/sites-enabled/ ,找到并编辑 default ,将80修改成8088

# Default server configuration
#
server {
listen default_server;
listen [::]: default_server; # SSL configuration
#
# listen ssl default_server;
# listen [::]: ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

Nginx启动命令

service nginx start

service nginx restart

service nginx stop

测试下,访问  http://10.1.35.51:8088/

二、配置uwsgi

1.安装

pip 安装 uwsgi 命令:

pip install uwsgi

2.配置 uwsgi 和 Django 的连接

Django 项目路径位于 /home/project/web/ ,在 Django 项目根目录(manage.py 同级目录)新建文件 uwsgi.ini (也支持 xml 文件格式)。在文件中添加如下内容:

# uwsgi.ini file
[uwsgi] # Django-related settings
socket = : # the base directory (full path)
chdir = /home/project/web # Django s wsgi file
module = web.wsgi # process-related settings
master = true # maximum number of worker processes
processes = 5 #maximum number of worker threads
threads = 5
# try to remove all of the generated file/sockets
vacuum = true

3. 配置 uwsgi 和 Nginx 的连接

修改Nginx配置文件:vi /etc/nginx/sites-available/default ,增加一段配置。然后重启Nginx

server {
listen ;
server_name 127.0.0.1
charset UTF-;
access_log /var/log/nginx/web_access.log;
error_log /var/log/nginx/web_error.log; client_max_body_size 75M; location / {
include uwsgi_params; # 通过uwsgi转发请求
uwsgi_pass 127.0.0.1:; # 和上文配置的socket端口保持一致
uwsgi_read_timeout 15; # 设置请求超时时间
}
location /static { # 访问静态资源
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /var/web/;
}
}

4.配置 Django 静态资源

在setting里增加配置,静态资源 路径 和 Nginx 里的访问路径一致

STATIC_ROOT = os.path.join(BASE_DIR,'/var/web')

执行命令

Python manage.py collectstatic

5.最后启动项目

cd /home/project/web
uwsgi --ini uwsgi.ini

运行成功:

(env35) root@ubuntu:/home/project/ShiHangTool# uwsgi --ini ShiHangTool_uwsgi.ini
[uWSGI] getting INI configuration from ShiHangTool_uwsgi.ini
*** Starting uWSGI 2.0.17.1 (64bit) on [Tue Dec :: ] ***
compiled with version: 5.4. on December ::
os: Linux-4.4.--generic #-Ubuntu SMP Wed Oct :: UTC
nodename: ubuntu
machine: x86_64
clock source: unix
detected number of CPU cores:
current working directory: /home/project/ShiHangTool
detected binary path: /root/.virtualenvs/env35/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /home/project/ShiHangTool
your processes number limit is
your memory page size is bytes
detected max file descriptor number:
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket bound to TCP address : fd
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.5. (default, Nov , ::) [GCC 5.4. ]
Python main interpreter initialized at 0x1504fe0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
python threads support enabled
your server socket listen backlog is limited to connections
your mercy for graceful operations on workers is seconds
mapped bytes ( KB) for cores
*** Operational MODE: preforking+threaded ***
WSGI app (mountpoint='') ready in seconds on interpreter 0x1504fe0 pid: (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 master process (pid: )
spawned uWSGI worker (pid: , cores: )
spawned uWSGI worker (pid: , cores: )
spawned uWSGI worker (pid: , cores: )
spawned uWSGI worker (pid: , cores: )
spawned uWSGI worker (pid: , cores: )

访问 http://10.1.35.51:8099/Home/OrderSettle-K8S/

nginx部署django应用的更多相关文章

  1. Ubuntu上通过nginx部署Django笔记

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式.今天在Ubuntu上使用Nginx部署Django服务,虽然不是第一次搞这个了,但是发现还是跳进了好多坑,g ...

  2. 使用uWSGI+nginx部署Django项目

    最近使用django写了一些项目,不过部署到服务器上碰到一些问题,还有静态文件什么的一堆问题,这里总结一下碰到的问题和解决方案,总体思路是按照官方文档走的. 原文地址:http://uwsgi-doc ...

  3. 通过Nginx部署Django(基于ubuntu)

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...

  4. 通过Nginx部署Django

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...

  5. ubuntu18+uwsgi+nginx部署django项目

    更新系统软件源 sudo apt-get update pip3安装 sudo apt install python3-pip 安装virtualenvwrapper pip3 install vir ...

  6. 【转】通过Nginx部署Django

    https://www.cnblogs.com/frchen/p/5709533.html Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中, ...

  7. 使用nginx部署Django静态文件配置

    首先,我们配置静态文件,要在setting.py里面加入如下几行代码: # settings.py # the settings above # STATIC SETTINGS STATIC_URL ...

  8. Python项目部署-使用Nginx部署Django项目

    一.nginx介绍及部署 二.nginx部署路飞学城代码 nginx配置安装 同样,nginx也有很多的安装方式: 1)源码安装(运维偏向:规范,便于配置管理) 2)yum,rpm安装(为了效率可以选 ...

  9. 关于Nginx部署Django项目的资料收集

    参考:https://www.cnblogs.com/chenice/p/6921727.html 参考:https://blog.csdn.net/fengzq15/article/details/ ...

随机推荐

  1. 解决.Net Core跨域问题

    什么是跨域?浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域 跨域的几种情况 1.端口和协议的不同,只能通过后台来解决 2.localhost和127.0.0.1虽然 ...

  2. Maven捆绑TestNG实现测试自动化执行、部署和调度

    一. 需求介绍 自动化测试,尤其是接口测试时,要写大量的测试用例,这些测试用例我们当然首选使用TesteNG编写,用例数量大,还涉及各种依赖包之类的问题,因此用Maven管理也是最方便最易实现的. 面 ...

  3. 环境搭建文档——Windows下的Git搭建详解

    Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.具体安装步骤如下: 第一步:先从官网下载最新版本的Git 官网地址:https://git-scm.com/do ...

  4. 隐藏Spring Elements

    Spring Elements 显示出来很烦,如何隐藏

  5. 文件在线预览doc,docx转换pdf(一)

    文件在线预览doc,docx转换pdf(一) 1. 前言 文档转换是一个是一块硬骨头,但是也是必不可少的,我们正好做的知识库产品中,也面临着同样的问题,文档转换,精准的全文搜索,知识的转换率,是知识库 ...

  6. noip第29课资料

  7. centos7下zabbix记录

    Zabbixrpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm - ...

  8. listcard记录

    .listcard-bg{ .listcard-head{ background: #F34A55; width: 100%; height: 50px; margin-bottom: -50px; ...

  9. Avro序列化与反序列化

    4.Avro实现序列化的步骤 1.创建maven工程,引入pom文件(定义属性和依赖信息,以及定义Avro插件) 2.根据Avro插件的要求,更改maven工程结构,需要创建一个source/main ...

  10. 利用python完成大学刷课(从0到完成的思路)

    i春秋作家:tllm 原文来自:利用python完成大学刷课(从0到完成的思路) 最近刚刚开学,学校总是有很多让人无语的课要修,还不能不修.然后我想写一个自动修课的脚本.大佬们不要笑我 是边面向百度学 ...