学习VirtualEnv和Nginx+uwsgi用于django项目部署
以下叙述中用到的操作系统:Linux CentOS 6.X。
最近几天了解一下VirtualEnv,Apache+Daemon mode,Nginx+uwsgi的概念,并且在项目中实验性部署了一下(目前我们的Django项目都使用Apache+mod_wsgi部署的,听说使用Nginx+uwsgi效率更高一些)。以下都是事后记录的安装部署过程,可能存在遗忘细节的地方。
VirtualEnv的作用:创建隔离的Python环境,解决模块或库的版本冲突或依赖。
在实际开发过程中,不同项目可能使用不同版本的库,例如,我们使用的Web框架Django,老项目使用的老的版本1.2.3,新项目使用的是1.4或1.5版本。那时我使用了最笨办法,即修改Django目录以切换,幸好没有使用太多多版本库,不然累死。
- 安装步骤(网上能找到很多安装VirtualEnv的URL,参考http://www.cnblogs.com/kym/archive/2011/12/29/2306428.html):
- easy_install virtualenv #安装
- virtualenv <myvenv> #创建虚拟环境
- 切换到虚拟环境<myvenv>目录运行source ./bin/activate #激活虚拟环境
- 可以在虚拟环境安装使用包,例如安装Django:easy_install Django
- 离开虚拟环境,使用命令:deactivate
- 应用虚拟环境
- 安装步骤(网上能找到很多安装VirtualEnv的URL,参考http://www.cnblogs.com/kym/archive/2011/12/29/2306428.html):
目前我们使用Apache+mod_wsgi部署Django项目的,项目可以通过以下方式使用虚拟环境:
WSGI作为Apache子进程运行
在httpd.conf中,可以VirtualHost上一行添加:WSGIPythonPath <myvenv path>/lib/python2.7/site-packages/.
WSGI作为独立daemon进程运行(参考http://modwsgi.readthedocs.org/en/latest/configuration-directives/WSGIDaemonProcess.html)
在WSGIDaemonProcess指令后添加:python-path=<myvenv path>/lib/python2.7/site-packages。
BTW:这种方式部署的时候,需要在httpd.conf中添加WSGISocketPrefix <full path>,否则web请求可能会503失败 (参考http://modwsgi.readthedocs.org/en/latest/configuration-directives/WSGISocketPrefix.html)
nginx可以作为WEB Server,反向代理,负载均衡等服务。
- 安装参考
nginx:http://wiki.nginx.org/Install,我选择的源码安装,安装后没有自启动的服务(网上有方法为nginx创建服务)
uwsgi:http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html#installing-uwsgi-with-python-support
- 为Django项目的配置
uwsgi的配置<project path>/uwsgi_XXX.ini如下(使用的是unix socke file方式进程通讯,在nginx也需要配相同路径文件)。注:如果要使用VirtualEnv虚拟环境,请设置home为虚拟环境路径。
# uwsgi_XXX.ini file
[uwsgi] # Django-related settings
# the base directory (full path)
chdir = /var/www/<project path>
# Django's wsgi file
module = <project name>.wsgi
# the virtualenv (full path)
home = <virtualenv path> uid=apache
gid=apache # process-related settings
# master
master = true
# maximum number of worker processes
processes =
# the socket (use the full path to be safe
socket = /var/www/<project path>/<project name>.sock
# ... with appropriate permissions - may be needed
# chmod-socket =
# clear environment on exit
vacuum = true daemonize = /var/log/uwsgi.log
stats = 0.0.0.0: pidfile=/var/www/<project path>/uwsgi.pid
另外需要在<project path>/<project name>/目录下创建文件wsgi.py。注:请记住添加PYTHON_EGG_CACHE,不然会报权限错误。
import os
os.environ['PYTHON_EGG_CACHE'] = '/<a path>/.python-eggs/' #为了防止Permission denied的web请求访问错误。
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "maps.settings") # This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
操作uwsgi进程(切换到<project path>目录下):
- 启动:uwsgi --ini uwsgi_XXX.ini
- 停止:uwsgi --stop uwsgi.pid
- reload: uwsgi --reload uwsgi.pid
#------------------------------------------------------------------------------------
nginx的配置如下(由于uwsgi配置使用unix socket file进程通讯的)
user apache; # 指定用户
worker_processes ;#启动2进程 pid logs/nginx.pid; #指定pid文件 events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; sendfile on;
keepalive_timeout ; #gzip on;
upstream django {
server unix:///var/www/<项目path>/XXX.sock; # for a file socket
#server 127.0.0.1:; # for a web port socket (we'll use this first)
} server {
listen ;# listen port
server_name localhost; charset utf-; #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
}
location /media {
alias /var/www/<项目path>/media;
}
location / {
uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed
}
操作nginx进程:
- 启动:/usr/local/nginx/sbin/nginx
- 停止:/usr/local/nginx/sbin/nginx -s stop
That's all.
学习VirtualEnv和Nginx+uwsgi用于django项目部署的更多相关文章
- nginx+uwsgi启动Django项目
1.安装项目环境 系统环境:ubuntu16.04 python环境:python3.5.2 Django版本:django1.11.7 nginx环境:nginx_1.10.3 虚拟环境:virtu ...
- nginx+uwsgi+flask+supervisor 项目部署
环境 - Linux: Ubuntu 16.04 - uWSGI 2.0.18 - Flask 1.0.2 - supervisor 3.2.0 - nginx/1.8.1 首先区分几个概念 WSGI ...
- 阿里云搭建nginx + uWSGI 实现 django 项目
系统版本 CentOS/7 64位 1.安装使用python3 创建python3目录 sudo mkdir /usr/local/python3 进入python3目录 cd /usr/local/ ...
- Django项目部署(django+guncorn+virtualenv+nginx)
一.说明 为了django项目部署到生产环境上,能够稳定的运行,且能够同时指出http和https的访问,对django的部署进行了一些研究,决定采用django + gunicorn + virtu ...
- 部署 Nginx +uwsgi+centos7+django+supervisor 项目
部署CRM项目 前言 使用软件 nginx 使用nginx是为了它的反向代理功能,项目会通过Django+uWSGI+Nginx进行服务器线上部署. uWSGI python web服务器开发使用WS ...
- CentOS 环境下基于 Nginx uwsgi 搭建 Django 站点
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,CentOS 环境下基于 Nginx uwsgi 搭建 Django 站点 以下 ...
- nginx + uWSGI 为 django 提供高并发
django 的并发能力真的是令人担忧,这里就使用 nginx + uwsgi 提供高并发 nginx 的并发能力超高,单台并发能力过万(这个也不是绝对),在纯静态的 web 服务中更是突出其优越的地 ...
- 使用gunicorn将django项目部署到生产环境的子目录下,在nginx后端获取客户真实IP地址
生产环境有时,并不是为了一个项目而存在的.毕竟,域名是比较稀有的. 今天遇到这个问题,解决了.作个记录. 并且,如果将django项目部署在Nginx后面,那如何获取用户真实的IP地址呢? 下面就来解 ...
- Dockerfile + Nginx.conf文件记录(用于前端项目部署)
Dockerfile + Nginx.conf文件记录(用于前端项目部署) 本教程依据个人理解并经过实际验证为正确,特此记录下来,权当笔记. 注:基于linux操作系统(敏感信息都进行了处理),默认服 ...
随机推荐
- How to solve Original Tango programmer”Hardware not Found”?
Original Tango programmer is a new generation of transponder programmer which is developed to cover, ...
- Creating a Mono 3 RPM on CentOS
Creating a Mono 3 RPM on CentOS A quick guide to creating an rpm of mono 3 from source, starting wit ...
- SSIS 学习(7):包配置(下)【转】
经过前面几个章节的学习,我们开发的ETL包算已经完成一大半了,但是还不够完美,正如一场足球比赛,前面大家打得很辛苦,传接得也很漂亮,但 是临门一脚的技术不过关,进不了球,一切都是白搭.今天我们就来为大 ...
- ORACLE 导入导出操作
1.导入命令: imp userId/psw@orcl full=y file=D:\data\xxx.dmp ignore=y 2.导出命令 exp userId/psw@orcl file=d: ...
- 锋利的jQuery第2版学习笔记4、5章
第4章,jQuery中的事件和动画 注意:使用的jQuery版本为1.7.1 jQuery中的事件 JavaScript中通常使用window.onload方法,jQuery中使用$(document ...
- 详解HTML<head> 头标签元素的意义以及使用场景
HTML<head>头部分的标签.元素有很多,涉及到浏览器对网页的渲染,SEO 等等,而各个浏览器内核以及各个国内浏览器厂商都有些自己的标签元素,这就造成了很多差异性.移动互联网时代,he ...
- Lombok 安装
Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注释实现这一目的.通过在开发环境中实现 Lombok ,开发人 ...
- HTTP - Session 机制
HTTP 是种无状态的协议,即使用 HTTP 协议时,每次发送请求都会产生对应的新响应,协议本身不会保留之前一切的请求或响应报文的信息.这是为了更快地处理大量事务,确保协议的可伸缩性,而特意把 HTT ...
- Nginx - Windows 环境安装 Nginx
1. 访问 http://nginx.org/en/download.html,下载 Windows 版本的安装包 2. 解压安装包,双击 nginx.exe,启动 nginx 3. 访问 http: ...
- Unity3d导入工程出现错误“Creating unique file”的解决方法
Unity3d导入工程出现错误“Creating unique file:creating file Temp/tempFile failed.Please ensure there is enoug ...