您的位置:首页 > 运维架构 > Nginx

Django笔记 生产环境部署 gunicorn+nginx+supervisor

2016-04-15 12:56 851 查看
使用supervisor来调用gunicorn。

用gunicorn来启动django服务。

然后用nginx做反向代理,代理django的服务,即实现面向用户的服务器。

1.

virtualenv venv34blog -p /usr/bin/python3.4

2.

source venv34blog/bin/activet

3.

pip—

现在要将Django程序部署到生产环境上,在vps上来练习一下。

部署环境

Ubuntu 12.04 LTS

Python 2.7

使用virtualenv建立多个虚拟环境,python为3.4.

前提

跟开发环境一样,使用了virtualenv建立了单独的运行环境,并且可以启动服务器了。

1.gunicorn

在虚拟环境中安装gunicorn

1.pip install gunicorn
2.在setting文件中添加app
3.gunicorn
projectname.wsgi:application --bind 0.0.0.0:9090


然后cd projectname,gunicorn projectname.wsgi:application –bind 0.0.0.0:9090,绑定在本机上的9090端口

此时可以用netstat -lpnt查看端口占用情况

然后用curl 0.0.0.0:9090去测试,如果出现以下情况,说明成功了:

设置gunicorn.conf.py

2.supervisor

supervisor是一个监视和管理进程的工具,在进程当掉之后,可以重启。因此适合用此来启动服务器进程。

安装

在os的python2.7环境下,安装supervisor

配置文件

[program:myproject]
command=/root/env/venv34/bin/gunicorn -c /root/workspace/myproject/myproject/gunicorn.conf.py  myproject.wsgi:application
;directory=/opt/www/memo
user=system
autostart=true
autorestart=true
redirect_stderr=true
;environment=PYTHON_EGG_CACHE=/opt/www/memo/.python-eggs


/config/supervisor.conf

命令行

supervisord -c supervisor.conf                             通过配置文件启动supervisor
supervisorctl -c supervisor.conf status                    察看supervisor的状态
supervisorctl -c supervisor.conf reload                    重新载入 配置文件
supervisorctl -c supervisor.conf start [all]|[appname]     启动指定/所有 supervisor管理的程序进程
supervisorctl -c supervisor.conf stop [all]|[appname]      关闭指定/所有 supervisor管理的程序进程


3.nginx

安装

装nginx,此步适用ubuntu:
apt-get install nginx


配置

/etc/nginx


输入没有www的域名自动转向含有www的域名的配置方法如下

server {
listen       80;
server_name  example.com;
return       301 http://www.example.com$request_uri; }

server {
listen       80;
server_name  www.example.com;
...
}


命令行

nginx命令:
启动: /etc/init.d/nginx start
停止: /etc/init.d/nginx stop
测试配置文件是否正确: nginx -t
配置proxy功能:
重启nginx:nginx -s reload
nginx restart
访问ip即可


参考

VPS环境搭建详解(Virtualenv+Gunicorn+Supervisor+Nginx)

http://www.tuicool.com/articles/MVRze2

django 部署,gunicorn、virtualenv、nginx

http://www.cnblogs.com/tk091/p/3859514.html

http://www.jianshu.com/p/be9dd421fb8d

http://www.atmarkit.co.jp/ait/articles/1406/17/news013.html

http://www.atmarkit.co.jp/ait/articles/1407/24/news003.html

http://my.oschina.net/u/1241293/blog/383850

一个用户绑定多个域名

http://www.2cto.com/os/201411/355366.html

http://blog.csdn.net/zacklin/article/details/7859680

http://blog.csdn.net/nightelve/article/details/19265421

supervisor

http://blog.anatoo.jp/entry/20120310/1331321778

Nginx no-www to www and www to no-www

http://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django mac 博客