Django+Nginx+uwsgi 项目部署

一:Django+Nginx+uwsgi 项目部署

# 1
在前面的章节中我们使用 python manage.py runserver 来运行服务器。这只适用测试环境中使用。

正式发布的服务,需要一个可以稳定而持续的服务器。

python django默认启动
python3 manage.py runserver 0.0.0.0:8000这种方式调用wsgiref单机模块,性能较低,生产环境不用


# 2
线上使用uwsgi工具(由c语言编写的工具,性能强悍)启动django,
使用方式:

在激活虚拟环境的前提下,使用uwsgi
安装配置好virtualenvwrapper工具,或者virtualenv皆可


1.准备django项目 crm  
# ALLOWED_HOSTS = ['*']

# 环境准备
# 基础开发环境配置
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

# 提前安装好python3环境
# virtualenv   : 请确保你的虚拟环境正常工作

# 安装django1.11
pip3 install django==1.11
#创建django项目mysite
django-admin startproject mysite
#创建app01
python3 manage.py startapp app01
mysite/settings.py

#settings.py设置
ALLOWED_HOSTS = ['*']
install app01
mysite/urls.py

from app01 import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^hello_django/', views.hello),
]
app01/views.py


from django.shortcuts import render,HttpResponse

# Create your views here.
def hello(request):
    print('request is :',request)
    return HttpResponse('django is ok ')


1.安装uwsgi工具

# 进入虚拟环境venv,安装uwsgi

pip3 install -i https://pypi.douban.com/simple 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


2.使用uwsgi.ini配置文件方式,启动crm项目

-手动创建uwsgi.ini,放在crm项目下,便于管理
                touch uwsgi.ini 
配置文件uwsgi.ini的内容如下,这就是一个普通文本,里面定义了功能参数

# (django1) [root@bogon crm]# ls
# app01  crm  db.sqlite3  manage.py  pltj.py  __pycache__  statics  templates
# (django1) [root@bogon crm]# touch uwsgi.ini
# (django1) [root@bogon crm]# vim uwsgi.ini

[uwsgi]
# Django-related settings
# the base directory (full path)
#项目的绝对路径,定位到nbcrm的第一层
chdir           = /opt/crm

# Django's wsgi file
# 找到项目第二层的wsgi文件
module          = crm.wsgi

# the virtualenv (full path)
# 找到虚拟环境的绝对路径
home            = /opt/allenv/venv1   

# (venv1) [root@bogon venv1 ]# pwd
# /opt/allenv/venv1  


# process-related settings
# master
# 主进程
master          = true


# maximum number of worker processes
# 开启uwsgi的多进程数,根据cpu核数来定义
processes       = 1


# the socket (use the full path to be safe
# 基于socket链接运行crm,只有与nginx结合的时候,才使用socket形式
socket          = 0.0.0.0:8000

# 当你没用nginx,调试项目的时候,使用http形式 
# http     =  0.0.0.0:8000


# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true



#指定一个参数,日志放在哪

#如果你使用了supervisor,请注释掉这个参数
#守护进程在后台运行,且将日志信息,输出到uwsgi.log日志中
#daemonize = uwsgi.log
[uwsgi]
        chdir           = /opt/NBcrm
    
        # Django's wsgi file
        # 找到项目第二层的wsgi文件
        module          = NBcrm.wsgi
    
        # the virtualenv (full path)
        # 找到虚拟环境的绝对路径
        home            = /root/Envs/nbcrm

3.启动配置文件的命令

通过uwsgi.ini配置文件,启动NBcrm

uwsgi --ini  uwsgi.ini

# 启动配置文件的命令
# /root/Envs/nbcrm/bin/uwsgi   --ini uwsgi.ini   

# /opt/allenv/crm/bin/uwsgi   --ini uwsgi.ini 

# uwsgi   --ini uwsgi.ini 

4:配置nginx,结合uwsgi,以及处理静态文件的配置

nginx.conf请求转发配置如下

    server {
        listen       80;
        server_name  localhost;
                        location / {
                        include uwsgi_params;
                        uwsgi_pass 0.0.0.0:8000;

        }
    }
        
        
nginx处理crm的静态文件方式
        1.修改django的settings.py静态文件
        添加如下参数
                # Static files (CSS, JavaScript, Images)
                # https://docs.djangoproject.com/en/1.11/howto/static-files/
                STATIC_ROOT='/opt/s20static'
                STATIC_URL = '/static/'
                STATICFILES_DIRS = [
                        os.path.join(BASE_DIR,'statics'),
                ]
                
        2.执行命令,收集crm的静态文件
         python3  /opt/NBcrm/manage.py collectstatic
         
         
         python3  /opt/crm/manage.py collectstatic
         
         

         3.配置nginx的location路径匹配,找到crm这些静态文件
         在nginx.conf中找到server{}标签,添加如下参数
         #当我的请求url是 192.168.16.142:80/static/xxxxxxxx 
                location /static {
                        alias  /opt/s20static/;
                }
                
         4. 最终的nginx.conf文件如下:
        
                修改nginx.conf配置如下
                        server {
                                listen       80;
                                server_name  localhost;
                                        #最低级路径匹配,当请求url是 www.s20crm.com(192.168.16.142/)
                                        location / {
                                           #include翻译就是包含的意思,将外部一个文件的内容,包含进来 
                                                include uwsgi_params;
                                                #
                                                uwsgi_pass 127.0.0.1:8000;
                                        }
                                        #当请求是 192.168.16.142/static/xxxxx
                                        #解析NBcrm静态文件内容,处理css js
                                        location /static {
                                                alias  /opt/s20static/;
                                        }
                                }
                        
        

         5.启动nginx,访问nginx的80,是否可以转发到crm

二:启动项目的其它几种方式

1.准备django项目 NB_crm  


2.安装虚拟环境,在虚拟环境下,安装uwsgi,进行部署
workon nbcrm
pip3 install -i https://pypi.douban.com/simple uwsgi

3.利用uwsgi运行一个python web脚本文件
        新建一个py脚本文件,写入如下内容
        def application(env, start_response):
                start_response('200 OK', [('Content-Type','text/html')])
                return [b"Hello World"] # python3
        启动命令如下
        uwsgi --http :8000 --wsgi-file test.py
                --http参数意思是,基于http协议运行 在 8000端口
                --socket  
                --wsgi-file  找到wsgi.py文件 
                
4.利用uwsgi运行django项目
(以参数形式运行项目),(还有以配置文件形式运行,把运行的参数写入到一个文件里面,基于这个文件运行)
(以参数形式运行项目)
(以参数形式运行项目)
命令如下 
uwsgi --http :8088 --module mysite.wsgi
        --module  找到django项目的第二层里面的wsgi.py文件
        
uwsgi默认不支持静态文件解析,使用nginx去解析静态文件 

uwsgi --http :8088 --module crm.wsgi



5.热加载django项目,uwsig自动重启django

uwsgi --http :9000 --module NBcrm.wsgi   --py-autoreload=1 




6.基于配置文件的形式,运行nbcrm

三:supervisor

supervisor 是基于 python 的任务管理工具,用来自动运行各种后台任务,当然你也能直接利用 nohup 命令使任务自动后台运行,但如果要重启任务,每次都自己手动 kill 掉任务进程,这样很繁琐,而且一旦程序错误导致进程退出的话,系统也无法自动重载任务。

这里超哥要配置基于virtualenv的supervisor

由于supervisor在python3下无法使用,因此只能用python2去下载!!!!!!

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

通过命令生成supervisor的配支文件

echo_supervisord_conf > /etc/supervisord.conf

然后再/etc/supervisord.conf末尾添加上如下代码!!!!!!

supervisord.conf配置文件参数解释
[program:xx]是被管理的进程配置参数,xx是进程的名称
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run  ; 程序启动命令
autostart=true       ; 在supervisord启动的时候也自动启动
startsecs=10         ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true     ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3       ; 启动失败自动重试次数,默认是3
user=tomcat          ; 用哪个用户启动进程,默认是root
priority=999         ; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; 把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB  ; stdout 日志文件大小,默认50MB
stdout_logfile_backups = 20   ; stdout 日志文件备份数,默认是10
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false     ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false     ;默认为false,向进程组发送kill信号,包括子进程
[program:my]
#command=/opt/venv/bin/uwsgi --ini  /etc/uwsgi_nginx.ini  #这里是结合virtualenv的命令 和supervisor的精髓!!!!
command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
#--home指的是虚拟环境目录  --module找到 mysite/wsgi.py   

最后启动supervisor,完成uWSGI启动django,nginx反向代理

supervisord -c /etc/supervisord.conf #启动supervisor
supervisorctl -c /etxc/supervisord.conf restart my  #重启my项目
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]

重新加载supervisor

一、添加好配置文件后

二、更新新的配置到supervisord    

supervisorctl update
三、重新启动配置中的所有程序

supervisorctl reload
四、启动某个进程(program_name=你配置中写的程序名称)

supervisorctl start program_name
五、查看正在守候的进程

supervisorctl
六、停止某一进程 (program_name=你配置中写的程序名称)

pervisorctl stop program_name
七、重启某一进程 (program_name=你配置中写的程序名称)

supervisorctl restart program_name
八、停止全部进程

supervisorctl stop all
注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。
5.通过supervisorctl管理命令,管理uwsgi
supervisorctl -c /etc/supervisor.conf 

命令如下
status all 
start s20nbcrm
stop s20nbcrm
stop all



linux出现swp交换文件,是因为vim异常退出,或者有人同时在操作一个文件,linux系统保护机制,会生成swp文件,删除即可


配置文件形式
nginx.conf
my.cnf
my.ini
uwsgi.ini
*.xml
*.json