nginx+uWSGI+Django框架学习

一.Django

1.利用 pip 安装 Django
[shell]pip install Django[/shell]

提示:

Complete output from command python setup.py egg_info:

==========================
Unsupported Python version
==========================

This version of Django requires Python 3.4, but you’re trying to
install it on Python 2.7.

This may be because you are using a version of pip that doesn’t
understand the python_requires classifier. Make sure you
have pip >= 9.0 and setuptools >= 24.2, then try again:

$ python -m pip install –upgrade pip setuptools
$ python -m pip install django

This will install the latest version of Django which works on your
version of Python. If you can’t upgrade your pip (or Python), request
an older version of Django:

$ python -m pip install “django<2"

—————————————-
暂时不想升级python,执行:
[shell]python -m pip install "django<2"[/shell]

检查是否安装成功,python环境执行:
[shell]
>>> import django
>>> django.VERSION
(1, 11, 10, u’final’, 0)
[/shell]

2.新建Django项目
[shell]django-admin.py startproject lzyone[/shell]
Django项目目录结构
[shell]
# tree
.
├── lzyone
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
[/shell]

二. uWSGI

1.安装uWSGI

[shell]
pip install uwsgi
[/shell]

2.运行uWSGI
配置文件:
[shell]
[uwsgi]
socket = 127.0.0.1:9090
chdir = /var/www/lzyone
module = lzyone.wsgi
master = true;
processes = 1
threads = 1
daemonize = /var/www/lzyone/uwsgi.log
[/shell]
执行:
[shell]
uwsgi –ini /usr/local/uwsgi/uwsgi.ini &
[/shell]

三. Nginx

配置文件:
[code]
server {
listen 80;
server_name lzy.one www.lzy.one;

location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
#uwsgi_param UWSGI_PYHOME /var/www/lzyone;
#uwsgi_param UWSGI_CHDIR /var/www/lzyone;
#uwsgi_param UWSGI_SCRIPT lzyone.wsgi;
#root /var/www/lzyone/;
#index index.htm;
}
}
[/code]

四.测试

浏览器访问http://lzy.one
报错:

DisallowedHost at /
Invalid HTTP_HOST header: ‘lzy.one’. You may need to add u’lzy.one’ to ALLOWED_HOSTS.

解决方法:
修改setting.py文件
ALLOWED_HOSTS = [‘*’] #在这里请求的host添加了*
修改完成后重启uwsgi

It worked!