uWSGI + Nginx + Django 部署
来源:https://www.cnblogs.com/midworld/p/10992005.html
1. uWSGI 服务器
Django 默认使用 WSGI(Python Web Server Gateway )
作为 Web 服务器,一般仅用来作为测试使用,实际生产环境而是使用 uWSGI 和 Nginx 作为服务器。
uWSGI 代码完全用C编写,效率高、性能稳定,但是处理 静态文件能力较弱,因此实际生产环境中,一般采用 uWSGI + Nginx
两者搭配使用:
- uWSGI:处理动态请求(css、js、图片文件等)
- Nginx:处理静态文件请求(提交表单、mysql、动态渲染 html)
安装:
pip3 install uWSGI
settings.py 配置
设置 ALLOWED_HOSTS
:
ALLOWED_HOSTS = [# 加上本机的IP地址'192.168.xx.xxx','127.0.0.1', 'localhost'
]
也可以这样设置:
ALLOWED_HOSTS = ['*']
1.1 通过参数启动 uWSGI
# 请更换成你服务器 ip 和端口,其中 Celery_Test/wsgi.py 为Django 项目中自带的 wsgi web 服务
hj@hj:~/桌面/Celery_Test$ uwsgi --http 192.168.21.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static
出现如下效果便是运行成功了:
查看启动端口:
hj@hj:~/桌面/Celery_Test/script$ ps -ef|grep uwsgihj 17176 5231 0 15:37 pts/1 00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static
hj 17177 17176 0 15:37 pts/1 00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static
hj 17206 6554 0 15:38 pts/2 00:00:00 grep --color=auto uwsgi
访问 192.168.21.128:8080
:
1.2 通过配置文件启动 uWSGI
在项目根目录创建一个名为 uwsgi.ini
的配置文件,配置如下:
[uwsgi]
# 项目目录
chdir=/home/hj/桌面/Celery_Test/
# 指定项目的application
module=Celery_Test.wsgi:application
# 指定sock的文件路径
socket=/home/hj/桌面/Celery_Test/script/uwsgi.sock
# 进程个数
workers=5
pidfile=/home/hj/桌面/Celery_Test/script/uwsgi.pid
# 指定IP端口
http=192.168.21.128:8080
# 指定静态文件
static-map=/static=/home/hj/桌面/Celery_Test/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=/home/hj/桌面/Celery_Test/script/uwsgi.log
配置好之后,运行 uwsgi --ini uwsgi.ini
启动 uwsgi,出现如下信息即表示启动成功:
hj@hj:~/桌面/Celery_Test$ uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
[uwsgi-static] added mapping for /static => /home/hj/桌面/Celery_Test/static
查看运行情况:
ps ajx|grep uwsgi
效果如下图:
常用命令:
uwsgi --ini uwsgi.ini # 启动# 启动时会生成两个文件,分别为:# PID文件 标识这个程序所处的状态# SOCK文件 用来和其他程序通信的
uwsgi --stop uwsgi.pid # 停止
uwsgi --reload uwsgi.ini # 重置
Tips
停止时出现 signal_pidfile()/kill(): No such process [core/uwsgi.c line 1693]
原因:当前端口进程与 uwsgi.pid
不一致,查看当前端口实际进程 ID,并修改 uwsgi.pid
:
# 根据端口,查看进程
hj@hj:~/桌面/Celery_Test$ sudo netstat -nap | grep 8080
tcp 0 0 192.168.21.128:8080 0.0.0.0:* LISTEN 6943/uwsgi # 修改 uwsgi.pid 的值为 6943,并再重新停止
hj@hj:~/桌面/Celery_Test$ uwsgi --stop script/uwsgi.pid# 查看发现已经成功停止
hj@hj:~/桌面/Celery_Test$ ps ajx|grep uwsgi5231 14550 14549 5231 pts/1 14549 S+ 1000 0:00 grep --color=auto uwsgi
Linux 中怎么查看端口和进程号
# 加上 sudo
# 根据进程 pid 查看端口
lsof -i | grep pid# 根据端口查看进程
lsof -i:port# 根据进程 pid 查看端口
netstat -nap | grep pid# 根据端口查看进程号
netstat -nap | grep port
2. Nginx 服务器
我们知道 uWSGI 处理静态文件请求能力比较弱,因此一般实际生产环境中以 动静分离 的方式处理动静请求,即 uWSGI + Nginx。
Nginx 作用还包括负载均衡、反向代理等。
2.1 Ubuntu 上安装 Nginx
Nginx 的软件包在 Ubuntu 默认软件仓库中可用。 安装非常简单,只需键入以下命令:
sudo apt update
udo apt install nginx
查看服务器版本信息:
sudo nginx -vnginx version: nginx/1.14.0 (Ubuntu)
查看服务器状态:
# 两个都可以
sudo systemctl status nginx
ps -ef | grep nignx
配置防火墙
打开 80 和 443 端口允许通过防火墙:
hj@hj:~$ sudo ufw allow 'Nginx Full'
防火墙规则已更新
规则已更新(v6)
检查是否更改:
hj@hj:~$ sudo ufw status
状态: 激活至 动作 来自
- -- --
22 ALLOW Anywhere
4200 ALLOW Anywhere
Nginx Full ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
4200 (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)
测试安装
访问:http://192.168.21.128/
:
使用 systemctl 管理 Nginx 服务
您可以像任何其他 systemd 单位一样管理 Nginx 服务:
# 停止Nginx服务
sudo systemctl stop nginx# 再次启动
sudo systemctl start nginx# 重新启动Nginx服务:
sudo systemctl restart nginx# 在进行一些配置更改后重新加载 Nginx 服务:
$sudo systemctl reload nginx# 如果你想禁用Nginx服务在启动时启动:
$sudo systemctl disable nginx# 并重新启用它:
$sudo systemctl enable nginx
参考链接:如何在Ubuntu 18.04上安装Nginx
2.2 CentOS 上安装
以CentOS6.x 系统为例
- 更换源:
# 备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup# 更换成国内源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo# 生成缓存
yum makecache
- 安装 Nginx:
yum -y install nginx
2.3 与 uWSGI 结合使用部署 Django
为 Nginx 添加配置文件,Ngnix 默认配置文件加载是在 /etc/nginx/conf.d/
目录下,新建一个配置文件(名字随意),编辑如下:
# 新建到配置文件 conf.d
vim /etc/nginx/conf.d/# 编辑配置文件
server { # 开始配置了listen 80; # 监听端口server_name 10.129.xxx.183 ; # 你访问的路径前面的 url名称access_log /var/log/nginx/access.log main; # 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; # 错误页面# 指定项目路径 uwsgilocation / { # 类似于 Django的 url(r'^admin/', admin.site.urls),include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock; # 指定uwsgi的sock文件所有动态请求就会直接丢给他}# 指定静态文件路径location /static/ {alias /opt/project_teacher/teacher/static/;index index.html index.htm;}}
参考:
server { listen 80; server_name 192.168.xx.128 ; access_log /var/log/nginx/access.log main; charset utf-8; gzip on; 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; location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/home/hj/桌面/Celery_Test/script/uwsgi.sock; }location /static/ {alias /home/hj/桌面/Celery_Test/static/;index index.html index.htm
启动 Nginx 服务 /etc/init.d/nginx start
,访问:http://192.168.21.128:8080/app/index/
,效果如下图:
常用命令:
/etc/init.d/nginx start # 启动
/etc/init.d/nginx stop # 关闭# Nginx配置是重启才生效,若修改配置不知道是否对不对,可以用来测试
/etc/init.d/nginx configtest# 生产环境直接 reload就行了,不要 stop start 或者 restart
/etc/init.d/nginx reload
配置 Django 静态文件
admin 所需的静态文件都在 Django 的安装内,我们没有配置指向 Django 的配置文件。
解决办法:
- 设置
settings.py
,将所有静态文件收集到static_all
中:
# settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static_all")# 收集成功提示(仅供参考)
120 static files copied to '/home/hj/桌面/Celery_Test/static_all'.
- 收集静态文件到
static_all
这个目录中(可不创建 static_all):
python3 manage.py collectstatic --noinput
- 修改
/etc/nginx/conf.d/test.ini
中静态文件路径:
alias /home/hj/桌面/Celery_Test/static_all;
指定其他地方放置静态文件
# 新建目录
sudo mkdir -vp /var/www/test2/static/# 赋予权限
sudo chmod 777 /var/www/test2/static/# 修改项目中 settings.py,指定静态文件路径
STATIC_ROOT = '/var/www/test2/static/'
STATIC_URL = '/static/'# 收集静态文件到 /var/www/test2/static/ 中
python3 manage.py collectstatic# 输入 yes,开始收集,重新加载 Nginx 服务
uWSGI + Nginx + Django 部署相关推荐
- Uwsgi+Nginx+Django部署
Django+Uwsgi+Nginx部署 这几天一直在捣腾Django部署,一只菜鸡的上山路 如果什么不对,还请多多指教. 参考:如何使用Django与uWSGI,Uwsgi文档 Linux使用的是u ...
- Django+Linux+Uwsgi+Nginx项目部署文档
Django+Linux+Uwsgi+Nginx项目部署文档 WSGI 在生产环境中使用WSGI作为python web的服务器 WSGI:全拼为Python Web服务器网关接口,Python We ...
- 瞬间心情愉悦,头发都少掉了几根!---Django+uwsgi+Nginx项目部署超详细步骤
文章目录 瞬间心情愉悦,头发都少掉了几根!---Django+uwsgi+Nginx项目部署超详细步骤 先决条件 步骤 本地运行测试 上传服务器 环境安装 准备django程序 完美解决! 瞬间心情愉 ...
- Django+uwsgi+nginx服务器部署
准备工作 推荐使用anaconda进行python环境的管理,因python环境容易出现各种版本冲突问题 安装anaconda: wget https://repo.anaconda.com/arch ...
- Nginx + uWSGI + Python + Django部署实例
Nginx: Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的 ...
- Python对接微信小程序V3接口进行支付,并使用uwsgi+nginx+django进行https部署
网上找了很多教程,但是很乱很杂,并且教程资源很少且说的详细.这里就记录一下分享给大家 共分为以下几个步骤: 一.开始前准备信息 二.使用前端code获取用户的openid 三.对接小程序v3接口下单 ...
- Ubuntu下Django+uWSGI+nginx部署
本文采用uwsgi+nginx来部署django 这种方式是将nginx作为服务端前端,将接受web所有的请求,统一管理,Nginx把所有的静态请求自己处理,然后把所有非静态请求通过uwsgi传递给D ...
- 【django云服务器部署】django+uwsgi+nginx 部署的完整流程
本篇博客完整地记录了我从购买服务器到完成部署的全过程,需要的朋友可以参考一下我的流程,看这一篇文章就能成功部署项目啦. 写这个的本意是为备忘( 保姆级步骤),同时由于我在最开始部署项目时,没有找到一个 ...
- python django部署docker_Docker 部署 Django+Uwsgi+Nginx+Vue
简介 * 1.两年前接触docker是因为要部署selenium_gird,后来呢发现zalenium可以录制ui自动化视频的工具,都是直接pull镜像,启动容器.所以一直以来就只用到了pull. r ...
最新文章
- 各种经典透镜投影模型
- 使用C++ ostringstream来格式化字符串输出
- pandas使用read_csv函数读取文件并解析日期数据列(parse dates)、pandas使用read_csv函数读取文件并将缺失值转化为空字符串
- java对一个无序列表进行分组
- 合格linux运维人员必会的30道shell编程面试题及讲解
- [TimLinux] JavaScript 元素动态显示
- jquery 点击事件
- 多线程中ThreadLocal的使用
- oracle存储sql片段引入_强大的跨数据库访问组件 UniDAC使用教程:注释和SQL函数...
- 如何用Java打出DNA图案_DNA (Java代码)
- Windows系统下SSH客户端连接阿里云Linux服务器
- Codeforces Round #511 (Div. 1) 题解
- 代码生成工具系列-----代码生成工具(CodeEasy)介绍
- 拓端tecdat|R语言单变量和多变量(多元)动态条件相关系数DCC-GARCH模型分析股票收益率金融时间序列数据波动率
- 明明是旅游小程序却做起了内容电商?
- 2023年厦门大学应用统计专硕考研上岸经验分享
- 视频压缩编码和音频压缩编码的基本原理
- 高德 android 百度转高德,记一次百度和高德经纬度互转(不是你想的那样)
- SCI论文撰写——Conclusion
- 服务器amd cpu性能排行,2018年PC处理器性能排行:英特尔9代酷睿被AMD反超!
热门文章
- C语言求最大公约数3种方法
- 47_pytorch循环神经网络(学习笔记)
- OA中总结:s:select,关于使用modelDriven,项目分层,@Transactional,jspf,各个层上配置注解交给spring管理的方法,简单的OGNL表达式写法
- 窗口分析函数_10_计算组总和的占比
- 线性代数之行列式基础点
- python四种可变类型_SICP Python 描述 2.4 可变数据
- 动态库、静态库、运行时库、引入库之间的区别
- MFC动态链接库的制作步骤和使用方法
- Linux驱动编程 step-by-step (七) 并发 竞态 (信号量与自旋锁)
- NC88-寻找第K大的数