用Nginx 和 uWSGI 来部署Django

周末趁着加班空闲,找了一个旧机器,装了个ubuntu13.04,以后就可以用它来做我在公司的git 以及web服务器,顺便在它上面编译一下android源码,哈哈,很爽。

先说web系统,最进一直在捣鼓Django,之前用apache2+apache2-python-mod搭了一个简易版本的服务器,用起来感觉不爽,直接换成nginx和uwsgi

安装包

Django

sudo pip install django==1.6.2

其中pip是一个Python的包管理工具, 1.6.2是的版本,这种写法在bae的 requirements.txt中也有体现,比如需要安装django,可以用==,>,<等比较操作符来选择版本号。

uwsgi

$sudo pip install uwsgi

wsgi的全称是Web Server Gateway Interface,通过$django-admin.py startproject mysite会生成一个wsgi.py的文件,里面定义了一个application变量,uwsgi服务的对象就是它。

nginx

$sudo apt-get install nginx

单独使用uwsgi也可以让Django跑起来,但是它无法作为静态文件 /static/以及 /media/的服务器。因此我们需要配合使用nginx作为web服务器来处理/static/以及/media/的请求,而它其他的页面请求都转交给uwsgi来处理。

配置文件

uwsgi

  1. 在Django的工程目录下新建一个mysite_uwsgi.ini(名称可随意)
  2. 内容如下,详细说明可参考注释
    [uwsgi]
    
    # Django-related settings
    # the base directory (full path)
    chdir		= /path/to/your/project
    # Django's wsgi file
    module		= dijkstra.wsgi
    
    plugin		= python
    # process-related settings
    # master
    master		= true
    # maximum number of worker processes
    processes	= 5
    
    # the socket 
    socket		= /path/to/your/project
    
    # ... with appropriate permissions - may be needed
    chmod-socket	= 666
    # clear environment on exit
    vacuum		= true
    

根据uid的不同可以设置不同的socket权限,如果权限设置不当,uwsgi可能会出现permmision denied的错误。

nginx

  1. 在Django工程目录(与mysite_uwsgi.ini相同)新建一个mysite_nginx.conf文件
  2. 文件内容如下
    # dijkstra_nginx.conf
    
    # the upstream component nginx needs to connect to
    upstream django {
    	server unix:///home/lyndon/github/Mathematician/dijkstra/dijkstra.socket;
    	# server 127.0.0.1:8001
    }
    
    # Configuration of the server
    server {
    	# the port your site will be served on
    	listen		8000;
    	
    	# the domain name it will serve for
    	server_name	x.x.x.x # 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 /path/to/your/project/media;
    	}
    	
    	location /static {
    		alias /path/to/your/project/static;
    	}
    
    	# Finally. send all non-media requests to the Django server
    	location / {
    		uwsgi_pass django;
    		include /path/to/your/project/uwsgi_params;
    	}
    }
    
  3. uwsgi_params考到工程目录下cp /etc/nginx/uwsgi_params /path/to/your/project/

部署

修改mysite/settings.py

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['x.x.x.x', 'localhost'] # substitute your machine's IP address or FQDN

尝试访问

尝试能否访问localhost:8000,如果出现 502 bad gateway,可以重启机器。

开机启动

  • sudo ln -s /path/to/your/project/mysite_nginx.ini /etc/nginx/sites-enabled/
  • uwsgi的Emperor mode,可参考http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html

常用命令

  • $sudo service uwsgi start
  • $sudo service uwsgi stop
  • $sudo service uwsgi restart
  • $sudo service nginx start
  • $sudo service nginx stop
  • $sudo service nginx restart

参考文献

  • Django and nginx

你可能感兴趣的:(用Nginx 和 uWSGI 来部署Django)