Centos下Nginx+Gunicorn+Django+supervisor项目部署
-
安装需要的包
yum install python3 yum install nginx yum install gunicorn pip install gevent pip install supervisorgunicorn的简单使用,在服务器上任意一个位置新建一个python文件, 文件名自己定义:
vi testgunicorn.py # 在testgunicorn.py中输入以下内容 def app(environ, start_response): data = b"Hello, World!\n" start_response("200 OK", [("Content-Type", "text/plain"),("Content-Length", str(len(data)))]) return iter([data])使用gunicorn运行testgunicorn.py, 在testgunicorn.py文件的当前目录输入下面命令:
gunicorn -w 2 -b 0.0.0.0:18000 testgunicorn:app在浏览器输入 外网ip:端口 , 如果出现"Hello, World!"即表示gunicorn正常
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-arRdh2N4-1629099220257)(E:\notes_daily\assets\image-20210816142111324.png)]](https://i-blog.csdnimg.cn/blog_migrate/d19b7986c20aa56df07aa874fb54592b.png)
-
配置nginx文件
# 新建一个配置文件,自定义名字,比如命名为hello_nginx.conf touch /etc/nginx/conf.d/hello_nginx.conf # 在/etc/nginx/conf.d/hello_nginx.conf里输入以下内容 server { listen 11111; server_name second.sunshine.site; charset utf-8; location / { proxy_pass http://0.0.0.0:17881; # 这里要配合启动文件使用 proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location /static { alias /root/hello/static/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }对文件里面配置进行解释:
server { listen 11111; server_name second.sunshine.site; # 访问项目的时候是在浏览器输入: second.sunshine.site:11111,second.sunshine.site是我的域名,也可以换成外网ip, charset utf-8; location / { proxy_pass http://0.0.0.0:17881; # 浏览器输入: second.sunshine.site:11111会去访问本地的17881端口起的进程 proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } location /static { alias /root/hello/static/; # 托管这个路径下hello项目的静态文件 } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } -
将django项目和nginx连接在一起
(1)在项目根目录创建一个gunicorn.py文件.(就是有manage.py的那个目录) vi gunicorn.py (2)在文件中输入以下内容 # coding:utf-8 import multiprocessing bind = '0.0.0.0:17881' #绑定ip和端口号 backlog = 512 #监听队列 chdir = '/root/hello' #gunicorn要切换到的目的工作目录, 项目的根目录 timeout = 30 #超时 worker_class = 'gevent' #使用gevent模式,还可以使用sync 模式,默认的是sync模式 workers = multiprocessing.cpu_count() * 2 + 1 #进程数 threads = 2 #指定每个进程开启的线程数 loglevel = 'info' #日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置 access_log_format = '%(t)s %(p)s %(h)s "%(r

本文详细介绍了如何在Centos系统中部署Django项目,通过Nginx、Gunicorn和Supervisor实现高可用的服务器配置。涉及到的关键步骤包括:安装所需软件包,使用Gunicorn启动Django应用,配置Nginx以代理请求,以及设置Supervisor管理Gunicorn进程。在部署过程中可能会遇到greenlet冲突,需要适当地处理和升级gevent。
最低0.47元/天 解锁文章
2102

被折叠的 条评论
为什么被折叠?



