前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Django Uwsgi Nginx

Django Uwsgi Nginx

原创
作者头像
vanguard
修改2021-01-21 10:53:29
6040
修改2021-01-21 10:53:29
举报
文章被收录于专栏:vanguard

Nginx

编译安装 Nginx

代码语言:shell
复制
# 依赖较多 gcc gcc-c++ autoconf automake openssl
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0
# 配置并安装
cd nginx-1.18.0
./configure
make && make install

工具安装 -- 推荐

代码语言:shell
复制
# 配置yum仓库
cd /etc/yum.repos.d/
vim nginx.repo
# [nginx]
# name=nginx repo
# baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
# gpgcheck=0
# enabled=1

开始安装

代码语言:shell
复制
# centos yum 安装
# 准备 PERE zlib openssl 等等依赖
# yum install -y gcc gcc-c++ ncurses-devel perl
# yum install -y pcre pcre-devel zlib  zlib-devel openssl openssl-devel
yum install -y nginx
# ubuntu apt-get 安装
sudo apt-get install nginx
# macos brew 安装
brew install nginx

运行管理,现实防火墙准备工作

代码语言:shell
复制
# 防火墙开放80端口
#which iptables
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/rc.d/init.d/iptables save
# 临时关闭系统防火墙
service iptables stop  
# 开机不启动防火墙
chkconfig iptables off

常规操作

代码语言:shell
复制
# 查看安装目录
which nginx
# 切换到目录并启动
./nginx
# 查看版本信息
nginx -v
# 配置语法检查
nginx -t
# 查看进程状态
ps -ef | grep nginx
# 打开页面
open http://localhost:8080/
# 停止服务
nginx -s stop
# ./nginx -s quit
# 刷新配置
nginx -s reload
# 强制重启服务
nginx -s reopen

Welcome to nginx!

配置端口、路径和文件名

代码语言:text
复制
# /usr/local/nginx/conf/nginx.conf
    server {
        listen       81;
        server_name  localhost;
        location / {
            root /MY_PATH/;
            index  MY_PAGE.html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    } 

uWSGI

安装服务

代码语言:shell
复制
pip install uwsgi
# 测试一下
uwsgi --http 127.0.0.1:8000 --file ./hello_django/wsgi.py 
# --static-map=/static=static
open http://127.0.0.1:8000
killall -9 uwsgi

在manage.py同级目录创建一个deploy文件夹,其中

代码语言:shell
复制
cd "$(dirname "$0")"
# 创建uwsgi工作目录
mkdir deploy
# 创建配置文件uwsgi.ini
touch ./deploy/uwsgi.ini

配置文件内容

代码语言:text
复制
# /deploy/uwsgi.ini
# uwsig使用配置文件启动
[uwsgi]

# 项目所在的根目录
chdir=/Users/workspace/hello_django/

# 指定项目的application,区别于启动命令--wsgi-filemysite/wsgi.py
module=hello_django.wsgi:application

# the local unix socket file than commnuincate to Nginx
# 指定sock的文件路径,这个sock文件会在nginx的uwsgi_pass配置,用来nginx与uwsgi通信   
# 支持ip+port模式以及socket file模式
socket=%(chdir)/deploy/uwsgi.sock
socket=127.0.0.1:8001

# 进程个数       
processes = 8
# 每个进程worker数
workers=5
procname-prefix-spaced=mywebapp # uwsgi的进程名称前缀
py-autoreload=1 # py文件修改,自动加载

# 指定IP端口,web访问入口
http=0.0.0.0:8000

# 指定多个静态文件:static目录和media目录,也可以不用指定该静态文件,在nginx中配置静态文件目录
# uwsgi有自己的配置语法,详细可参考官网,无需写绝对路径,可以用循环、判断等高级配置语法
#for =static media
#static-map=/static=%(chdir)/%(_)
#endfor =
# 启动uwsgi的用户名和用户组
#uid=root
#gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置一个超时,用于中断那些超过服务器请求上限的额外请求
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=%(chdir)/deploy/uwsgi.log
# uWSGI进程号存放
pidfile=%(chdir)/deploy/uwsgi.pid
#monitor uwsgi status  通过该端口可以监控 uwsgi 的负载情况
# 支持ip+port模式以及socket file模式
# stats=%(chdir)/deploy/uwsgi.status 
#stats = 127.0.0.1:9001

通过uwsgi.ini运行uwsgi

代码语言:shell
复制
uwsgi --ini uwsgi.ini
open http://localhost:8000

整体部署

完成uwsgi服务后,更新nginx配置文件 nginx.conf

代码语言:text
复制
server {
       listen  80;
       server_name  localhost;
       charset utf-8;
       location / {
          # 导入一个Nginx模块他是用来和uWSGI进行通讯的
          include uwsgi_params;
          # 设置连接uWSGI超时时间
          uwsgi_connect_timeout 30;
          # 指定uwsgi的sock文件所有动态请求就会直接丢给他
          uwsgi_pass unix:/Users/workspace/hello_django/deploy/uwsgi.sock; 
       }
}

启动nginx

代码语言:shell
复制
nginx
open http://localhost

通过Nginx的特性反向代理、动静分离和负载均衡提升性能

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Nginx
  • uWSGI
  • 整体部署
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档