利用Gunicorn和Nginx布局Django网站

来源:互联网 发布:php 自动化部署工具 编辑:程序博客网 时间:2024/06/06 14:02

首先在服务器上安装Django,Nginx 和Gunicorn
Nginx
Nginx的配置地址位于/etc/nginx/sites-available/default
一般而言,如下配置即可快速布局

 server {    listen 80;    server_name example.org;    access_log  /var/log/nginx/example.log;    location / {        proxy_pass http://127.0.0.1:8000;        proxy_set_header Host $host;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    }  }

值得一提的是,server_name 一般而言是写自己的域名,但如果是写IP地址的话,需要在django的setting.py中将该IP添加入allowed_host,类似于白名单。

service nginx start
nginx -s reload

来操控这个服务,可以使用PS -E查看服务,以及 netstat -ntpl查看端口占用
Gunicorn
直接对于在Django的project文件夹内与managy.py 同目录执行

gunicorn appname.wsgi

appname 用本项目的名称替代
默认监听端口是128.0.01:8000
如需修改则gunicorn appname.wsgi:application --bind 0.0.0.0:9090 至此,直接键入网址即可登录网站。

使用supervisor管理后台进程

sudo pip install supervisor

配置程序

echo_supervisord_conf > /etc/supervisord.conf
nano /etc/supervisord.conf

修改如下并添加自己的进程:

[supervisord]nodaemon=true[program:django]command = gunicorn django_website.wsgidirectory=/Django/django_websiteautostart=trueautorestart=truestdout_logfile=/Django/django_website/log/gunicorn.logstderr_logfile=/Django/django_website/log/gunicorn.err

启动服务

supervisord -c /etc/supervisord.conf

注意关闭窗口时使用exit退出,不要右上角红叉,会杀死这个进程导致后面无法运行
在supervisord 运行期间可以用如下命令控制

sudo supervisorctl statussudo supervisorctl stop djangosudo supervisorctl start django
0 0
原创粉丝点击