环境搭建
1. 安装 uwsgi,nginx 和 django
- apt install nginx
- pip install uwsgi
- pip install django
2. 测试 uwsgi 和 nginx 的连接
PS: 下面的例子采用的是 unix socket 的链接发送
创文件 foobar.py
- def application(env, start_response):
- start_response('200 OK', [('Content-Type','text/html')])
- return [b"Hello World"] # python3
- #return ["Hello World"] # python2
创文件 foobar_uwsgi.INI
- [uwsgi]
- # Django-related settings
- # the base directory (full path)
- chdir = /home/dmd/project/ENV/mysite
- # Django's wsgi file
- module = foobar
- # process-related settings
- # master
- master = true
- # maximum number of worker processes
- processes = 10
- # the socket (use the full path to be safe
- socket = /home/dmd/project/ENV/mysite/foobar.sock
- # ... with appropriate permissions - may be needed
- # chmod-socket = 664
- # clear environment on exit
- # 这个配置本来是 true, 即退出就删掉 socket, 但这会导致 nginx 的启动失败, 所以改为 false
- vacuum = false
创文件 foobar_nginx.conf
- server {
- listen 8099;
- server_name 127.0.0.1
- charset UTF-8;
- access_log /var/log/nginx/myweb_access.log;
- error_log /var/log/nginx/myweb_error.log;
- client_max_body_size 75M;
- location / {
- include uwsgi_params;
- uwsgi_pass unix:///home/dmd/project/ENV/mysite/foobar.sock; # 用 unix socket
- # uwsgi_pass 127.0.0.1:8000 # 用 TCP socket
- uwsgi_read_timeout 2;
- }
- }
将这个文件链接到 / etc/nginx/sites-enabled, 这样 nginx 就可以看到它了
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
启动 nginx
sudo service nginx start
启动 uwsgi
uwsgi --INI foobar_uwsgi.INI
访问 127.0.0.1:8099, 如果出现 "Hello world" 就说明下面连接栈是成功的.
the Web client <-> the Web server <-> the socket <-> uWSGI <-> Python
3. 建立整个连接栈
the Web client <-> the Web server <-> the socket <-> uwsgi <-> Django
建立 django 项目
django-admin startproject mysite
在项目的根目录建立 mysite_uwsgi.INI
- # myweb_uwsgi.INI file
- [uwsgi]
- # Django-related settings
- socket = mysite.sock
- # the base directory (full path)
- chdir = /home/dmd/project/ENV/mysite
- # Django s wsgi file
- module = mysite.wsgi
- # 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 = false
在项目根目录建立 mysite_nginx.conf
- server {
- listen 8099;
- server_name 127.0.0.1
- charset UTF-8;
- access_log /var/log/nginx/myweb_access.log;
- error_log /var/log/nginx/myweb_error.log;
- client_max_body_size 75M;
- location / {
- include uwsgi_params;
- uwsgi_pass unix:///home/dmd/project/ENV/mysite/mysite.sock; # 用 unix socket
- # uwsgi_pass 127.0.0.1:8000 # 用 TCP socket
- uwsgi_read_timeout 2;
- }
- location /static {
- expires 30d;
- autoindex on;
- add_header Cache-Control private;
- alias /home/dmd/project/ENV/mysite/static/;
- }
- }
链接 sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
运行
- # 运行 uwsgi
- uwsgi --INI mysite_uwsgi.INI
- # 开启 niginx
- sudo service nginx start
测试. 访问 127.0.0.1:8099 , 如果看到 django 的页面, 说明成功.
完整的目录树
mysite/
├── db.sqlite3
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── mysite.sock
├── mysite_nginx.conf
├── mysite_uwsgi.INI
└── uwsgi_params
来源: http://www.jianshu.com/p/22beb24b3841