Django项目部署方案推荐

原因:在django1.8版本使用fastcgi就爆出了fastcgi在django1.9就会被移除的警告,也就是说django1.9还是可以使用fastcgi,而到了django1.9以后fastcgi就完全不能使用了。这个时候替代方法就是uwsgi.本文就是讲述如何部署django1.9以后的项目。

1 安装必要包:

安装supervisor:

sudo apt-get install supervisor

安装nginx:

sudo apt-get install nginx

安装django:

pip install django==1.9.0

安装uwsgi:

pip install uwsgi

注意:django和uwsgi如果在全局环境下安装需要加sudo

2 项目根目录下uwsgi.ini:

[uwsgi]

# 下面的二选一,选择不同的方式启动uwsgi,nginx里面的配置也会不同。

# http=127.0.0.1:4741

socket=/home/administrator/work_summary/work_summary.sock

# socket文件的权限,因为nginx读取时会因为权限发生错误。

# chmod-socket=666

# 项目根目录

chdir=/home/administrator/work_summary

# django项目里面的wsgi.py文件在项目目录下的路径

wsgi-file=work_summary/wsgi.py

# 虚拟环境路径

home=/home/administrator/env_django1.9

# http请求的buffer空间

buffer-size=65535

# 进程数

processes=10

# 以主进程开启

master=true

# 线程数

threads=4

#当服务器退出的时候自动删除unix socket文件和pid文件。

vacuum=true

3 /etc/supervisor/conf.d/work_summary.conf配置:

[program:work_summary]

command=uwsgi --ini /home/administrator/work_summary/work_summary.ini

process_name=%(program_name)s

numprocs=1

umask=022

autostart=true

autorestart=true

startsecs=10

startretries=3

exitcodes=0,2

stopsignal=INT

stopwaitsecs=10

user=administrator

redirect_stderr=true

stdout_logfile=/home/administrator/work_summary/debug

stdout_logfile_maxbytes=1MB

stdout_logfile_backups=10

stdout_capture_maxbytes=1MB

stderr_logfile=/home/administrator/work_summary/err

stderr_logfile_maxbytes=1MB

stderr_logfile_backups=10

stderr_capture_maxbytes=1MB

environment=A="1", B="2"

serverurl=AUTO

4 nginx配置:

upstream django{

# uwsgi_pass 127.0.0.1:4741;

server unix:///home/administrator/work_summary/work_summary.sock;

}

server

{

listen 80;

server_name server.name.cn;

root html;

index index.html;

location /

{

alias /home/administrator/work_summary/templates/;

}

location /api/

{

uwsgi_pass django;

include uwsgi_params;

}

}

5 启动项目:

使用supervisor启动django项目:

# 进入supervisor管理终端

sudo supervisorctl

# 更新配置信息

update

# 按照配置文件启动服务

start work_summary


# 重启nginx:

sudo service nginx restart

你可能感兴趣的:(Django项目部署方案推荐)