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