centos+nginx+uwsgi部署django项目上线
作者:蒙多不减肥 时间:2023-12-13 03:36:31
上传完整django项目文件到服务器
sftp ftp lrzsz都可以上传文件到服务器,看自己
我django项目叫yunwei,主要app是rabc和web,整个项目放/opt/下
如下:
[root@test-code opt]# ls
django_virt nginx redis redis-6.2.6 yunwei
[root@test-code opt]# ls yunwei/
manage.py rbac static templates uwsgi web yunwei
[root@test-code opt]# ls yunwei/uwsgi/
cut_log.sh log uwsgi.ini uwsgi.log uwsgi.pid uwsgi.sock
[root@test-code opt]#
/opt/yunwei/就是我的django项目的根目录,manage.py就是在这个目录下,/opt/yunwei/下有一个同名子目录yunwei,下面有一个setting.py的配置文件
Python安装
如果用到的python版本是2,不需要重新安装,如果是3,那么需要重新安装,参照:centos安装python3.8
django相关库的安装
#在原项目处导出django项目安装的库存入文件
pip freeze > install.txt
把文件放到需要部署的服务器上,并安装文件内的包
#在待部署的服务器上执行
pip install -r install.txt
nginx安装&配置(处理静态请求和代理动态请求到uwsgi)
nginx安装参照:linux安装nginx
nginx配置文件如下
[root@test-code opt]# cat /opt/nginx/conf.d/django.conf
server {
listen 8881;
server_name localhost;
server_tokens off;
location /static {
root /opt/yunwei;
index index.html index.htm;
}
location / {
include uwsgi_params; #nginx加载uwsgi模块
uwsgi_buffer_size 16k;
uwsgi_busy_buffers_size 24k;
#如果你后端的需要超过60秒时间处理请求,那么一定要加上下面三个超时时间的设置,不然60s之后nginx断开链接报超时
uwsgi_send_timeout 600; # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。
uwsgi_connect_timeout 600; # 指定连接到后端uWSGI的超时时间。
uwsgi_read_timeout 600; # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间。
uwsgi_pass unix:/opt/yunwei/uwsgi/uwsgi.sock; #nginx对应的uwsgi socket文件
}
}
uwsgi安装&配置
pip install uwsgi
在django的项目目录下/opt/yunwei/创建一个uwsgi的目录用于存放uwsgi相关的文件。
cd /opt/yunwei/ && mkdir uwsgi
touch uwsgi/uwsgi.ini
创建配置文件
[root@test-code yunwei]# cat uwsgi/uwsgi.ini
[uwsgi]
#django项目的根目录,即manage.py所在的目录
chdir=/opt/yunwei
#django项目的wsgi,yuwnei的项目名
module=yunwei.wsgi:application
socket=/opt/yunwei/uwsgi/uwsgi.sock
#这里是我的python虚拟环境,可以不配置
home=/opt/django_virt
#进程数
workers=5
#pid文件路径
pidfile=/opt/yunwei/uwsgi/uwsgi.pid
#IP端口
socket = 0.0.0.0:8000
master=true
#退出清理文件
vacuum=true
#启用线程
enable-threads=true
#序列化接受的内容,如果可能的话
thunder-lock=true
#设置自中断时间,如果后端处理一些请求时间比较长,这个一定要设置大一点
harakiri=3600 #
socket-timeout=3600 #这个是和nginx搭配部署时的设置
http-timeout=3600 #这个是单独部署时的设置
#设置缓冲
post-buffering=65535
buffer-size = 6553600
#后台守护方式运行,日志路径
daemonize=/opt/yunwei/uwsgi/uwsgi.log
标题关闭setting.py的DEBUG模式
vim /opt/yunwei/yunwei/setting.py
DEBUG = True #改为 DEBUG = False
debug模式为True时,django会自己处理static的静态请求,现在是nginx去处理这些请求,故为False
做完上述步骤,只需要启动uwsgi和nginx即可,通过nginx listen端口去访问
#uwsgi 启动命令
uwsgi --ini /opt/yunwei/uwsgi/uwsgi.ini
#uwsgi 重启命令
uwsgi --reload /opt/yunwei/uwsgi/uwsgi.ini
uwsgi的日志文件在/opt/yunwei/uwsgi/uwsgi.log处
可以通过 ss -tnulp | grep uwsgi 去kill -9 进程ID去杀掉进程
#启动nginx
nginx
#重启
nginx -s reload
#关闭
nginx -s stop
#检测配置文件是否正确
nginx -t
搞定,作此记录。到此这篇关于centos+nginx+uwsgi部署django项目上线的文章就介绍到这了,更多相关nginx uwsgi部署django内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://blog.csdn.net/qq_43084874/article/details/124046685
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决Windows10下mysql5.5数据库命令行中文乱码问题
python实战之德州扑克第三步-比较大小
网页中的平衡、对比、连贯和留白
5招带你轻松优化MySQL count(*)查询性能
![](https://img.aspxhome.com/file/2023/0/107430_0s.png)
人工智能学习PyTorch实现CNN卷积层及nn.Module类示例分析
![](https://img.aspxhome.com/file/2023/9/133239_0s.png)
M1芯片Mac上Anaconda的暂时替代(miniforge)
![](https://img.aspxhome.com/file/2023/2/93872_0s.jpg)
Python实现智能贪吃蛇游戏的示例代码
![](https://img.aspxhome.com/file/2023/1/100861_0s.jpg)
golang 中 channel 的详细使用、使用注意事项及死锁问题解析
asp开发Wap中的随笔
Python基于smtplib实现异步发送邮件服务
python使用html2text库实现从HTML转markdown的方法详解
python实现一个简单的ping工具方法
django中的图片验证码功能
![](https://img.aspxhome.com/file/2023/9/95469_0s.png)
Python Tkinter Menu组件详解
![](https://img.aspxhome.com/file/2023/3/81863_0s.png)
如何实现一个python函数装饰器(Decorator)
SQL Server 2008登录错误:无法连接到(local)解决方法
![](https://img.aspxhome.com/file/2023/3/129323_0s.png)
PHP实现简易计算器功能
![](https://img.aspxhome.com/file/2023/1/126211_0s.gif)
如何基于pythonnet调用halcon脚本
![](https://img.aspxhome.com/file/2023/2/67372_0s.png)
C#实现的ACCESS数据库操作类完整实例
Python爬取视频时长场景实践示例
![](https://img.aspxhome.com/file/2023/9/65139_0s.png)