一、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. 元祖&字典

    #什么是元祖:元祖是一个不可变的列表(没有改的需求) #======================================基本使用============================== ...

  2. 树上问题&图论模板整理

    去除过水的模板,包括但不限于dijkstra(甚至堆优化都被过滤了).SPFA.kruskal.拓扑排序等. 欧拉回路:http://uoj.ac/problem/117 #include<bi ...

  3. h5-圆角的使用-案例安卓机器人

    1.圆角的使用 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  4. image compression with libjpeg

    http://www.aaronmr.com/en/2010/03/test/ Working on the project I've seen in the need for compression ...

  5. 正确返回Unicode码点大于0xFFFF的字符串长度

    如下: function codePointLength(text){ var result = text.match(/[\s\S]/gu); return result? result.lengt ...

  6. 吴裕雄--天生自然 JAVA开发学习:String 类

    public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'r', 'u', 'n' ...

  7. PAT Basic 反转链表 (25) [链表]

    题目 给定⼀个常数K以及⼀个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4:如果K为4,则输出应该为4→3→2→1→5→6, ...

  8. UML-领域模型-如何找到概念类

    有3个方法 方法1:对已有的重用和修改(这是最好的方法) 重用和修改现有模型.这些模型来源于之前的项目.网上的 方法2:使用分类列表 从网上搜索该领域的常见分类,或参考书籍Martin Fowler的 ...

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

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

  10. java使用forEach填充字典值

    // 填充字典值 Vector vector = vectorMapper.selectByPrimaryKey(id); VectorModel vectorModel = new VectorMo ...