以下叙述中用到的操作系统:Linux CentOS 6.X。

最近几天了解一下VirtualEnv,Apache+Daemon mode,Nginx+uwsgi的概念,并且在项目中实验性部署了一下(目前我们的Django项目都使用Apache+mod_wsgi部署的,听说使用Nginx+uwsgi效率更高一些)。以下都是事后记录的安装部署过程,可能存在遗忘细节的地方。

  1. VirtualEnv

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
    • 应用虚拟环境

   目前我们使用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

  1. nginx&& uwsgi

  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.

04-25 04:33
查看更多