Django 正式环境部署
经过好几章节的学习,我们初步已经完善了 Django HelloWorld 项目。我们一直在使用开发环境访问我们的 HelloWorld 项目,是时候把它部署到正式环境,对外开放访问了。
使用 python manage.py runserver 来运行服务,这只适用测试环境中使用
正式发布的服务,我们需要一个可以稳定而持续的服务器,比如 Apache, Nginx, lighttpd等。
本章节以 CentOS + Nginx + uWsgi 为例,学习如何部署 Django 项目
安装基础开发包
CentOS 下安装步骤如下:
[root@localhost ~]# yum groupinstall "Development tools" [root@localhost ~]# yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
CentOS 自带 Python 2.7.5 和 Python 3.4.5,但我们可以再安装 Python 3.6.3:
[root@localhost ~]# cd ~ [root@localhost ~]# wget wget http://mirrors.sohu.com/python/3.6.3/Python-3.6.3.tar.xz [root@localhost ~]# xz -d Python-3.6.3.tar.xz [root@localhost ~]# tar xvf Python-3.6.3.tar [root@localhost ~]# cd Python-3.6.3 [root@localhost ~]# ./configure --prefix=/usr/local [root@localhost ~]# make && make install [root@localhost ~]# python3 --version Python 3.6.3
如果你是远程 SSH 登录服务器,那么需要退出 SSH 重新登录,这样输入 python3 --version 就会正确了
安装Python 包管理工具 pip
安装 pip 的好处是可以用 pip list、pip uninstall 管理 Python 包 easy_install 没有这个功能,只有 uninstall
pip 包: https://pypi.python.org/pypi/pip
Python 3.6.3 版本自身携带了 pip
包管理工具,所以我们在安装 Python 3.6.3 时也自动安装了 pip
如果你在输入 pip3
是提示命令不存在,则可以使用以下方式安装
安装步骤:
[root@localhost ~]# wget https://bootstrap.pypa.io/get-pip.py [root@localhost ~]# chmod u+x get-pip.py [root@localhost ~]# python3 get-pip.py [root@localhost ~]# pip3 --version pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)
创建项目目录
我们接下来把所有的 Django 代码都放到 /mnt/data/www
目录中,所以先要创建目录结构
[root@localhost ~]# mkdir -p /mnt/data/www/ [root@localhost ~]# cd /mnt/data/www [root@localhost www]#
安装 uwsgi
uwsgi 介绍: https://pypi.python.org/pypi/uWSGI
uwsgi 参数详解: http://uwsgi-docs.readthedocs.org/en/latest/Options.html
[root@localhost www]# pip3 install uwsgi [root@localhost www]# uwsgi --version # 查看 uwsgi 版本 2.0.15
测试 uwsgi 是否安装正确
新建 test.py 文件,内容如下:
#! /usr/bin/python # -*- encoding: utf-8 -*- ''' filename: view.py author: 简单教程(www.twle.cn) copyright: Copyright © 2015-2065 www.twle.cn. All rights reserved. ''' def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
然后在终端运行:
[root@localhost www]# uwsgi --http :8001 --wsgi-file test.py --uid nobody
整个过程 shell 操作如下
[root@localhost www]# touch test.py [root@localhost www]# vi test.py [root@localhost www]# uwsgi --http :8001 --wsgi-file test.py --uid nobody *** Starting uWSGI 2.0.15 (64bit) on [Mon Oct 9 23:15:29 2017] *** compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-16) on 09 October 2017 20:39:41 os: Linux-3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 nodename: localhost.localdomain machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 1 current working directory: /mnt/data/www detected binary path: /usr/local/bin/uwsgi setuid() to 99 *** WARNING: you are running uWSGI without its master process manager *** your processes number limit is 3819 your memory page size is 4096 bytes detected max file descriptor number: 1024 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uWSGI http bound on :8001 fd 4 spawned uWSGI http 1 (pid: 1585) uwsgi socket 0 bound to TCP address 127.0.0.1:43367 (port auto-assigned) fd 3 Python version: 3.6.3 (default, Oct 9 2017, 20:27:44) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x27c9b70 your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 72768 bytes (71 KB) for 1 cores *** Operational MODE: single process *** WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x27c9b70 pid: 1584 (default app) *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 1584, cores: 1)
在浏览器内输入:http://127.0.0.1:8001,查看是否有 Hello World
输出
若没有输出,则需要检查安装过程然后重新安装
如果你是远程 SSH 操作,可以打开另一个终端,输入以下命令来检查
[root@localhost www]# curl -i http://127.0.0.1:8001/ HTTP/1.1 200 OK Content-Type: text/html Hello World
安装 Django
[root@localhost www]# pip3 install django
测试 django 是否正常运行:
[root@localhost www]# django-admin.py startproject demosite [root@localhost www]# cd demosite/ [root@localhost demosite] python3 -m venv . [root@localhost demosite]# source bin/activate (demosite)[root@localhost demosite]# python3 manage.py runserver 0.0.0.0:8002 Performing system checks... System check identified no issues (0 silenced). You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. October 09, 2017 - 15:24:25 Django version 1.11.6, using settings 'demosite.settings' Starting development server at http://0.0.0.0:8002/ Quit the server with CONTROL-C.
在浏览器内输入:http://127.0.0.1:8002,检查 django 是否运行正常
如果你是远程 SSH 操作,可以打开另一个终端,输入以下命令来检查
[root@localhost www]# curl -i http://127.0.0.1:8002/ ... <h1>It worked!</h1> ...
安装 Nginx
安装命令如下:
[root@localhost www]# cd ~ [root@localhost ~]# wget http://nginx.org/download/nginx-1.13.5.tar.gz [root@localhost ~]# tar xf nginx-1.13.5.tar.gz [root@localhost ~]# cd nginx-1.13.5 [root@localhost ~]# ./configure --prefix=/usr/local/nginx-1.13.5 --with-http_stub_status_module --with-http_gzip_static_module [root@localhost ~]# make && make install [root@localhost ~]# ln -s /usr/local/nginx-1.13.5/sbin/nginx /usr/local/bin/nginx [root@localhost ~]# nginx -v nginx version: nginx/1.13.5
可以通过 Nginx 安装配置 了解更多内容
uwsgi 配置
uWSGI 支持 ini、xml 等多种配置方式,我们使用 ini
格式来配置
在 /ect/
目录下新建 uwsgi_8080.ini
文件并添加如下配置:
[uwsgi] socket = 127.0.0.1:8080 #主进程 master = true #多站模式 vhost = true #多站模式时不设置入口模块和文件 no-site = true #子进程数 workers = 2 reload-mercy = 10 #退出、重启时清理文件 vacuum = true max-requests = 1000 limit-as = 512 buffer-size = 30000 #pid 文件,用于下面的脚本启动、停止该进程 pidfile = /var/run/uwsgi_8080.pid daemonize = /var/log/uwsgi_8080.log virtualenv=/mnt/data/www/demosite/bin chdir=/mnt/data/www/demosite module=demosite.wsgi:application static-map=/static=/mnt/data/www/demosite/static uid=nobody
Nginx 配置
找到 Nginx 的安装目录(如:/usr/local/nginx-1.13.5
),打开 conf/nginx.conf
文件,修改 server
配置:
server { listen 80; server_name localhost; location / { include uwsgi_params; #必须和uwsgi中的设置一致 uwsgi_pass 127.0.0.1:8080; #入口文件,即wsgi.py相对于项目根目录的位置,'.'相当于当前一层目录 uwsgi_param UWSGI_SCRIPT site.wsgi; #项目根目录 uwsgi_param UWSGI_CHDIR /mnt/data/www/demosite/demosite/; index index.html index.htm; client_max_body_size 35m; } }
可以 通过 Nginx 安装配置 了解更多内容
设置完成后,在终端运行:
uwsgi --ini /etc/uwsgi_8080.ini &
/usr/local/nginx-1.13.5/sbin/nginx
在浏览器输入:http://127.0.0.1,你就可以看到 django 的 "It work" 了