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


     https://www.cnblogs.com/pyyu/p/9481344.html?tdsourcetag=s_pcqq_aiomsg 

    一 uwsgi安装

    1.安装uwsgi,进入到一个虚拟机环境中去安装
    pip3 install uwsgi

    二 运行uwsgi
    2.通过uwsgi启动一个python web文件
    test.py

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

      


    通过命令启动
    uwsgi --http :9000 --wsgi-file test.py
    启动后可以浏览器 ip:9000 访问django程序

    三 运行一个django项目

    通过uwsgi启动一个python django项目

    django-admin startproject s21django 创建一个项目
    django-admin startapp app01 创建一个app

    1.准备django项目,编写urls、views.py ,修改settings.py 里面的访问权限['*'],还有注册app
    2.通过命令启动 django ,
    要点就是,必须先进入django的第一层目录,然后通过相对路径找到wsgi.py
    具体项目目录如图:

    然后运行命令
    uwsgi --http :9999 --module s21crm.wsgi
    访问方式 http://192.168.1.100:9999/hello/

    uWsgi热加载python程序

    uwsgi --http :9999 --module s21crm.wsgi  --py-autoreload=1   # 就是代码发生改变的时候可以自动加载

    四  uwsgi配置文件运行方式
     1 (cms1) [root@localhost s21crm]# cat uwsgi_cms.ini 
     2 #uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/目录下新建uwsgi_nginx.ini,添加如下配置:
     3 
     4 # mysite_uwsgi.ini file
     5 [uwsgi]
     6 
     7 # Django-related settings
     8 # the base directory (full path)
     9 chdir           = /root/s21crm  ###### django项目存放的目录
    10 # Django's wsgi file
    11 module          = s21crm.wsgi  ##### wsgi存放的地方,相对目录,是相对于chdir目录的
    12 # the virtualenv (full path)
    13 home            = /root/Envs/cms1/ ####虚拟环境目录
    14 # process-related settings
    15 # master
    16 master          = true
    17 # maximum number of worker processes
    18 processes       = 1
    19 # the socket (use the full path to be safe
    20 socket          = 0.0.0.0:8888 ######
    21 # ... with appropriate permissions - may be needed
    22 # chmod-socket    = 664
    23 # clear environment on exit
    24 vacuum          = true

    启动方式:

    uwsgi --ini  /etc/uwsgi_cms.ini 

     ###################################nginx安装

    1.yum install nginx -y #需要提前备好epel源

    2.启动nginx web服务器
    /usr/sbin/nginx 启动nginx web服务

    3.配置nginx.conf配置反向代理uwsgi,以及静态资源处理

    1 location / {
    2 include /etc/nginx/uwsgi_params;
    3 uwsgi_pass 0.0.0.0:8000;
    4 }
    5 #静态资源入口
    6 location /static {
    7 alias /opt/static/;
    8 }

    4.修改完,重启nginx
    /usr/bin/nginx -s reload

    ####################################修改django项目的setting配置文件

    1.修改django的setings.py文件

    1 #指定静态文件存放路径
    2 STATIC_ROOT='/opt/static'
    3 #指定静态资源,请求入口
    4 STATIC_URL = '/static/'
    5 STATICFILES_DIRS=[
    6 os.path.join(BASE_DIR,"static"),
    7 ]

    2.通过命令收集静态文件资源
    python3 manage.py collectstatic 收集所有你使用的静态文件保存到STATIC_ROOT!

    这个可能会提示你么有static目录,直接创建一个就可以,然后再次运行即可。

    3.此时static_root下有了所有项目的静态资源
    最后访问的项目的时候就不要用8000端口了,因为已经被nginx代理了,访问80就可以了

    #################################################supervisor  安装和配置

    1.通过easy_install安装
    #注意此时已经退出虚拟环境了!!!!!
    yum install python-setuptools
    easy_install supervisor


    2.通过命令生成supervisor的配置文件
    echo_supervisord_conf > /etc/supervisord.conf

    3.在配置文件中,添加任务,管理django项目
    vim /etc/supervisord.conf #写入信息

    1 [program:cms]
    2 command=/root/Envs/cms1/bin/uwsgi --ini /root/s21crm/uwsgi_cms.ini
    3 #检测项目是否挂掉,自动重启
    4 autorestart=true
    5 #在supervisord启动的时候也自动启动
    6 autostart=true
    7 stdout_logfile=/root/cms.log
    8 stderr_logfile=/root/cms_err.log

    4.启动supervisord
    supervisord -c supervisord.conf

    5  查看服务是否启动

    (cms1) [root@localhost s21crm]# supervisorctl
    cms RUNNING pid 20415, uptime 0:36:05
    
    
    第二种:
    root     20362  0.0  1.0 215500 10632 ?        Ss   05:08   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
    
    netstat 查看uwsgi是否启动
    tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      20415/uwsgi

    6 supervisord管理命令

    5.管理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都不会自动重启。

    注意:你的服务器要先操作如下:才能正常访问django环境

    关闭防火墙
    systemctl stop firewalld
    iptables -F
    setenforce 0 #系统自带防火墙也给关了

  • 相关阅读:
    jdbc配置Spring
    zend studio报错
    phpStudy 5.5n +zendstudio12.5+xDebugger的配置
    一个关于finally和return的面试题
    进制详解
    设计模式--桥接(Bridge)模式
    Struts2中文件上传下载实例
    java int and Integer
    java面试题
    Python __slots__的使用
  • 原文地址:https://www.cnblogs.com/huningfei/p/9985995.html
Copyright © 2020-2023  润新知