闲逸笔记

keep it simple & stupid

Django 一机多网站部署:Debian + Nginx + uWSGI

部署 Django 网站是很烦的事情。最近成功在 Debian VPS 机器上部署成功,将步骤记录下来备份。(一台机器多个网站)

1. 安装环境

安装基本所需的软件:

$ sudo apt-get install nginx
$ sudo apt-get install build-essential python-dev
$ sudo apt-get install python-pip
$ sudo pip install uwsgi

然后安装 Django 及相关库。

2. 配置 Nginx

新建 websites.conf

$ sudo vi /etc/nginx/websites.conf


server {
    listen 80;

    server_name sleepycat.org www.sleepycat.org;
    charset     utf-8;

    if ($host = 'www.sleepycat.org'){
        rewrite ^/(.*)$ http://sleepycat.org/$1 permanent;
    }

    location / {
        root /website/sleepycat;
        uwsgi_pass 127.0.0.1:3031;
        include     /etc/nginx/uwsgi_params;
    }

    location /static/ {
        alias /website/sleepycat/static/;
        autoindex off;
    }

}

server {
    listen 80;

    server_name haveku.com www.haveku.com;
    charset     utf-8;

    location / {
        root /website/haveku;
        uwsgi_pass 127.0.0.1:3032;
        include     /etc/nginx/uwsgi_params;
    }

    location /static/ {
        alias /website/haveku/static/;
        autoindex off;
    }

}

保存后关联配置文件并重启服务:

$ sudo mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.bak
$ sudo ln -s /etc/nginx/websites.conf /etc/nginx/sites-enabled/default
$ sudo /etc/init.d/nginx restart

部分参数:

  • if ($host = 'www.sleepycat.org'){rewrite ^/(.*)$ http://sleepycat.org/$1 permanent;} 是配置去掉域名前的 www

  • loation /static/ 配置静态文件目录。

  • uwsgi_pass 127.0.0.1:3032; 每个域名的端口不一样。

3. 配置 uWSGI

$ vi sleepycat.ini
[uwsgi]
socket=127.0.0.1:3031
chdir=/website
pythonpath=/website/sleepycat
module=sleepycat.wsgi:application
env = DJANGO_SETTINGS_MODULE=sleepycat.settings
processes=2
threads=1
stats=127.0.0.1:9191
vacuum=true
master=true
daemonize=/tmp/uwsgi.log

参数:

  • processes/threads 分别表示进程、线程数。
  • 配置好 pythonpath 确保能找到 sleepycat.settings

其他域名类似。如:

$ vi haveku.ini
[uwsgi]
socket=127.0.0.1:3032
chdir=/website
pythonpath=/website/haveku
module=sleepycat.wsgi:application
env = DJANGO_SETTINGS_MODULE=mydomain2.settings
processes=2
threads=1
stats=127.0.0.1:9192
vacuum=true
master=true
daemonize=/tmp/uwsgi_mydomain2.log

4. 配置 vassals 与重启自动加载

配置 vassals

$ sudo mkdir /etc/uwsgi
$ sudo mkdir /etc/uwsgi/vassals
$ sudo ln -s /home/user/sleepycat.ini /etc/uwsgi/vassals/
$ sudo ln -s /home/user/haveku.ini /etc/uwsgi/vassals/
$ sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data

修改 rc.local,使机器重启时自动加载。

$ sudo vi /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data  --daemonize /home/user/emperor.log

exit 0

5. 配置 hosts 名

$ sudo vi /etc/hosts
127.0.0.1               localhost.localdomain localhost
127.0.0.1                    sleepycat.org
127.0.0.1                   haveku.com

6. 版本信息

Debian 7,Django 1.6


Permalink [http://sleepycat.org/blog/30]


发表评论

top bottom