1、下载uwsgi
wget http://projects.unbit.it/downloads/uwsgi-lastest.tar.gz
tar -zxvf xxx.tar.gz
cd xxx
python uwsgiconfig.py --build
(如果出现错误参考http://uwsgi-docs.readthedocs.org/en/latest/Install.html,
如在centos6.3上装就是因为没有安装python-devel模块,sudo yum install python-devel后再安装)
cp uwsgi /usr/bin/uwsgi
2、下载并安装django
https://www.djangoproject.com/download/
解压后进入目录执行
sudo python setup.py install
3、创建django工程
进入网站根目录,执行
django-admin.py startproject mysite // 创建工程
Django 1.6以后创建的工程自带wsgi模块
4、配置nginx
cd /etc/nginx/conf.d
sudo cp default.conf testing.conf
sudo vim testing.conf
修改监听端口使其与default.conf中的端口号不同。
如我的testing.conf全部内容为:
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
#uwsgi_pass 127.0.0.1:8000; 使用端口 or 使用sock,推荐使用sock
uwsgi_pass unix:/usr/share/nginx/html/testing/mysite/mysite.sock; # 要和mysite_uwsgi.ini中的配置一样
include uwsgi_params;
}
}
5、配置uwsgi
cd /usr/share/nginx/html/testing/mysite // 进入工程目录
sudo vim mysite_uwsgi.ini
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /usr/share/nginx/html/testing/mysite
# Django's wsgi file
module = mysite.wsgi
# the virtualenv (full path)
# home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 5
# the socket (use the full path to be safe
socket = /usr/share/nginx/html/testing/mysite/mysite.sock # 要和testing.conf中的配置一样 或socket=127.0.0.1:8000
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
5、启动nginx
sudo service nginx start/stop/restart
6、启动uwsgi
sudo uwsgi --ini mysite_uwsgi.ini
7、在浏览器输入地址(http://ip)访问,成功的话将出现下面内容:
如果出错,则查看/var/log/nginx/error.log,根据错误信息来处理。
总结: stack is : the web client <-> the web server <-> the socket <-> uWSGI <-> Python
参考:https://uwsgi.readthedocs.org/en/latest/tutorials/Django_and_nginx.html