配置 nginx + uwgi django 服务 以及 supervisor 的用法

 

virtualenv虚拟环境介绍与简单使用

- 简介:

virtualenv是如何创建“独立”的Python运行环境的呢?
原理很简单,就是把系统Python复制一份到virtualenv的环境,
用命令source venv/bin/activate进入一个virtualenv环境时,
virtualenv会修改相关环境变量,让命令python和pip均指向当前的virtualenv环境。

 

- 安装:

1.安装virtualenv
pip3 install virtualenv
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv   # 指定源
pip3 install --upgrade pip        # 升级 pip

 

- 使用:

# 创建目录
mkdir Myproject
cd Myproject

# 创建独立运行环境-命名
virtualenv --no-site-packages venv#得到独立第三方包的环境

# 进入虚拟环境
source venv/bin/activate#此时进入虚拟环境(venv)Myproject

# 安装第三方包
(venv)Myproject: pip install django==1.9.8
#此时pip的包都会安装到venv环境下,venv是针对Myproject创建的

# 退出venv环境
deactivate命令

 

uwsgi启动django项目

 - uwsgi简介:

wsgi    全称web server gateway interface,wsgi不是服务器,也不是python模块,只是一种协议,描述web server如何和web application通信的规则。
运行在wsgi上的web框架有bottle,flask,django
uwsgi    和wsgi一样是通信协议,是uWSGI服务器的单独协议,用于定义传输信息的类型
uWSGI    是一个web服务器,实现了WSGI协议,uwsgi协议。a

 

- uwsgi安装:

# 进入虚拟环境venv,安装uwsgi
(venv) [root@slave 192.168.11.64 /opt]$pip3 install uwsgi

# 检查uwsgi版本
(venv) [root@slave 192.168.11.64 /opt]$uwsgi --version
2.0.17.1

# 检查uwsgi python版本
uwsgi --python-version

 

- 简单运行一个小脚本:

#启动一个python
uwsgi --http :8000 --wsgi-file test.py

http :8000: 使用http协议,端口8000
wsgi-file test.py: 加载指定的文件,test.py

#test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3

 

- uWsgi 热加载python程序:

在启动命令后面加上参数
uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1 

#发布命令
command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi

#此时修改django代码,uWSGI会自动加载django程序,页面生效

 

- 运行django项目

# mysite/wsgi.py  确保找到这个文件

uwsgi --http :8000 --module mysite.wsgi

# module mysite.wsgi: 加载指定的wsgi模块

 

- uwsgi配置文件:

复制代码
uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/目录下新建uwsgi_nginx.ini,添加如下配置:

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /opt/mysite
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
home            = /opt/venv
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
# the socket (use the full path to be safe
socket          = 0.0.0.0:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true
View Code

 

- Nginx + uWsgi +Django:

  - 配置Nginx的反向代理,修改配置文件:

worker_processes  1;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
   #nginx反向代理uwsgi
    server {
        listen       80;
        server_name  192.168.11.64;
        location / {
         include  /opt/nginx1-12/conf/uwsgi_params;
         uwsgi_pass 0.0.0.0:8000;
            root   html;
            index  index.html index.htm;
        }
      #nginx处理静态页面资源
      location /static{
        alias /opt/nginx1-12/static;   
         }
     #nginx处理媒体资源
     location /media{
        alias /opt/nginx1-12/media;   
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

  - 启动Nginx

nginx 

 

  - 用 uWsgi启动django:

/home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi


# /home/venv/bin/uwsgi :命令路径
# --uwsgi 0.0.0.0:8000: 开启服务器的ip+端口
# --chdir /opt/mysite: 项目路径
# --home=/home/venv: 虚拟环境路径
# --module mysite.wsgi:  在项目路径下的django主文件下的wsgi文件

 

supervisor介绍与简单使用

 supervisor 是基于 python 的任务管理工具,用来自动运行各种后台任务,

当然你也能直接利用 nohup 命令使任务自动后台运行,

但如果要重启任务,每次都自己手动 kill 掉任务进程,

这样很繁琐,而且一旦程序错误导致进程退出的话,系统也无法自动重载任务。

 

- 用python2安装 supervisor, python3不支持

#注意此时已经退出虚拟环境了!!!!!
yum install python-setuptools
easy_install supervisor 

 

- 安装完毕后,通过命令自动生成supervisor 配置文件:

echo_supervisord_conf > /etc/supervisord.conf

 

- 在/etc/supervisord.conf末尾添加上 用uWsgi启动django的命令,和自己项目的名字, 如:

[program:mysite]
command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
directory=/opt/mysite
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

 

- 启动supervisord,关闭supervisord控制的项目,以及重启:

# 启动 supervisord:
supervisord -c /etc/supervisord.conf 


# 项目相关的语法:
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]

#重启mysite项目
supervisorctl -c /etc/supervisord.conf restart mysite  

# 开启mysite项目
supervisorctl -c /etc/supervisord.conf start mysite

# 关闭mysite项目
supervisorctl -c /etc/supervisord.conf stop mysite

 

 

nginx+uWSGI+django+virtualenv+supervisor发布web服务器

# 第一步:在虚拟环境中创建好django项目

# 第二步:在虚拟环境中下载uWsgi

# 第三步:配置好Nginx反向代理,并启动Nginx服务

# 第四步:下载并配置好supervisord

# 第五步: 开启supervisord里面的项目

 

posted @ 2018-10-21 17:44  浮生凉年  阅读(1228)  评论(0编辑  收藏  举报