django 是 python 开源的 web 框架 ,django 本身运行后也是一个轻量级的服务器,可以满足本地的测试,生产环境中的 大量访问, 一般和nginx 和 apache 结合。下面以 Django 与nginx 结合为例进行部署
1、 部署环境
操作系统:Ubuntu 16.04.2
nginx 版本:nginx/1.10.3
2、部署原理
client----》nginx ----》socket ----》uwsgi----》Django
3、安装步骤
3.1 安装 nginx
apt-get install nginx
systemctl start nginx
测试 Nginx
3.2 安装 uwsgi
pip install uwsgi
在opt 下面创建 test.py 测试 uwsgi 是否正常安装
1
2
3
4
|
#!/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 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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
本文转自 水滴石川1 51CTO博客,原文链接:http://blog.51cto.com/sdsca/2044983,如需转载请自行联系原作者