加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Liunx之Ubuntu下Django+uWSGI+nginx部署

发布时间:2020-12-13 17:55:24 所属栏目:Linux 来源:网络整理
导读:本文采用uwsgi+nginx来部署Django。 这种方式是将nginx作为服务器前端,将接受web所有的请求,统一管理。Nginx把所有的静态请求自己处理(静态文件处理是ngInx强项),然后把所有非静态请求通过uwsgi传递给Django,由Django来处理,从而完成一次web请求。 项

本文采用uwsgi+nginx来部署Django。

这种方式是将nginx作为服务器前端,将接受web所有的请求,统一管理。Nginx把所有的静态请求自己处理(静态文件处理是ngInx强项),然后把所有非静态请求通过uwsgi传递给Django,由Django来处理,从而完成一次web请求。

项目目录

路径:/opt/project_teacher

├──?teacher
│???├──?__init__.py
│???├──?settings.py
│???├──?urls.py
│???└──?wsgi.py
├──?app01
│???├──?urls.py
│???├──?views.py
└──?script
│???├──?uwsgi.ini?#?该文件是uwsgi配置文件
└──?manage.py

一、uWSGI

1. 安装

pip?install?uwsgi

2. 测试uWSGI是否安装成功

????????在终端中输入以下命令查看uwsgi的版本号,如果输出正常,说明uswgi已安装成功

$?uwsgi?--version
2.0.15

3. 编写一个简单的wsgi应用测试uwsgi是否能正常使用

????????首先创建一个test.py文件(命令:vim test.py)

#?test.py
def?application(env,?start_response):
????start_response('200?OK',?[('Content-Type','text/html')])
????return?[b"Hello?World"]?#?python3
????#return?["Hello?World"]?#?python2

4.运行uwsgi:

uwsgi?--http?:8000?--wsgi-file?test.py

参数解释:

????http :8000表示使用http协议,端口号为8000,

????wigi-file则表示要运行的wsgi应用程序文件。

uwsgi运行后打开浏览器,访问http://127.0.0.1:8000/ ,或者是相应服务器地址的8000端口,就可以看到hello world 页面了。

如果想要运行项目来测试

#?uwsgi?--http?:8000?--chdir?项目路径?-w?项目.wsg?--static-map=/static=static

uwsgi?--http?:8000?--chdir?/home/teacher/?-w?teacher.wsgi?--static-map=/static=static

uWSGI常用命令:

uwsgi?--chdir=/path/to/your/project?
????--module=mysite.wsgi:application?
????--env?DJANGO_SETTINGS_MODULE=mysite.settings?
????--master?--pidfile=/tmp/project-master.pid?
????--socket=127.0.0.1:49152???????#?可以ip地址,也可以是文件?
????--processes=5??????????????????#?进程数量
????--uid=1000?--gid=2000??????????#?如果是root用户,uwsgi可以有删除权限
????--harakiri=20??????????????????#?一个请求超时时间
????--max-requests=5000????????????#?一个工作进程最大请求数
????--vacuum???????????????????????#?退出时清楚环境
????--home=/path/to/virtual/env????#?virtualenv的路径
????--?static???????????????????????#?做一个映射,指定静态文件
????--http??????????????????????????#?这个就和runserver一样指定IP?端口
????--daemonize=/var/log/uwsgi/yourproject.log??????#?日志 

uWSGI官方文档:http://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html

5. 创建uwsgi配置文件

????在项目统计目录下创建文件夹script,(比如项目目录路径/home/project_teacher/teacher,那么scrip文件存放在/home/project/teacher/script)

$?pwd
/home/project_teacher

$?mkdir?script
$?vim?uwsgi.ini

????配置信息如下:

[uwsgi]
#?项目目录
chdir=/opt/project_teacher/teacher/
#?指定项目的application
module=teacher.wsgi:application
#?进程个数
workers=5
pidfile=/opt/project_teacher/script/uwsgi.pid
#?指定IP端口
http=192.168.31.123:8080
#?指定静态文件
static-map=/static=/opt/test_project/teacher/static
#?启动uwsgi的用户名和用户组
uid=root
gid=root
#?启用主进程
master=true
#?自动移除unix?Socket和pid文件当服务停止的时候
vacuum=true
#?序列化接受的内容,如果可能的话
thunder-lock=true
#?启用线程
enable-threads=true
#?设置自中断时间
harakiri=30
#?设置缓冲
post-buffering=4096
#?设置日志目录
daemonize=/opt/project_teacher/script/uwsgi.log
#?指定sock的文件路径
socket=/opt/project_teacher/script/uwsgi.sock

6. 启动配置

$?uwsgi?--ini?uwsgi.ini???#?启动uwsgi配置
[uwsgi-static]?added?mapping?for?/static?=>?/home/trunk/static????#?启动成功

$?uwsgi?--stop?uwsgi.pid??#?关闭uwsgi
signal_pidfile()/kill():?Operation?not?permitted?[core/uwsgi.c?line?1659]

$?uwsgi?--reload?uwsgi.pid??#重新加载配置


二、Nginx

1. 安装

$?sudo?apt-get?install?nginx??#安装

2. 检查nginx是否安装成功

$?/etc/init.d/nginx?start

[?ok?]?Starting?nginx?(via?systemctl):?nginx.service.

检查nginx是否启动成功

$?ps?-ef|grep?-i?nginx
root???????6961??????1??0?03:56??????????00:00:00?nginx:?master?process?/usr/sbin/nginx?-g?daemon?on;?master_process?on;
www-data???6962???6961??0?03:56??????????00:00:00?nginx:?worker?process
pala???????6985???2090??0?03:57?pts/0????00:00:00?grep?--color=auto?-i?nginx

然后打开浏览器,访问ip地址,出现如下页面即代表nginx安装完成且可以正常启动。

Welcome to nginx!

If you see this page,the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to?nginx.org.
Commercial support is available at?nginx.com.

Thank you for using nginx.


Nginx常用命令

$?/etc/init.d/nginx?start??#启动
$?/etc/init.d/nginx?stop??#关闭
$?/etc/init.d/nginx?restart??#重启
$??killall?nginx????杀死所有nginx

#?如果是生产环境的话Nginx正在运行,就不要直接stop?start?或者?restart??直接reload就行了
#?对线上影响最低
$?/etc/init.d/nginx?reload

三、Django + uWSGI +Nginx

  1. ?创建一个xxx.conf配置文件(nginx的默认配置目录为/etc/nginx/conf.d)

    $?vim?/etc/nginx/conf.d/xxx.conf
  2. 配置文件信息如下:

server?{???#?这个server标识我要配置了
????????listen?80;??#?80?是http默认的端口,?443?是https默认的端口(网页一般使用这两个端口)
????????server_name?www.chenxm.cc?;??#?你访问的路径前面的url名称
????????access_log??/var/log/nginx/access.log;??#?Nginx日志配置
????????error_log??/var/log/nginx/error.log;????#?Nginx错误日志配置
????????charset??utf-8;?#?Nginx编码
????????gzip?on;??#?启用压缩,这个的作用就是给用户一个网页,比如3M压缩后1M这样传输速度就会提高很多
????????gzip_types?text/plain?application/x-javascript?text/css?text/javascript?application/x-httpd-php?application/json?text/json?image/jpeg?image/gif?image/png?application/octet-stream;??#?支持压缩的类型

????????error_page??404???????????/404.html;??#?错误页面
????????error_page???500?502?503?504??/50x.html;??#?错误页面

????????#?指定项目路径uwsgi
????????location?/?{????????#?这个location就和咱们Django的url(r'^admin/',?admin.site.urls),????????????include?uwsgi_params;??#?导入一个Nginx模块他是用来和uWSGI进行通讯的
????????????uwsgi_connect_timeout?30;??#?设置连接uWSGI超时时间
????????????#?指定uwsgi的sock文件所有动态请求就会直接丢给他
????????????uwsgi_pass?unix:/opt/project_teacher/script/uwsgi.sock;??
????????}

????????#?指定静态文件路径
????????location?/static/?{
????????????alias??/opt/project_teacher/teacher/static/;
????????????index??index.html?index.htm;
????????}
????????}

????3. 检查nginx语法问题

$?nginx?-t??#?检查nginx语法问题

nginx:?the?configuration?file?/etc/nginx/nginx.conf?syntax?is?ok
nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful

? ? 4. 重启nginx

$?/etc/init.d/nginx?restart??#重启

如果重启nginx报错,请查看此文章:

ubuntu/linux 重启nginx失败,提示 Restarting nginx nginx ...fail!

四 django静态文件收集

????1.setting.py设置

DEBUG?=?False
STATIC_ROOT?=?os.path.join(BASE_DIR,?'statics')

????2. 执行collectstatic命令:

python?manage.py?collectstatic

好处:django把静态文件拷贝到你设置的statics目录下(这样可以更方便的和nignx集成,权限管理也更方便)


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读