标签:hosts django tokenize exec modifier 进程 x86 自动重启 mission
Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。
wsgi是PythonWeb服务器网关接口(Web Server Gateway Interface),WSGI是作为Web服务器与Web应用程序或应用框架之间的一种低级别的接口,以提升可移植Web应用开发的共同点。WSGI是基于现存的[[CGI]]标准而设计的。
Django是一个开放源代码的Web应用框架,由Python写成。
Virtualenv指的的Python的虚拟环境
supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就好了。此时被管理进程被视为supervisor的子进程,若该子进程异常中断,则父进程可以准确的获取子进程异常中断的信息,通过在配置文件中设置autostart=ture,可以实现对异常中断的子进程的自动重启。
整个请求和响应流程如下:
说明,系统为centos 7.3
安装python 3.4和对应版本的pip
[root@node1 ~]# yum install python34.x86_64 python34-pip.noarch -y
[root@node1 ~]# pip3 install virtualenv
(1)创建虚拟目录
[root@node1 ~]# virtualenv django
(2)进入虚拟目录
[root@node1 ~]# cd django/
(3)启用虚拟环境
[root@node1 django]# source bin/activate
可以看到目录地下有默认的几个文件夹
(django) [root@node1 django]# ll total 0 drwxr-xr-x 3 root root 330 Nov 5 15:00 bin drwxr-xr-x 2 root root 24 Nov 5 14:55 include drwxr-xr-x 3 root root 23 Nov 5 14:55 lib lrwxrwxrwx 1 root root 16 Nov 5 14:55 lib64 -> /root/django/lib
(django) [root@node1 django]# pip3 install django==1.11 Collecting django==1.11 …………… Installing collected packages: pytz, django Successfully installed django-1.11 pytz-2018.7
(1)创建名为mysite的项目
(django) [root@node1 django]# django-admin startproject mysite
(2)创建名为app01的应用
(django) [root@node1 django]# cd mysite/
(django) [root@node1 mysite]#python3 manage.py startapp app01
(3)可以看到mysite项目下有如下目录
(django) [root@node1 mysite]# ll total 4 drwxr-xr-x 3 root root 123 Nov 5 15:07 app01 -rwxr-xr-x 1 root root 804 Nov 5 15:04 manage.py drwxr-xr-x 3 root root 93 Nov 5 15:07 mysite
(1)修改Django配置文件mysite/settings.py
ALLOWED_HOSTS = [‘*‘] 注册app01 INSTALLED_APPS = [ ‘app01.apps.App01Config‘, .... ] 静态文件目录 STATIC_URL = ‘/static/‘ STATICFILES_DIRS=[ os.path.join(BASE_DIR,"static"), ]
(2)修改路由匹配规则mysite/urls.py
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^index/‘, views.index), ]
(3)新建功能,修改app01/views.py
from django.shortcuts import HttpResponse def index(request): return HttpResponse(‘Hello World!‘)
(django) [root@node1 mysite]# python3 manage.py runserver Performing system checks... ……. Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
测试访问
[root@node1 ~]# curl http://127.0.0.1:8000/index/ Hello World!
在ubuntu上不会有这个错误
Command "/usr/bin/python3.4 -u -c "import setuptools, tokenize; __file__=‘/tmp/pip-install-k0ebn5sl/uwsgi/setup.py‘; f=getattr(tokenize, ‘open‘, open)(__file__); code=f.read().replace(‘\r\n‘, ‘\n‘); f.close(); exec(compile(code, __file__, ‘exec‘))" \ install --record /tmp/pip-record-eekd779j/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-k0ebn5sl/uwsgi/
故采用以下安装方式(仅限centos):
[root@node1 ~]# yum install uwsgi 查看版本 [root@node1 ~]# uwsgi --version 2.0.17.1
注:插件 Ubuntu不需要此步骤
[root@node1 ~]# yum install -y uwsgi-plugin-python34.x86_64
为解决启动报错:
!!! UNABLE to load uWSGI plugin: /usr/lib64/uwsgi/python_plugin.so: cannot open shared object file: No such file or directory !!!
[root@node1 ~]# ln -sv /usr/lib64/uwsgi/python34_plugin.so /usr/lib64/uwsgi/python_plugin.so
(django) [root@node1 mysite]# uwsgi --http-socket :8888 --plugin python --wsgi-file ./test.py
Ubuntu下:uwsgi --http :88888 --wsgi-file test.py
[root@node1 ~]# curl http://127.0.0.1:8888/index/ This is uwsgi test
test.py文件内容如下:
def application(env, start_response): start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)]) return [b"This is uwsgi test"]
(1)invalid request block size: 21573 (max 4096)...skip
buffer-size = 65536
(2)-- unavailable modifier requested: 0 --
-- unavailable modifier requested: 0 –
plugins = python #指定插件
(1)uWsgi热加载python程序(仅做测试使用)
(django) [root@node1 mysite]# uwsgi --http-socket :8000 --plugin python --module mysite.wsgi --py-autoreload=1
按下Ctrl +C 可终止进程
(2)启动Django
(django) [root@node1 mysite]# uwsgi --http-socket :8000 --plugin python --module mysite.wsgi
uwsgi支持ini、xml等多种配置方式,本文以 ini 为例,添加如下配置
[root@node1 ~]# cat /etc/uwsgi_nginx.ini [uwsgi] # Django-related settings # the base directory (full path) #Django项目的目录 chdir = /root/django/mysite # Django‘s wsgi file module = mysite.wsgi # the virtualenv (full path) #虚拟目录的路径 home = /root/django # process-related settings # master master = true # maximum number of worker processes #启动进程数,处理器个数 processes = 1 # threads = 4 #线程个数 # the socket (use the full path to be safe #指定套接字 socket = 0.0.0.0:8000 # ... with appropriate permissions - may be needed #权限设置,要是提示权限不够,可以改为666 # chmod-socket = 664 # clear environment on exit #退出时清除环境 vacuum = true #指定插件,不然会报错:-- unavailable modifier requested: 0 -- plugins = python
[root@node1 ~]# uwsgi --ini /etc/uwsgi_nginx.ini
[root@node1 ~]# yum install nginx –y
[root@node1 ~]# vim /etc/nginx/conf.d/djang.conf server { listen 80; server_name 192.168.15.128; #指定本机ip或者配置你的域名 location / { include /etc/nginx/uwsgi_params; uwsgi_pass 0.0.0.0:8000; } location /static { #配置静态文件目录 alias /root/django/mysite/static; } location /media { alias /root/django/mysite/media; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
[root@node1 ~]# systemctl start nginx
此时以及可以测试访问了
[root@node1 ~]# curl http://127.0.0.1/index/ Hello World!
用浏览器测试如下
[root@node1 ~]# yum install python34-setuptools.noarch [root@node1 ~]# yum install supervisor
通过命令生成supervisor的配支文件
[root@node1 ~]# echo_supervisord_conf > /etc/supervisord.conf
再最后添加如下内容:
[program:mysite] command= /usr/sbin/uwsgi --uwsgi-socket 0.0.0.0:8000 --plugin python--chdir /root/django/mysite --home=/root/django --module mysite.wsgi directory=/root/django/mysite startsecs=0 stopwaitsecs=0 autostart=true autorestart=true
说明:
# [program:mysite] 指定程序名称
# /usr/sbin/uwsgi 指定uwsgi的位置,
#--chdir /root/django/mysite 指定项目路径
# --home=/root/django 指定寻虚拟目录
最后启动supervisor,完成uWSGI启动django,nginx反向代理
[root@node1 ~]# supervisord -c /etc/supervisord.conf
此时配置完成!!至此,所有配置均已完成
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
supervisorctl -c /etxc/supervisord.conf start mysite
supervisorctl -c /etc/supervisord.conf restart mysite
supervisorctl -c /etc/supervisord.conf stop mysite
supervisorctl update
supervisorctl reload
参考:
https://www.cnblogs.com/pyyu/p/9481344.html
https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html
https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
nginx+uWSGI+django+virtualenv+supervisor部署发布web项目
标签:hosts django tokenize exec modifier 进程 x86 自动重启 mission
原文地址:https://www.cnblogs.com/Sunzz/p/9923861.html