一、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},
        },
    }
}
 

django+nginx+gunicorn+supervisro部署的更多相关文章

  1. flask +gevent+nginx+Gunicorn+supervisor部署flask应用

    上篇   可以完美部署flask ,但是视乎在结合gevent+apscheduler 实现异步非阻塞后台和定时任务的时候视乎不是那么完美.请教了前辈,决定使用flask+gevent+nginx+g ...

  2. django+nginx+uwsgi_cent0s7.4 部署

    django+nginx+uwsgi_cent0s7.4 部署 几条命令 # 查看是否有 uwsgi 相关的进程 ps -aux|grep "uwsgi" # 杀死有关 uwsgi ...

  3. 2020最新nginx+gunicorn+supervisor部署基于flask开发的项目的生产环境的详细攻略

    本攻略基于ubuntu1804的版本,服务器用的华为云的服务器,python3(python2已经在2020彻底停止维护了,所以转到python3是必须的)欢迎加我的QQ6398903,或QQ群讨论相 ...

  4. django+nginx+uwsgi 项目部署

    Django虽然自带一个Server,但只能作为开发时测试使用,我们需要一个可以稳定而持续的服务器对网站进行部署,比如Apache, Nginx, lighttpd等,本篇将利用nginx和uWSGI ...

  5. CentOS 下部署Nginx+Gunicorn+Supervisor部署Flask项目

    原本之前有一部分东西是在Windows Server,但是由于Gunicorn不支持Windows部署起来颇为麻烦.最近转战CentOS,折腾一段时间,终于简单部署成功.CentOS新手,作为一个总结 ...

  6. Nginx+Gunicorn+Supervisor部署Flask应用

    Flask 内置了简单的 Web 环境,让我们在开发的时候只需要专注于应用实现,而真正要在生产环境运行时这个简单的 Web 环境就不够用了,还需要一系列操作才能让 Web 应用高效的运行起来.现在记录 ...

  7. 给我一台全新的服务器,使用nginx+gunicorn+supervisor部署django

    0.准备工作 在一台全新的服务器中新建用户以及用户的工作目录,之后的操作都以这个用户的身份进行,而不是直接用root. 举个栗子: 在服务器下新建用户rinka并赋予sudo权限 1) root登陆, ...

  8. django+nginx+gunicorn+pipenv微信小程序实践笔记

    一.我采用pipenv来管理虚拟环境,在本地新建虚拟环境: mkdir wxProject #进入环境目录,创建虚拟环境 pipenv install #激活虚拟环境 pipenv shell #然后 ...

  9. Django + nginx + uswgi 的部署总结

    一.引言 自己小组内写了一个网站,需要部署到远程服务器,搜索了好多资料,但是大部分资料都比较繁琐,并且没有一个教程能够直接从头到尾适合,在部署过程中,我是按照很多教程然后综合试验着逐渐部署成功,其中有 ...

随机推荐

  1. INNER JOIN & OUTER JOIN

    INNER JOIN & OUTER JOIN 参考:sql

  2. CPU构架和工作原理

    -- CPU -- -- CPU 由三部分组成:时钟:控制单元:算术逻辑单元 -- -- -- 时钟:对CPU内部操作与系统其他硬件进行同步: -- -- -- 控制单元:控制机器指令的执行顺序: - ...

  3. dfs--八皇后问题

    在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合法的放置方法. 因为我们 ...

  4. javascript中的location的用法

    javascript中的location.href有很多种用法,主要如下. self.location.href="/url" 当前页面打开URL页面 location.href= ...

  5. python 运算符 取余 取商 in not in

    #运算符sum = 9//2 #取商print(sum) sum = 9%2 #取余print(sum) #inname1 = '小林'name2 = '林倩'if '林' in name1: pri ...

  6. PAT Basic 1047 编程团体赛(20) [Hash散列]

    题目 编程团体赛的规则为:每个参赛队由若⼲队员组成:所有队员独⽴⽐赛:参赛队的成绩为所有队员的成绩和:成绩最⾼的队获胜.现给定所有队员的⽐赛成绩,请你编写程序找出冠军队. 输⼊格式: 输⼊第⼀⾏给出⼀ ...

  7. python编程:从入门到实践----第六章:字典>练习

    6-1 人:使用一个字典来存储一个熟人的信息,包括名.姓.年龄和居住的城市.该字典应包含键first_name .last_name .age 和city .将存储在该字典中的每项信息都打印出来. f ...

  8. java通过HSSFWorkbook导出xls文件

    使用swgger2.Restlet等接口工具有bug导致导出失败,测试直接使用浏览器 //导出列表-新 @UserRoleJudgment(authpos = SystemControllerLog. ...

  9. Java集合详解(全)

    Java的集合主要有List , Set, Map List , Set继承至Collection接口,Map为独立接口 List下有ArrayList,LinkedList,Vector Set下有 ...

  10. Python 生成requirements文件以及使用requirements.txt部署项目

    生成requirements.txt 当你的项目不再你的本地时,为了方便在新环境中配置好环境变量,你的项目需要一个记录其所有依赖包以及它们版本号的文件夹requirements.txt 文件. pip ...