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 相关文章推荐
简单的通用表达式求10乘阶示例
Mar 03 Python
Python3.x版本中新的字符串格式化方法
Apr 24 Python
使用Python的Twisted框架编写非阻塞程序的代码示例
May 25 Python
Python简单实现Base64编码和解码的方法
Apr 29 Python
Python利用ElementTree模块处理XML的方法详解
Aug 31 Python
Python通过TensorFlow卷积神经网络实现猫狗识别
Mar 14 Python
详解python执行shell脚本创建用户及相关操作
Apr 11 Python
django框架使用orm实现批量更新数据的方法
Jun 21 Python
Python 3 判断2个字典相同
Aug 06 Python
Numpy中ndim、shape、dtype、astype的用法详解
Jun 14 Python
Python爬虫小例子——爬取51job发布的工作职位
Jul 10 Python
python实现进度条的多种实现
Apr 29 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中使用CURL伪造来路抓取页面或文件
2011/05/04 PHP
PHP笔记之:基于面向对象设计的详解
2013/05/14 PHP
高质量PHP代码的50个实用技巧必备(上)
2016/01/22 PHP
用js实现的仿sohu博客更换页面风格(简单版)
2007/03/22 Javascript
jquery BS,dialog控件自适应大小
2009/07/06 Javascript
JS操作Cookies包括(读取添加与删除)
2012/12/26 Javascript
innerText 使用示例
2014/01/23 Javascript
JavaScript分秒倒计时器实现方法
2015/02/02 Javascript
javascript实现相同事件名称,不同命名空间的调用方法
2015/06/26 Javascript
javascript实现选中复选框后相关输入框变灰不可用的方法
2015/08/11 Javascript
jQuery事件_动力节点Java学院整理
2017/07/05 jQuery
jQuery图片查看插件Magnify开发详解
2017/12/25 jQuery
浅谈Koa2框架利用CORS完成跨域ajax请求
2018/03/06 Javascript
深入浅出 Vue 系列 -- 数据劫持实现原理
2019/04/23 Javascript
bootstrap-table formatter 使用vue组件的方法
2019/05/09 Javascript
node.js中npm包管理工具用法分析
2020/02/14 Javascript
Vue中通过vue-router实现命名视图的问题
2020/04/23 Javascript
JS实现密码框效果
2020/09/10 Javascript
python 远程统计文件代码分享
2015/05/14 Python
基于Python的接口测试框架实例
2016/11/04 Python
详解python之配置日志的几种方式
2017/05/22 Python
完美解决安装完tensorflow后pip无法使用的问题
2018/06/11 Python
实践Vim配置python开发环境
2018/07/02 Python
idea创建springMVC框架和配置小文件的教程图解
2018/09/18 Python
python匹配两个短语之间的字符实例
2018/12/25 Python
Pyqt5 实现跳转界面并关闭当前界面的方法
2019/06/19 Python
如何爬取通过ajax加载数据的网站
2019/08/15 Python
HTML5标签大全
2016/11/23 HTML / CSS
canvas实现滑动验证的实现示例
2020/08/11 HTML / CSS
GUESS德国官网:美国牛仔服装品牌
2017/02/14 全球购物
佛罗里达州印第安河新鲜水果:Hale Groves
2017/02/20 全球购物
SQL Server数据库笔试题和答案
2016/02/04 面试题
社区庆中秋节活动方案
2014/02/07 职场文书
QT与javascript交互数据的实现
2021/05/26 Javascript
详解python网络进程
2021/06/15 Python
mysql 8.0.27 绿色解压版安装教程及配置方法
2022/04/20 MySQL