djanog 部署的方式多种多样,本篇采用的是nignx+uwsgi的方式。
安装配置Nignx
Nignx的安装和配置请参考相关文档
安装多版本python
由于我这里用的是python3,但是服务器默认的是python2.7,我们这里需要安装python3的环境。
可以参考 Ubuntu安装多版本python
安装uwsgi
1 | apt-get install python3-dev |
这样大体的流程是:nginx作为服务器最前端,负责接收client的所有请求,统一管理。静态请求由Nginx自己处理。非静态请求通过uwsgi传递给Django,由Django来进行处理,从而完成一次WEB请求。
通信原理是:1
the web client <-> the web server(nginx) <-> the socket <-> uwsgi <-> Django
配置域名
编辑 nginx配置文件:1
2
3
4
5
6
7
8
9
10
11server {
listen 80;
server_name xx.xx.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}
}
然后 重新加载nginx配置1
sudo nginx -s reload
简单的测试
创建一个脚本:1
2
3def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
启动:1
uwsgi --http :8000 --wsgi-file test.py
然后访问你设置的域名,看到hello world 就说明成功了。
启动项目
执行:1
uwsgi --http :8000 --file guowang/wsgi.py
报错:1
2
3
4
5
6
7
8
9*** Operational MODE: single process ***
Traceback (most recent call last):
File "guowang/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 6659, cores: 1)
太费劲了,卒(有空再折腾)