nginx,uwsgi发布web服务器

 

复制代码
1.单机启动django项目,性能低,默认使用wsgiref模块,性能低的wsgi协议

python3 manager.py runserver 0.0.0.0:8000   > wsgiref模块中

2.高并发启动django,django是没有这个功能的,而uWSGI模块,遵循uwsgi协议,支持多进程处理django请求

uwsgi  通过他,启动你的django,而不再是python3 manager.py runserver 0.0.0.0:8000


3.公司中一般用 nginx + uwsgi + django + virtualenv  + supervisord(进程管理工具)


搭建笔记:
    1.确定依赖组件是否安装
    yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
    
    

    
    
nginx 正向代理,反向代理的概念




用户阿段,去访问mycrm.com:80 ,他想直接从80端口,找到hello视图,也就是mycrm.com:80/hello 
实现手段就是,阿段去访问 mycrm.com:80 这个nginx服务,并且让nginx,把hello这个请求,丢给后端的 uwsgi+django程序处理

1.基础环境准备好
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

2.准备好python3环境

3.准备好virtualenv 

4.安装uWSGI
    1.激活虚拟环境
    source /opt/all_venv/venv2/bin/activate
    
    2.安装uWSGI
    (venv2) [root@s13linux ~ 05:18:21]$pip3 install uwsgi
    
    3.检查uwsgi版本
        (venv) [root@slave 192.168.11.64 /opt]$uwsgi --version
        2.0.17.1
        #检查uwsgi python版本
        uwsgi --python-version
    
    4.运行一个简单的uwsgi服务器
        1.创建一个test.py文件,写入内容
        def application(env, start_response):
            start_response('200 OK', [('Content-Type','text/html')])
            return [b"Hello World"] # python3
            
        2.然后用uwsgi命令启动
        uwsgi --http :8000 --wsgi-file test.py
            参数解释
            http :8000: 使用http协议,端口8000
            wsgi-file test.py: 加载指定的文件,test.py
        
    5.用uwsgi运行你的django项目(测试使用)
        1.准备好mysite,自己写好MTV视图函数  /hello 
    
    先确保你在项目文件夹下,例如/opt/mysite/底下
    
    uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1 
        参数解析
            --http 启动在8088端口,--module 指定项目文件夹路径  --py-autoreload是热加载程序
    
    6.配置nginx反向代理uwsgi+django!!!!(此步重要!!!)
        1.首先kill杀掉nginx进程
        2.配置nginx.conf,通过此步才能生效!!
            填入重要两个参数,根据自己目录结构配置,uwsgi_pass通过这个参数,nginx才能转发请求给后端0.0.0.0:9000的应用
            include  /opt/nginx112/conf/uwsgi_params;
            uwsgi_pass 0.0.0.0:9000;
        --------------------------分割线--------------------------------------------------------
             server {
                    listen       80;
                    server_name  mycrm.com;
                    location / {
                        include  /opt/nginx112/conf/uwsgi_params;
                        uwsgi_pass 0.0.0.0:9000;
                        root   html;
                        index  index.html index.htm;
                        #deny 10.0.0.1;
        }
        
        配置nginx.conf之后,启动nginx服务,等待配置启动uwsgi+django 
uwsgi --uwsgi 0.0.0.0:9000 --chdir=/opt/mysit --home=opt/all_venv/venv2/ --module=mysite.wsgi
7.配置supervisor进程管理工具 1.通过python2的包管理工具easy_install安装 yum install python-setuptools easy_install supervisor 2.通过命令生成supervisor的配支文件 echo_supervisord_conf > /etc/supervisord.conf 3.写入/etc/supervisord.conf配置信息(参数根据自己环境填写) [program:my_crm] command=/opt/all_venv/venv2/bin/uwsgi(uwsgi的绝对路径) --uwsgi 0.0.0.0:9000(端口号应与nginx配置中的端口号一致) --chdir=/opt/s13crm(项目目录) --home=/opt/all_venv/venv2/(虚拟环境目录) --module=s13crm.wsgi(项目中和项目同名文件夹下的wsgi.py文件,使用.连接文件夹名和文件,注意不是 /) directory=/opt/s13crm (项目目录) startsecs=0 stopwaitsecs=0 autostart=true autorestart=true 8.启动supervi服务,(同时启动uwsgi+django服务) 最后启动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] 9.此时访问网站mycrm.com ,查看是否可以通过80端口,访问到django应用,完成项目发布。 由于nginx的高并发性能,配合uwsgi的多进程性能,可以达到一个线上的django应用发布!!! 内容博客地址: https://www.cnblogs.com/pyyu/p/9481344.html
复制代码

 

 

 

 

大家都学过了django,用django写了各种功能,写了bbs项目,写了路飞学城。

咱们都知道django是一个web框架,方便我们快速开发web程序,http请求的动态数据就是由web框架来提供处理的。

前面超哥也对nginx简单的介绍了,本文将nginx、WSGI、uwsgi、uWSGI、django这几个关系梳理一下。

wsgi    全称web server gateway interface,wsgi不是服务器,也不是python模块,只是一种协议,描述web server如何和web application通信的规则。
运行在wsgi上的web框架有bottle,flask,django
uwsgi    和wsgi一样是通信协议,是uWSGI服务器的单独协议,用于定义传输信息的类型
uWSGI    是一个web服务器,实现了WSGI协议,uwsgi协议。a
nginx    web服务器,更加安全,更好的处理处理静态资源,缓存功能,负载均衡,因此nginx的强劲性能,配合uWSGI服务器会更加安全,性能有保障。
django 高级的python web框架,用于快速开发,解决web开发的大部分麻烦,程序员可以更专注业务逻辑,无须重新造轮子

逻辑图

web服务器

传统的c/s架构,请求的过程是
客户端 > 服务器 
服务器 > 客户端
服务器就是:1.接收请求 2.处理请求 3.返回响应

web框架层

HTTP的动态数据交给web框架,例如django遵循MTV模式处理请求。
HTTp协议使用url定位资源,urls.py将路由请求交给views视图处理,然后返回一个结果,完成一次请求。
web框架使用者只需要处理业务的逻辑即可。

如果将一次通信转化为“对话”的过程

Nginx:hello wsgi,我刚收到一个请求,你准备下然后让django来处理吧

WSGI:好的nginx,我马上设置环境变量,然后把请求交给django

Django:谢谢WSGI,我处理完请求马上给你响应结果

WSGI:好的,我在等着

Django:搞定啦,麻烦wsgi吧响应结果传递给nginx

WSGI:太棒了,nginx,响应结果请收好,已经按照要求传递给你了

nginx:好滴。我把响应交给用户。合作愉快

Django Nginx+uwsgi 安装配置

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

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

基础开发环境配置

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

提前安装好python3环境

https://www.cnblogs.com/pyyu/p/7402145.html

virtualenv

请确保你的虚拟环境正常工作
https://www.cnblogs.com/pyyu/p/9015317.html

安装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 ')
复制代码

安装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
复制代码

运行简单的uWSGI

复制代码
#启动一个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
复制代码

指定配置文件启动命令

uwsgi --ini  /etc/uwsgi_nginx.ini

配置nginx结合uWSGI

配置nginx.conf

复制代码
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

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末尾添加上如下代码!!!!!!

复制代码
[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
directory
=/opt/mysite startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
复制代码

最后启动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都不会自动重启。
复制代码

 django的静态文件与nginx配置

mysite/settings.py

STATIC_ROOT='/opt/nginx1-12/static'
STATIC_URL = '/static/'
STATICFILES_DIRS=[
    os.path.join(BASE_DIR,"static"),
]

上述的参数STATIC_ROOT用在哪?

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

STATIC_ROOT 文件夹 是用来将所有STATICFILES_DIRS中所有文件夹中的文件,以及各app中static中的文件都复制过来
# 把这些文件放到一起是为了用nginx等部署的时候更方便

 

复制代码
1.单机启动django项目,性能低,默认使用wsgiref模块,性能低的wsgi协议

python3 manager.py runserver 0.0.0.0:8000   > wsgiref模块中

2.高并发启动django,django是没有这个功能的,而uWSGI模块,遵循uwsgi协议,支持多进程处理django请求

uwsgi  通过他,启动你的django,而不再是python3 manager.py runserver 0.0.0.0:8000


3.公司中一般用 nginx + uwsgi + django + virtualenv  + supervisord(进程管理工具)


搭建笔记:
    1.确定依赖组件是否安装
    yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
    
    

    
    
nginx 正向代理,反向代理的概念




用户阿段,去访问mycrm.com:80 ,他想直接从80端口,找到hello视图,也就是mycrm.com:80/hello 
实现手段就是,阿段去访问 mycrm.com:80 这个nginx服务,并且让nginx,把hello这个请求,丢给后端的 uwsgi+django程序处理

1.基础环境准备好
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

2.准备好python3环境

3.准备好virtualenv 

4.安装uWSGI
    1.激活虚拟环境
    source /opt/all_venv/venv2/bin/activate
    
    2.安装uWSGI
    (venv2) [root@s13linux ~ 05:18:21]$pip3 install uwsgi
    
    3.检查uwsgi版本
        (venv) [root@slave 192.168.11.64 /opt]$uwsgi --version
        2.0.17.1
        #检查uwsgi python版本
        uwsgi --python-version
    
    4.运行一个简单的uwsgi服务器
        1.创建一个test.py文件,写入内容
        def application(env, start_response):
            start_response('200 OK', [('Content-Type','text/html')])
            return [b"Hello World"] # python3
            
        2.然后用uwsgi命令启动
        uwsgi --http :8000 --wsgi-file test.py
            参数解释
            http :8000: 使用http协议,端口8000
            wsgi-file test.py: 加载指定的文件,test.py
        
    5.用uwsgi运行你的django项目(测试使用)
        1.准备好mysite,自己写好MTV视图函数  /hello 
    
    先确保你在项目文件夹下,例如/opt/mysite/底下
    
    uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1 
        参数解析
            --http 启动在8088端口,--module 指定项目文件夹路径  --py-autoreload是热加载程序
    
    6.配置nginx反向代理uwsgi+django!!!!(此步重要!!!)
        1.首先kill杀掉nginx进程
        2.配置nginx.conf,通过此步才能生效!!
            填入重要两个参数,根据自己目录结构配置,uwsgi_pass通过这个参数,nginx才能转发请求给后端0.0.0.0:9000的应用
            include  /opt/nginx112/conf/uwsgi_params;
            uwsgi_pass 0.0.0.0:9000;
        --------------------------分割线--------------------------------------------------------
             server {
                    listen       80;
                    server_name  mycrm.com;
                    location / {
                        include  /opt/nginx112/conf/uwsgi_params;
                        uwsgi_pass 0.0.0.0:9000;
                        root   html;
                        index  index.html index.htm;
                        #deny 10.0.0.1;
        }
        
        配置nginx.conf之后,启动nginx服务,等待配置启动uwsgi+django 
uwsgi --uwsgi 0.0.0.0:9000 --chdir=/opt/mysit --home=opt/all_venv/venv2/ --module=mysite.wsgi
7.配置supervisor进程管理工具 1.通过python2的包管理工具easy_install安装 yum install python-setuptools easy_install supervisor 2.通过命令生成supervisor的配支文件 echo_supervisord_conf > /etc/supervisord.conf 3.写入/etc/supervisord.conf配置信息(参数根据自己环境填写) [program:my_crm] command=/opt/all_venv/venv2/bin/uwsgi(uwsgi的绝对路径) --uwsgi 0.0.0.0:9000(端口号应与nginx配置中的端口号一致) --chdir=/opt/s13crm(项目目录) --home=/opt/all_venv/venv2/(虚拟环境目录) --module=s13crm.wsgi(项目中和项目同名文件夹下的wsgi.py文件,使用.连接文件夹名和文件,注意不是 /) directory=/opt/s13crm (项目目录) startsecs=0 stopwaitsecs=0 autostart=true autorestart=true 8.启动supervi服务,(同时启动uwsgi+django服务) 最后启动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] 9.此时访问网站mycrm.com ,查看是否可以通过80端口,访问到django应用,完成项目发布。 由于nginx的高并发性能,配合uwsgi的多进程性能,可以达到一个线上的django应用发布!!! 内容博客地址: https://www.cnblogs.com/pyyu/p/9481344.html
复制代码

 

 

 

 

大家都学过了django,用django写了各种功能,写了bbs项目,写了路飞学城。

咱们都知道django是一个web框架,方便我们快速开发web程序,http请求的动态数据就是由web框架来提供处理的。

前面超哥也对nginx简单的介绍了,本文将nginx、WSGI、uwsgi、uWSGI、django这几个关系梳理一下。

wsgi    全称web server gateway interface,wsgi不是服务器,也不是python模块,只是一种协议,描述web server如何和web application通信的规则。
运行在wsgi上的web框架有bottle,flask,django
uwsgi    和wsgi一样是通信协议,是uWSGI服务器的单独协议,用于定义传输信息的类型
uWSGI    是一个web服务器,实现了WSGI协议,uwsgi协议。a
nginx    web服务器,更加安全,更好的处理处理静态资源,缓存功能,负载均衡,因此nginx的强劲性能,配合uWSGI服务器会更加安全,性能有保障。
django 高级的python web框架,用于快速开发,解决web开发的大部分麻烦,程序员可以更专注业务逻辑,无须重新造轮子

逻辑图

web服务器

传统的c/s架构,请求的过程是
客户端 > 服务器 
服务器 > 客户端
服务器就是:1.接收请求 2.处理请求 3.返回响应

web框架层

HTTP的动态数据交给web框架,例如django遵循MTV模式处理请求。
HTTp协议使用url定位资源,urls.py将路由请求交给views视图处理,然后返回一个结果,完成一次请求。
web框架使用者只需要处理业务的逻辑即可。

如果将一次通信转化为“对话”的过程

Nginx:hello wsgi,我刚收到一个请求,你准备下然后让django来处理吧

WSGI:好的nginx,我马上设置环境变量,然后把请求交给django

Django:谢谢WSGI,我处理完请求马上给你响应结果

WSGI:好的,我在等着

Django:搞定啦,麻烦wsgi吧响应结果传递给nginx

WSGI:太棒了,nginx,响应结果请收好,已经按照要求传递给你了

nginx:好滴。我把响应交给用户。合作愉快

Django Nginx+uwsgi 安装配置

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

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

基础开发环境配置

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

提前安装好python3环境

https://www.cnblogs.com/pyyu/p/7402145.html

virtualenv

请确保你的虚拟环境正常工作
https://www.cnblogs.com/pyyu/p/9015317.html

安装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 ')
复制代码

安装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
复制代码

运行简单的uWSGI

复制代码
#启动一个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
复制代码

指定配置文件启动命令

uwsgi --ini  /etc/uwsgi_nginx.ini

配置nginx结合uWSGI

配置nginx.conf

复制代码
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

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末尾添加上如下代码!!!!!!

复制代码
[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
directory
=/opt/mysite startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
复制代码

最后启动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都不会自动重启。
复制代码

 django的静态文件与nginx配置

mysite/settings.py

STATIC_ROOT='/opt/nginx1-12/static'
STATIC_URL = '/static/'
STATICFILES_DIRS=[
    os.path.join(BASE_DIR,"static"),
]

上述的参数STATIC_ROOT用在哪?

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

STATIC_ROOT 文件夹 是用来将所有STATICFILES_DIRS中所有文件夹中的文件,以及各app中static中的文件都复制过来
# 把这些文件放到一起是为了用nginx等部署的时候更方便

猜你喜欢

转载自www.cnblogs.com/perfey/p/9825751.html