ubuntu NGINX uwsgi https 部署Django 遇到的问题

Wesley13
• 阅读 671

搞了3天终于把Django成功部署到Ubuntu,记录一下;

引用来自泡泡茶壶

Ubuntu下的Nginx + Uwsgi + Django项目部署详细流程

前提说明:

Django作为小程序的后端,因小程序的请求到后端的都是https请求,所以Django必须支持https请求

写在前面:

部署Django项目前,先用Django自带服务器运行一下看有没有问题,再部署;否则可能项目本身有问题,却以为是部署不正确导致的

运行命令:python manage.py runserver .....

各种配置:

1、nginx支持https请求

1)项目对象的nginx.conf配置

# the upstream component nginx needs to connect to
upstream django {
    # unix:///home/breavo/PyWorkSpace/mysite_code_shuffle/config/eshop.sock
    server xx.xx.xx.xx:8002; # xx.xx.xx.xx是服务器的ip,若是腾讯云服务器,则是服务器外部ip,这里的ip和端口是和uwsgi通信的方式,所以和uwsgi.ini配置文件中一致
}


# configuration of the server
server {
    # the port your site will be served on
    listen 90 ssl; # 监听端口可以是任何端口,但不要和已使用端口冲突
    # listen 8003;
    # the domain name it will serve for
    server_name  indoor.crazymonkey.ml; # 这里可以是域名也可以是本机ip,同上面的xx.xx.xx.xx
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # ssl configuration
    ssl on; # 打开了ssl验证,支持https请求
    ssl_certificate /root/hewenjuan/Indoor_Localization/2446810_www.preciselocation.top.pem; # ssl证书放的位置
    ssl_certificate_key /root/hewenjuan/Indoor_Localization/2446810_www.preciselocation.top.key; # 使用绝对路径肯定没错啦

    # Django media
    location /media  {
        alias /root/hewenjuan/Indoor_Localization/media; # your Django project's media files - amend as required,有图片等,
    }

    location /static {
        # alias /path/to/your/mysite/static; # your Django project's static files - amend as required
        alias /root/hewenjuan/Indoor_Localization/static; # 静态文件位置,Django项目admin静态文件
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django; # 这个就是引用了上面的upstream django配置了
        # include     /home/breavo/PyWorkSpace/mysite_code_shuffle/config/uwsgi_params; # the uwsgi_params file you installed
        include /etc/nginx/uwsgi_params;
    }
}

  说明:用nginx -t命令查看上面nginx文件的正确性

2)项目对应的uwsgi.ini配置

# mysite_uwsgi.ini file
[uwsgi]
socket =  xx.xx.xx.xx:8002 # 腾讯云内部ip,端口和nginx.conf配置的Django部分相同
# Django-related settings
# the base directory (full path)
chdir           = /root/hewenjuan/Indoor_Localization  # Django项目所在目录
# Django's wsgi file
module          = Indoor_Localization.wsgi # 
# module =  config/hello.py:application
wsgi_file = Indoor_Localization/wsgi.py # Django项目wsgi.py所在位置,相对于上面的chdir目录
# wsgi_file =  /root/hewenjuan/Indoor_Localization/config/hello.py:application
# the virtualenv (full path)
virtualenv = /root/.pyenv/versions/indoorLocation/bin/python3.6  # python虚拟环境绝对位置
# home            = /root/.pyenv
home            = /root/.pyenv/versions/indoorLocation

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
# socket          = /root/hewenjuan/Indoor_Localization/Indoor_Localization.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

stats           = %(chdir)/config/uwsgi.status           

pidfile         = %(chdir)/config/uwsgi.pid
# daemonize = %(chdir)/config/uwsgi.log # 开启该字段,uwsgi会在后台运行,日志保存在该文件中,若没有开启该字段,则在前台运行,日志显示在界面上(调试用方便)
# pidfile = %(chdir)/config/uwsgi.pid
#plugins =  /root/.pyenv/versions/indoorLocation/bin/python3.6

  3)每次修改了Django项目文件代码,都需要重启uwsgi和nginx,所以自己写了个启动文件start.sh

killall -9 uwsgi  # 关闭所有uwsgi进程,有时候没有关掉上次的uwsgi进程,启动新的会报错等问题
killall -9 uwsgi  # 额,有时候关闭一次会出现没关掉的情况
uwsgi --ini mysite_uwsgi.ini # 启动mysite_uwsgi.ini文件,即启动uwsgi进程
/etc/init.d/nginx restart # 重启nginx进程

  说明:启动start.sh命令:sh start.sh

2、uwsgi支持https请求

 1)uwsgi.ini配置

# mysite_uwsgi.ini file
[uwsgi]# 2446810_www.preciselocation.top.pem,2446810_www.preciselocation.top.key是ssl证书
https = xx.xx.xx.xx:443,2446810_www.preciselocation.top.pem,2446810_www.preciselocation.top.key # xx.xx.xx.xx是腾讯云内部ip
# Django-related settings
# the base directory (full path)
chdir           = /root/hewenjuan/Indoor_Localization
# Django's wsgi file
module          = Indoor_Localization.wsgi
# module =  config/hello.py:application
wsgi_file = Indoor_Localization/wsgi.py
# wsgi_file =  /root/hewenjuan/Indoor_Localization/config/hello.py:application
# the virtualenv (full path)
virtualenv = /root/.pyenv/versions/indoorLocation/bin/python3.6
# home            = /root/.pyenv
home            = /root/.pyenv/versions/indoorLocation

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
# socket          = /root/hewenjuan/Indoor_Localization/Indoor_Localization.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

stats           = %(chdir)/config/uwsgi.status           

pidfile         = %(chdir)/config/uwsgi.pid
# daemonize = %(chdir)/config/uwsgi.log
# pidfile = %(chdir)/config/uwsgi.pid
#plugins =  /root/.pyenv/versions/indoorLocation/bin/python3.6

遇到的问题:

1、每次退出账号重新登录都会提示,pyenv 不存在了

ubuntu NGINX uwsgi https 部署Django 遇到的问题

原因:

方法:命令行输入:source ~/.bashrc;即执行.bashrc文件中的语句(包含 了pyenv初始化语句)

2、nginx权限问题:django admin后台样式不显示,查看nginx错误日志(位置:/var/log/nginx/error.log),显示权限拒绝Permission denied

原因:Django项目下的static文件执行权限不够

方法:打开文件/etc/nginx/nginx.conf,修改 user www-data为user root(root:你目前登录的账号名称,也可能是Ubuntu等其他)

参考qingspacehttps://www.cnblogs.com/qingspace/p/6838747.html

3、unavailable modifier requested

ubuntu NGINX uwsgi https 部署Django 遇到的问题

原因:没有激活python虚拟环境

方法:输入命令:pyenv activate xx,激活xx环境,xx是你项目所在的python虚拟环境

 4、安装项目依赖包:pip install -r requirements.txt,遇到一个问题:安装不成功,界面没有显示不成功

出现下面显示的Successfully ...才表示安装成功

ubuntu NGINX uwsgi https 部署Django 遇到的问题

 原因:requirements.txt文件中有win_xx的安装包,即有python接入window系统所使用的安装包,类似了win32等,啥除掉即可;

因为有可能写代码使用的是window系统,部署的时候放在了Linux系统

解决方法:删除win32等针对window系统的安装包

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这