Diango + uwsgi + nginx项目部署的全过程(可外网访问)


Posted in Python onApril 22, 2018

前言

自己通过nginx uwsgi 部署django项目,查询了很多资料,遇到了很多问题,最终完成了部署,趁着心情愉悦,写个随笔,为曾像我一样苦寻解决方案的小伙伴们提供些思路。

方法如下

安装Nginx:

#安装nginx
sudo apt-get install nginx

#一些有用的命令
#启动nginx
sudo /etc/init.d/nginx start 
#重启nginx
 8sudo /etc/init.d/nginx restart
#停止nginx
sudo /etc/init.d/nginx stop

#很暴力的方法,我喜欢
sudo killall nginx

安装uwsgi:

pip install uwsgi
 
 #注意uwsgi需要在虚拟环境中运行

配置uwsgi:

#在项目目录中建立个conf文件夹,将nginx和uwsgi文件都放进去,不是必须#但是个好习惯

#my_uwsgi.ini
ite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir   = /to/your/project/#这个是项目的路径
# Django's wsgi file
module   = project.wsgi#这个project要换成自己的项目名,也就是uwsgi.py所在的文件夹名
# the virtualenv (full path)
home   = /home/ubuntu/.virtualenvs/虚拟环境名#这个就是虚拟环境的路径

# process-related settings
# master
master   = true
# maximum number of worker processes
processes  = 10
# the socket (use the full path to be safe
socket   = 127.0.0.1:8080#这个用来和Nginx对接,端口号可以改,本人项目将uwsgi作为本地服务,外网不能直接访问,用nginx作为代理,所以用本地的地址。
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum   = true
~

配置nginx

#以下内容在mysite_nginx.conf中,这个文件名也可以随意起
# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
 # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
 server 127.0.0.1:8080; #这个是用来跟uwsgi对接的,要和my_uwsgi.ini中写一致
}

# configuration of the server
server {
 # the port your site will be served on
 listen  8000;#这个端口是nginx用来监听uwsgi的,默认的是80,总之项目是通过下面的server_name:8000来访问的
 # the domain name it will serve for
 server_name xxx.xxx.xx.xx ; #这个ip就是服务器的ip
 charset  utf-8;

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

 # Django media
 location /media {
  alias /your/project/media; #这个目录是项目的meda目录
 }
 location /static {
  alias /your/project/static; # 这个目录是项目的static目录
 }

 # Finally, send all non-media requests to the Django server.
 location / {
  uwsgi_pass django;#这个是对接uwsgi的
  include  uwsgi_params; # 这个参数按我这样写nginx就能找到的
 }
}

将nginx配置文件链接到启动配置目录:

#注意修改下面的路径及文件名,哈哈不要只复制粘贴啊
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

修改django项目中的setting.py文件,添加

#要将STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static')]注释掉,Debug在生产模式也要改成False
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

将静态文件打包,让nginx代理:

python manage.py collectstatic

启动nginx,uwsgi

sudo /etc/init.d/nginx restart
#进入conf文件夹,或者说配置的uwsgi.ini文件所在目录
#uwsgi.ini改成自己的名字
uwsgi -i uwsgi.ini

访问:

ip:port(端口为nginx.conf中配置的)

总结:

写到这也差不多了,项目可以跑起来了,nginx,uwsgi高级配置还在学习中,希望本文对你有所帮助,谢谢。

最后再提醒下,网上有很多配置文件的模板,将我写注释的地方对比修改下,别遗漏。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

参考文档:https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

 http://uwsgi-docs.readthedocs.io/en/latest/Nginx.html

Python 相关文章推荐
Python爬取三国演义的实现方法
Sep 12 Python
Python中异常重试的解决方案详解
May 05 Python
python下setuptools的安装详解及No module named setuptools的解决方法
Jul 06 Python
Python2/3中urllib库的一些常见用法
Dec 19 Python
python实现C4.5决策树算法
Aug 29 Python
django配置连接数据库及原生sql语句的使用方法
Mar 03 Python
python实现nao机器人身体躯干和腿部动作操作
Apr 29 Python
Django更新models数据库结构步骤
Apr 01 Python
Windows 平台做 Python 开发的最佳组合(推荐)
Jul 27 Python
详解用python -m http.server搭一个简易的本地局域网
Sep 24 Python
python 实现socket服务端并发的四种方式
Dec 14 Python
用python自动生成日历
Apr 24 Python
Python3.6笔记之将程序运行结果输出到文件的方法
Apr 22 #Python
Python解决八皇后问题示例
Apr 22 #Python
Django重装mysql后启动报错:No module named ‘MySQLdb’的解决方法
Apr 22 #Python
对python中raw_input()和input()的用法详解
Apr 22 #Python
对Python3中的input函数详解
Apr 22 #Python
Python实现中一次读取多个值的方法
Apr 22 #Python
使用python编写udp协议的ping程序方法
Apr 22 #Python
You might like
利用中国天气预报接口实现简单天气预报
2014/01/20 PHP
Laravel利用gulp如何构建前端资源详解
2018/06/03 PHP
PHP检测一个数组有没有定义的方法步骤
2019/07/20 PHP
Laravel 6 将新增为指定队列任务设置中间件的功能
2019/08/06 PHP
如何使用Javascript获取距今n天前的日期
2013/07/08 Javascript
JavaScript中的原型和继承详解(图文)
2014/07/18 Javascript
从零学习node.js之搭建http服务器(二)
2017/02/21 Javascript
微信小程序动态生成二维码的实现代码
2018/07/25 Javascript
详解微信小程序之scroll-view的flex布局问题
2019/01/16 Javascript
Vue动态组件和异步组件原理详解
2019/05/06 Javascript
[01:00]DOTA2 store: Collection of Artisan's Wonders
2015/08/12 DOTA
浅谈Python 中整型对象的存储问题
2016/05/16 Python
Linux上安装Python的PIL和Pillow库处理图片的实例教程
2016/06/23 Python
python批量添加zabbix Screens的两个脚本分享
2017/01/16 Python
深入理解Python3 内置函数大全
2017/11/23 Python
Python框架Flask的基本数据库操作方法分析
2018/07/13 Python
基于python指定包的安装路径方法
2018/10/27 Python
python3 深浅copy对比详解
2019/08/12 Python
Python 元组操作总结
2019/09/18 Python
thinkphp5 路由分发原理
2021/03/18 PHP
HTML5微信播放全屏问题的解决方法
2017/03/09 HTML / CSS
美国第一香水网站:Perfume.com
2017/01/23 全球购物
Bailey帽子官方商店:Bailey Hats
2018/09/25 全球购物
美国在线健康和美容市场:Pharmapacks
2018/12/05 全球购物
Dower & Hall官网:英国小众轻奢珠宝品牌
2019/01/31 全球购物
如何利用cmp命令比较文件
2013/09/23 面试题
AJAX的全称是什么
2012/11/06 面试题
制药工程专业毕业生推荐信
2013/12/24 职场文书
评析教师个人的自我评价
2014/02/19 职场文书
行政专员岗位职责说明书
2014/07/30 职场文书
大二学生学年自我鉴定
2014/09/12 职场文书
代领学位证书毕业证书委托书
2014/09/30 职场文书
计划生育工作汇报
2014/10/28 职场文书
新党章的学习心得体会
2014/11/07 职场文书
幼儿园中班个人总结
2015/02/28 职场文书
乡镇团委工作总结2015
2015/05/26 职场文书