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

uwsgi+django
1.创建新的虚拟环境,且解决crm的环境依赖

mkvirtualenv crm
workon crm

2.在虚拟环境下安装uwsgi

pip3 install uwsgi

3.学习uwsgi命令,如何启动python应用

启动python web文件
创建一个test.py写入如下代码
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3


用uwsgi启动一个python web文件
#指定8000端口启动 http服务
#指定wsgi文件
uwsgi --http :8000 --wsgi-file test.py

4.用uwsgi启动django项目


使用uwsgi配置文件去启动项目
1.手动创建uwsgi.ini 配置文件
找个地方创建 uwsgi.ini

[uwsgi]
# Django-related settings
# the base directory (full path)
#指定django的项目目录,第一层
chdir           = /opt/Alibab_crm
# Django's wsgi file
#找到django的wsgi文件
#这里需要写项目的第二层目录Alibab_crm
module          = Alibab_crm.wsgi
# the virtualenv (full path)
#填写虚拟环境的绝对路径
home            = /root/Envs/alicrm
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 5
# the socket (use the full path to be safe
#指定socket协议,运行django,只能与nginx结合时使用
#指定socket协议,运行django,只能与nginx结合时使用
#指定socket协议,运行django,只能与nginx结合时使用
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

2.通过配置文件启动uwsgi   (--py-autoreload=1 热加载,后台改动文件 实时更新)

uwsgi --ini uwsgi.ini --py-autoreload=1

5.收集django crm的静态文件

编辑crm的settings.py配置文件
写入如下代码

#定义django的静态资源根目录,便于用命令收集资源,存放的地儿

STATIC_ROOT="/opt/crm_static"
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')

进入虚拟环境  

workon crm

用命令收集静态文件

python3 manage.py collectstatic

6.配置nginx,反响代理django服务器,且解析静态文件

proxy_pass 仅仅是请求转发的参数,与uwsgi结合,还有跟高级的协议参数

修改nginx配置文件如下

server {
        listen       80;
        server_name  niuli.xyz;
        location / {
            uwsgi_pass 公网ip:8000;
            include  /opt/nginx1-12/conf/uwsgi_params;
        }
        #配置一个url的入口,告诉django静态文件在哪里去找
        #当请求url是 niuli.xyz/static/的时候
        #就进行别名,nginx去/opt/crm_static下寻找js文件
        location /static {
        alias  /opt/crm_static/;
}
       #通过这个参数,定义错误页面的文件  ,当状态码是 404 400 401 时,返回40x.html页面
        error_page  404 401 400 403              /40x.html;
    }

7.此时nginx结合uwsgi 已经完成

niuli.xyz

8.记住这里推出虚拟环境,使用物理环境去运行
8.记住这里推出虚拟环境,使用物理环境去运行
8.记住这里推出虚拟环境,使用物理环境去运行
8.记住这里推出虚拟环境,使用物理环境去运行
8.记住这里推出虚拟环境,使用物理环境去运行
8.记住这里推出虚拟环境,使用物理环境去运行
8.记住这里推出虚拟环境,使用物理环境去运行

配置supervisor工具,管理django后台

这个东西只能用python2去实现

1.下载supervisor

easy_install supervisor

2.配置supervisor的配置文件,编写django任务

echo_supervisord_conf > /etc/supervisor.conf

3.编写运行django的任务

vim /etc/supervisor.conf 

在最底行写入如下代码

[program:crm]
# command=虚拟环境中的uwsgi --ini /项目中的uwsgi配置文件 热加载
command=/root/Envs/alicrm/bin/uwsgi --ini /opt/Alibab_crm/uwsgi.ini --py-autoreload=1
autorestart=true
stopasgroup=true
killasgroup=true

4.启动suopersivod这个程序

启动服务端
supervisord -c /etc/supervisor.conf
通过客户端命令查看任务
supervisorctl -c /etc/supervisor.conf 

5.学习supervisor管理命令

[root@s16ds alicrm]# supervisorctl -c /etc/supervisor.conf 
s16alicrm RUNNING pid 5293, uptime 0:03:03
supervisor> stop all #停止所有任务
supervisor> start all #启动s所有任务
supervisor> status s16alicrm
原文地址:https://www.cnblogs.com/niuli1987/p/10409058.html