测试驱动开发(Django)10

第10章 为部署到生产环境做好准备

10.1 换用Gunicorn

Django的吉祥物是一匹小马。现在有一只小马了,但更需要一头“绿色独角兽”--Guincorn(green unicorn)。

服务器:cd /home/sites/superlists-staging.com/source

             ../virtualenv/bin/pip install gunicorn

Guincorn需要知道WSGI服务器的路径。可以通过application函数获取。Django在文件project/wsgi.py中提供了这个函数。

../virtualenv/bin/gunicorn TDDweb.wsgi:application

功能测试:STAGING_SERVER=127.0.0.1 python manage.py test functional_tests        #服务器域名或IP地址

AssertionError: 86.66666412353516 != 512 within 10 delta        #不能显示css

Django开发服务器可以伺服静态文件,但Guincorn不能伺服静态文件。

10.2 让Nginx伺服静态文件

集中静态文件:../virtualenv/bin/python manage.py collectstatic --noinput

结果:17 static files copied to '/home/sites/superlists-staging.com/static'.

查看:ls ../static

修改 /usr/local/nginx/conf/sites/lists_site.conf         #将所有网站配置当在sites目录中

server {
    listen 80;
    server_name '域名或IP';

    location /static {
        alias /home/sites/superlists-staging.com/static;
}
    location / {
        proxy_pass http://localhost:8000;
        }
}

重启nginx和guincorn

功能测试通过。

10.3 换用Unix套接字

修改 sites/lists_site.conf

location / {
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/【域名或IP】.socket;
        }

重启:/usr/local/nginx/sbin/nginx -s reload

../virtualenv/bin/gunicorn --bind unix:/tmp/【域名或IP】.socket TDDweb.wsgi:application

单元测试:STAGING_SERVER=【域名或IP】 python manage.py test functional_tests

通过

10.4 把DEBUG设为False,设置ALLOWED_HOSTS

服务器:settings.py 

DEBUG = False

ALLOWED_HOSTS = ['域名或IP']
 

重启服务器并进行单元测试

10.5 设置开机启动Gunicorn

服务器使用的系统是centos 6版本,如果是7以上或者ubantu使用systemd。

mkdir /home/centent

touch touch lists-site.sh

vim lists-site.sh

#!/bin/bash
/usr/local/nginx/sbin/nginx
cd /home/sites/superlists-staging.com/source
/home/sites/superlists-staging.com/virtualenv/bin/gunicorn --bind unix:/tmp/47.105.180.65.socket TDDweb.wsgi:application

chmod +x lists-site.sh

vim /etc/rc.d/rc.local

touch /var/lock/subsys/local
/home/centent/lists-site.sh

回到本地仓库:

pip install gunicorn

pip freeze | grep gunicorn >> requirements.txt

Add gunicorn to virtualenv requirements

git push origin_1 master

10.6 考虑自动化

project目录下创建:deploy_tools

三个文件:

deploy_tools/list_site.conf:

server {
    listen 80;
    server_name 【域名】;

    location /static {
        alias /home/sites/superlists-staging.com/static;
}
    location / {
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/47.105.180.65.socket;
        }
}

deploy_tools/lists-site.sh:

#!/bin/bash
/usr/local/nginx/sbin/nginx
cd /home/sites/superlists-staging.com/source
/home/sites/superlists-staging.com/virtualenv/bin/gunicorn --bind unix:/tmp/【域名】.socket TDDweb.wsgi:application

deploy_tools/provisioning_notes.md:

配置网站
=============

##需要的包
* Nginx
* python3.6
* virtualenv + pip
* git

##Nginx虚拟主机
* 参考list_site.conf
* 把SITENAME替换为所需域名

##开机启动
* 参考lists-site.sh
* 把SITENAME替换为所需域名

##文件夹结构


/home/username
  └── sites
         │──SITENAME           
             ├── database
             ├── source
             ├── static
             └── virtualenv

 提交:git commit -m 'Notes and template config file for provisioning'

10.7 保存进度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值