Ubuntu 14.04+Django 1.7.1+Nginx+uwsgi部署教程


Posted in Python onNovember 18, 2014

具体环境:
Ubuntu 14.04 Python 2.7.6 Django 1.7.1 Virtualenv name:test Nginx uwsgi

假设 项目文件夹位于 /data/www/ts 设置保存在 ./conf

virtualenv name = test

domain name = example.com

django+uwsgi的部署实在是太蛋疼了..网上已有的教程似乎有新版本的兼容问题。最后跑到uwsgi官网上找的教程终于跑通了..
不过官网的教程似乎有引导教学性质,部署的时候就显得很绕弯路,在这里记录下来精简内容

http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html

首先,需要一个uwsgi_params文件,放在项目的conf文件夹里面。之后需要指向它。文件内容如下:

uwsgi_param QUERY_STRING $query_string;

uwsgi_param REQUEST_METHOD $request_method;

uwsgi_param CONTENT_TYPE $content_type;

uwsgi_param CONTENT_LENGTH $content_length;

uwsgi_param REQUEST_URI $request_uri;

uwsgi_param PATH_INFO $document_uri;

uwsgi_param DOCUMENT_ROOT $document_root;

uwsgi_param SERVER_PROTOCOL $server_protocol;

uwsgi_param HTTPS $https if_not_empty;

uwsgi_param REMOTE_ADDR $remote_addr;

uwsgi_param REMOTE_PORT $remote_port;

uwsgi_param SERVER_PORT $server_port;

uwsgi_param SERVER_NAME $server_name;

创建一个叫做ts_nginx.conf 的文件,内容如下

# ts_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:8001; # for a web port socket (we'll use this first)

}
# configuration of the server

server {

    # the port your site will be served on

    listen 80;

    # the domain name it will serve for

    server_name .example.com; # substitute your machine's IP address or FQDN

    charset utf-8;
    # max upload size

    client_max_body_size 75M; # adjust to taste
    # Django media

    location /media {

        alias /data/www/ts/media; # your Django project's media files - amend as required

    }
    location /static {

        alias /data/www/ts/static; # your Django project's static files - amend as required

    }
    # Finally, send all non-media requests to the Django server.

    location / {

        uwsgi_pass django;

        include /data/www/ts/conf/uwsgi_params; # the uwsgi_params file you installed

    }

}

把这个conf文件连接到nginx的搜索目录里面。

sudo ln -s /data/www/ts/conf/ts_nginx.conf /etc/nginx/sites-enabled/

先决条件:这里要设置好django项目的settings里面static files

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

and then run
python manage.py collectstatic

之后:

service nginx restart

应该就可以看到
http://example.cn:8000/media/1.gif
了(事先放进去一个静态文件)

之后的blabla步骤都是废话,跳到这里:

Configuring uWSGI to run with a .ini file

ts_uwsgi.ini 在项目根目录

[uwsgi]

# Django-related settings

# the base directory (full path)

chdir = /data/www/ts

# Django's wsgi file

module = ts.wsgi

# the virtualenv (full path)

home = /root/.envs/test

# 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:8001

# ... with appropriate permissions - may be needed

# chmod-socket = 664

# clear environment on exit

vacuum = true

# set an environment variable

env = DJANGO_SETTINGS_MODULE=conf.settings 

uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

这里环境变量设置env需要conf文件夹有init.py,否则conf不会被认为是module

(目前除了80端口,其他端口都可以通过地址:端口访问。已经测试8000,81.80测试不知道为什么不成。明天待续)

Python 相关文章推荐
python实现DNS正向查询、反向查询的例子
Apr 25 Python
举例详解Python中的split()函数的使用方法
Apr 07 Python
Python中的pass语句使用方法讲解
May 14 Python
一个基于flask的web应用诞生 bootstrap框架美化(3)
Apr 11 Python
Django 实现下载文件功能的示例
Mar 06 Python
python使用turtle绘制国际象棋棋盘
May 23 Python
Django在pycharm下修改默认启动端口的方法
Jul 26 Python
Pytorch的mean和std调查实例
Jan 02 Python
基于Python3.6中的OpenCV实现图片色彩空间的转换
Feb 03 Python
python如何把字符串类型list转换成list
Feb 18 Python
Python itertools.product方法代码实例
Mar 27 Python
python 模拟登陆163邮箱
Dec 15 Python
python服务器与android客户端socket通信实例
Nov 12 #Python
Python访问MySQL封装的常用类实例
Nov 11 #Python
python实现ipsec开权限实例
Nov 11 #Python
python获取文件后缀名及批量更新目录下文件后缀名的方法
Nov 11 #Python
python实现在目录中查找指定文件的方法
Nov 11 #Python
Python实现list反转实例汇总
Nov 11 #Python
用python实现面向对像的ASP程序实例
Nov 10 #Python
You might like
PHP自定session保存路径及删除、注销与写入的方法
2014/11/18 PHP
flash javascript之间的通讯方法小结
2008/12/20 Javascript
Extjs学习过程中新手容易碰到的低级错误积累
2010/02/11 Javascript
js中的push和join方法使用介绍
2013/10/08 Javascript
二叉树的非递归后序遍历算法实例详解
2014/02/07 Javascript
JavaScript列表框listbox全选和反选的实现方法
2015/03/18 Javascript
JavaScript+CSS实现仿Mootools竖排弹性动画菜单效果
2015/10/14 Javascript
JavaScript下的时间格式处理函数Date.prototype.format
2016/01/27 Javascript
JavaScript实现图片自动加载的瀑布流效果
2016/04/11 Javascript
详解JavaScript中数组的reduce方法
2016/12/02 Javascript
详解基于Bootstrap+angular的一个豆瓣电影app
2017/06/26 Javascript
Angular中ng-repeat与ul li的多层嵌套重复问题
2017/07/24 Javascript
9种使用Chrome Firefox 自带调试工具调试javascript技巧
2017/12/22 Javascript
VUE中使用HTTP库Axios方法详解
2020/02/05 Javascript
Vue 实现一个简单的鼠标拖拽滚动效果插件
2020/12/10 Vue.js
win与linux系统中python requests 安装
2016/12/04 Python
Python操作Redis之设置key的过期时间实例代码
2018/01/25 Python
Centos下实现安装Python3.6和Python2共存
2018/08/15 Python
浅谈python在提示符下使用open打开文件失败的原因及解决方法
2018/11/30 Python
解决python中无法自动补全代码的问题
2018/12/04 Python
python SQLAlchemy 中的Engine详解
2019/07/04 Python
Python测试Kafka集群(pykafka)实例
2019/12/23 Python
基于python修改srt字幕的时间轴
2020/02/03 Python
Tensorflow全局设置可见GPU编号操作
2020/06/30 Python
HTML 5.1来了 9月份正式发布 更新内容预览
2016/04/26 HTML / CSS
国外平面设计第一市场:99designs
2016/10/25 全球购物
菲律宾酒店预订网站:Hotels.com菲律宾
2017/07/12 全球购物
缅甸网上购物:Shop.com.mm
2017/12/05 全球购物
Brasty罗马尼亚:购买手表、香水、化妆品、珠宝
2020/04/21 全球购物
毕业自荐书
2013/12/09 职场文书
食堂厨师岗位职责
2014/08/25 职场文书
计划生育诚信协议书
2014/11/02 职场文书
2015年生产部工作总结范文
2015/05/25 职场文书
2016教师暑期培训学习心得体会
2016/01/09 职场文书
《极主夫道》真人电影正式预告 定档6月3日上映
2022/04/05 日漫
解决WINDOWS电脑开机后桌面没有任何图标
2022/04/09 数码科技