centos下使用nginx+uwsgi部署django网站(前后端分离单页面)

项目简介

一个简单的可视化展示项目,前端采用html+css布局,使用echarts调用后端django写的接口得到数据并渲染展示。

前端部署

使用nginx,用于返回静态内容,包括网页和图片等
安装
推荐使用源码安装方式,安全可控
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar xf nginx-1.18.0.tar.gz
cd nginx-1.18.0
安装gcc编译,为了支持rewrite功能,我们需要安装pcre,需要ssl的支持安装openssl,gzip 类库安装
yum install gcc zlib zlib-devel pcre-devel openssl-devel
./configure --prefix=/opt/nginx/ --with-http_ssl_module --with-http_stub_status_module

with-http_stub_status_module:支持nginx状态查询
–with-http_ssl_module:支持https

make && make install
测试是否安装成功
cd /opt/nginx
./sbin/nginx

nginx对应的指令介绍
Options:
  -?,-h         : this help     #获得帮助
  -v            : show version and exit   #版本
  -V            : show version and configure options then exit   #版本和配置信息
  -t            : test configuration and exit          #测试配置
  -T            : test configuration, dump it and exit              
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload   #传送新好
  -p prefix     : set prefix path (default: /fox/nginx//)       #设置前置路径
  -c filename   : set configuration file (default: conf/nginx.conf)    #选择配置文件
  -g directives : set global directives out of configuration file     #从配置文件中设置全局指令


浏览器访问127.0.0.1
即可看到欢迎页面
安装完成后各文件夹对应

 conf  html  logs  sbin
conf:配置
html:静态文件
logs:日志
sbin:程序
启动以后会生成一个主进程,根据配置文件的worker_processes来生成子进程(工作进程),主进程不负责处理用户的请求,
只用来转发用户的请求,真正负责处理用户请求的是子进程。

编辑配置文件:
vim conf/nginx.conf

server {
        listen       80;    #端口
        server_name  localhost;
        location / {
            root   /wwwroot/site/templates;   #根目录,默认在安装位置,即/opt/nginx/html,我这里改成自己的位置
            index  index.html index.htm;  #首页
        }
        error_page   500 502 503 504  /50x.html; #错误返回页面
        location = /50x.html {
            root   html;
        }
        #静态文件访问地址,即url中/static的对应位置
        location /static {
            alias /wwwroot/site/templates/static;
        }
}

即只要访问网站ip,如我的是192.168.23.2
那么返回/wwwroot/site/templates/index.html的内容
然后这个index.html内如果有<script src="static/bootstrap-3.3.7/js/bootstrap.min.js"></script>,就会去访问
/wwwroot/site/templates/static/bootstrap-3.3.7/js/bootstrap.min.js,如果有就返回。
好了,网站静态文件处理完成,这只是简单的部署,后续高级部署如负载均衡,ip控制等请自行探索。
哦,对了,如果要开放多个端口返回不同的网站静态文件,只需加server{}就够了
如:

server {
        listen       81;    #开放端口81
        server_name  localhost;
        location / {
            root   /wwwroot/site2/templates;   #根目录,默认在安装位置,即/opt/nginx/html,我这里改成自己的位置
            index  index.html index.htm;  #首页
        }
        error_page   500 502 503 504  /50x.html; #错误返回页面
        location = /50x.html {
            root   html;
        }
        #静态文件访问地址,即url中/static的对应位置
        location /static {
            alias /wwwroot/site2/templates/static;
        }
}

这样就部署了81端口,对应的是site2下的文件

注意:
修改完配置文件应重启nginx,即
./sbin/nginx -s reload

后端部署

使用uwsgi运行django应用,因为需要给其他电脑访问调试,所以采用的是直接本地运行的方式,这里以后可能会有多个django项目同时运行,所以在虚拟环境中操作。
首先在虚拟环境中安装pip install uwsgi
然后写uwsgi启动文件,创建一个uwsgi_conf的文件夹mkdir uwsgi_conf && cd uwsgi_conf
进入后vim uwsgi.ini

#添加配置选择
[uwsgi]
#配置和nginx连接的socket连接,有三种方式,速度对比:文件>socket>port,我这里用的是port,因为要给其他用户连
#socket=127.0.0.1:8002
#指定ip和端口,下面这样设定是所有的ip都能访问
http=0.0.0.0:8003
#配置项目路径,项目的所在目录
chdir=/project/pro_house/
#配置wsgi接口模块文件路径,也就是wsgi.py这个文件所在的目录名.完整目录:/project/pro_house/pro_sys/wsgi.py
wsgi-file=pro_sys/wsgi.py
#配置启动的进程数
processes=4
#配置每个进程的线程数
threads=2
#配置启动管理主进程
master=True
#配置存放主进程的进程号文件
pidfile=uwsgi.pid
#配置dump日志记录
daemonize=uwsgi.log
#配置虚拟环境
virtualenv=/venv/pro-env

保存后启动:
uwsgi --ini uwsgi.ini
停止:
uwsgi --stop uwsgi.pid
关闭uwsgi进程
pkill -f uwsgi -9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值