virtualenv+gunicorn+supervisor+nginx 部署 django项目

virtualenv

virtualenv用于搭建虚拟环境,之后的一切操作最好都在虚拟环境下执行,可能用到的命令:

pip install virtualenv #安装

virtualenv path #指定路径文件夹创建虚拟环境

source env/bin/activate #激活虚拟环境,env为前面指定的文件夹

cd django-blog-tutorial #进入你的django项目

pip freeze > requirements.txt #生成项目依赖

pip install -r requirements.txt #安装项目依赖

python manage.py collectstatic  #收集静态文件

gunicorn

pip install gunicorn

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'gunicorn', # 在setting文件中添加

)

supervisor

  1. 在项目中创建文件:gunicorn.conf.py
#gunicorn.conf.py 

bind = "127.0.0.1:9090"
user = "root" #这句应该也可不要
  1. supervisor配置文件:
[program:blogproject]

command     = /home/ubuntu/sites/www.zhougb3.cn/env/bin/gunicorn -c /home/ubuntu/sites/www.zhougb3.cn/blogproject/blogProject/gunicorn.conf.py blogProject.wsgi:application
directory   = /home/ubuntu/sites/www.zhougb3.cn/blogproject
startsecs   = 3

redirect_stderr         = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups  = 10
stdout_logfile          = /home/ubuntu/sites/www.zhougb3.cn/blogproject/log/app.log

注意command命令,指定使用虚拟环境,指定读取文件去绑定端口。

使用以下命令查看是否配置成功:

netstat -lpnt #9090端口是否在监听

curl 127.0.0.1:9090 #是否显示出应有的页面内容

Nginx

配置文件如下:

server {
    listen      80;

    server_name www.zhougb3.cn;

    client_max_body_size 1m;

    access_log /home/ubuntu/sites/www.zhougb3.cn/blogproject/log/access_log;

    error_log  /home/ubuntu/sites/www.zhougb3.cn/blogproject/log/error_log;

    root /home/ubuntu/sites/www.zhougb3.cn/blogproject;

    #gzip            on;
    #gzip_min_length 1024;
    #gzip_buffers    4 8k;
    #gzip_types      text/css application/x-javascript application/json;

    sendfile on;

    location /static {
        alias /home/ubuntu/sites/www.zhougb3.cn/blogproject/static;
    }
   location / {
        proxy_pass       http://127.0.0.1:9090;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

static文件读取权限问题

正确配置之后,访问可以显示HTML页面,但是无法加载css样式,查了很多种方法。后面发现是nginx的权限问题。

#error.log
"/home/ubuntu/sites/www.zhougb3.cn/blogproject/staticblog/js/jquery-2.1.3.min.js" failed (13: Permission denied), client: 120.236.174.168, server: www.zhougb3.cn, request: "GET /static/blog/js/jquery-2.1.3.min.js HTTP/1.1", host: "www.zhougb3.cn", referrer: "http://www.zhougb3.cn/"

使用nginx -t显示得到nginx.conf路径,然后给配置文件添加’user root;’但是没有效果。同时该命令提示如下信息:

nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2017/12/10 10:43:21 [warn] 2991#2991: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2017/12/10 10:43:21 [emerg] 2991#2991: open() "/run/nginx.pid" failed (13: Permission denied)

后面经过查找,真的是文件夹权限问题,把最外面的文件夹权限改成777就可以了。


参考链接:

django部署

django部署

gunicorn.conf.py

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值