django+nginx+gunicorn+supervisro部署

一、nginx

1、yum install -y nginx  #默认安装后的配置文件路径:/etc/nginx/nginx.conf

2、新建项目的配置文件,因为默认配置文件会包含子配置文件,目录为:/etc/nginx/conf.d/*.conf

比如:新建 blog.conf,内容为:

server {
charset utf-8;
listen 80;
server_name app.mysite.com;

#找不到icon文件时不报错
location = /favicon.ico {access_log off;log_not_found off;}

#下面的静态文件路径后面不加/

location /static {

#这里的路径为django的配置文件settings.py中设置的静态文件收集目录:STATIC_ROOT = os.path.join(BASE_DIR,'collect_static')

alias /root/www/blog/blog/collect_static;
}
location /media {
alias /root/www/blog/blog/media/;
}

location / {

#下面两行为uwsgi模式
#include uwsgi_params;
#uwsgi_pass unix:/run/uwsgi/meikar.sock;

#下面的为使用gunicorn服务,其中8000为将来gunicorn服务端口
proxy_pass http://127.0.0.1:8000;  
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} #非静态文件或媒体文件,将由uwsgi处理

#websocket配置

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "upgrade";
}

二、gunicorn 

1、安装 pipenv install gunicorn

测试是否可以启动服务:gunicorn blog.wsgi:application --bind 0.0.0.0:8000

这时操作的目录应该在项目blog的根目录下,blog.wsgi:application这个库的调用才正确

三、supervisor

1、yum install supervisor  #安装

* 在centos8中,可能yum安装时会报错,这时安装源:yum install epel-release

2、默认配置文件路径为/etc/supervisro.conf,因为它可以管理多个进程,为了方便它默认包含自定义配置文件

/etc/supervisord.d/*.ini

这时新建自己的配置文件,如blog.ini

[program:meikar]
#这里的命令是启动gunicorn服务进程的命令,和上面gunicorn测试服务的一样 command
=/root/www/blog/.venv/bin/gunicorn -w2 -b0.0.0.0:8000 blog.wsgi:application #在执行命令前要切换到的目录,就是项目的根目录,manage.py所在的目录,只有这样,上面的blog.wsgi:application才能找到对应的模块文件 directory=/root/www/blog/blog startsecs=0 stopwaitsecs=0 autostart=true autorestart=true redirect_stderr=true stdout_logfile=/root/www/meikar/meikar/log/gunicorn.inf stderr_logfile=/root/www/meikar/meikar/log/gunicorn.err #如果日志中有中文会报错,所以设置为utf8 environment=LANG="en_US.utf8", LC_ALL="en_US.UTF-8", LC_LANG="en_US.UTF-8"

*启动supervisor:      

supervisord -c /etc/supervisord.conf

*停止

supervisorctl shutdown

如果报错:

第一种:Error: Cannot open an HTTP server: socket.error reported errno.ENOENT (2)

For help, use /usr/bin/supervisord -h

则:systemctl start supervisord

第二种:error: another program is already listening on a port that one of our HTTP servers is config...rvisord.

执行命令 find / -name supervisor.sock  然后 unlink /路径/supervisor.sock 最后再执行启动命令 systemctl start supervisord 

*设置开机启动:

systemctl enable supervisord

检查是否开机启动:systemctl is-enabled supervisord

查看状态:systemctl status supervisord.service

supervisorctl 常用命令

查看任务状态:supervisorctl status

启动任务:supervisorctl start <name>

停止任务:supervisorctl stop <name>

重启任务:supervisorctl restart <name>

清除日志文件:supervisorctl  clear <name>

清除多个日志文件:supervisorctl  clear <name> <name> 

清除所有日志文件:supervisorctl  clear all 

移除任务:supervisorctl  remove <name>

*/etc/supervisor.conf中有两种客户端管理方式,一种是TCP,一种是SOCK,只要配置一种即可

如果采用的是tcp方式,即网页的方式,使用了哪个端口要在防火墙中开放对应的端口,例如

firewall-cmd --permanent --zone=public --add-port=8080/tcp

firewall-cmd --reload

查看端口的开放情况:

firewall-cmd --list-ports

三、django

1、在根目录下收集静态文件

python manage.py collectstatic

这时会在根目录下生成settings.py中配置的静态文件目录collect_static(nginx的/static指定的目录就是这个)

接下来生成模型对应的表文件

python manage.py makemigrations

python manage.py migrate

有时APP对应的表没有生成:python manage.py makemigrations blog  然后再执行migrate迁移就可以了

***如果在生成数据库时报错:

Django报错django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
/venv/lib/site-packages/django/db/backends/mysql/base.py里面注释掉以下内容:if version < (1,3,13):下面的raise 行,或者修改为: pass
接下来会有:attributeError: 'str' object has no attribute 'decode',同样的目录路径下找到operations.py文件,把 decode修改为encode(errors='replace')
 ***如果使用到了redis,同时redis是密码连接,那么记住配置格式为:
CACHES = {
    "default": {
        "BACKEND""django_redis.cache.RedisCache",
        "LOCATION""redis://:requirepass的值@127.0.0.1:6379/1",# http://冒号:密码@ip:port/库号
        "OPTIONS": {
            "CLIENT_CLASS""django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {"max_connections"1000},
        },
    }
}
 
posted @ 2019-12-09 20:47  小小财经  阅读(246)  评论(0编辑  收藏  举报