django 是 python 开源的 web 框架 ,django 自己运行后也是一个轻量级的服务器,能够知足本地的测试,生产环境中的 大量访问, 通常和nginx 和 apache 结合。下面以 Django 与nginx 结合为例进行部署html
一、 部署环境 python
操做系统:Ubuntu 16.04.2nginx
nginx 版本:nginx/1.10.3web
二、部署原理apache
client----》nginx ----》socket ----》uwsgi----》Django django
三、安装步骤 bash
3.1 安装 nginx 服务器
apt-get install nginx app
systemctl start nginx框架
测试 Nginx
3.2 安装 uwsgi
pip install uwsgi
在opt 下面建立 test.py 测试 uwsgi 是否正常安装
#!/usr/bin/env python def application(env,start_response): start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"]
测试 :
uwsgi --http:8000 --wsgi-file test.py
访问 ip:8000,uwsgi 测试成功
3.3 Nginx+uwsgi+django
上传 django 项目至 OurCMDB
建立 OurCMDB_uwsgi.ini 配置文件
#OurCMDB_uwsgi.ini [uwsgi] # Django-related settings socket = :8000 # socket 监听端口,对应下文nginx 配置文件 uwsgi_pass 监听的端口号 # the base directory (full path) chdir = /opt/OurCMDB/ #项目目录 # Django s wsgi file module = OurCMDB.wsgi #wsgi.py 建立django 项目时已经生成 目录 /opt/OurCMDB/OurCMDB/wsgi.py # process-related settings # master master = true # maximum number of worker processes processes = 4 # # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
查看项目下面的文件
新增nginx 虚拟配置文件
cat /etc/nginx/sites-available/OurCMDB_nginx.conf
server { listen 8010; server_name _; charset UTF-8; access_log /var/log/nginx/OurCMDB_access.log; error_log /var/log/nginx/OurCMDB_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; # uwsgi_read_timeout 2; } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /opt/OurCMDB/static/; } }
建立 软链接 ln -s OurCMDB_nginx.conf ../sites-enabled/OurCMDB_nginx.conf
启动
uwsgi --ini OurCMDB_uwsgi.ini
检查Nginx 配置文件
启动nginx
访问测试 django