使用nginx+uwsgi+django启动一个项目

安装uwsgi

1 pip install uwsgi

测试uwsgi能否正常使用:

新建test_uwsgi.py,内容如下:

1 # -*- conding: utf-8 -*-
2 
3 
4 def application(env, start_response):
5     start_response('200 OK', [('Content-Type', 'text/html',)])
6     return 'Hello World'

然后在命令行 运行

1 uwsgi --http :8001 --wsgi-file test_uwsgi.py

在浏览器访问8001端口,看是否能正常访问,输出Hello World

安装Django

 

1 pip install django

 

创建一个django项目

1 django-admin.py startproject demosite

使用django 自带的wsgi运行django项目

1 cd demosite
2 python manage.py runserver 0.0.0.0:8001

在浏览器中访问8001 端口,出现It worked!表明启动成功

使用uwsgi启动django项目

新建一个uwsgi.ini文件,内容如下

1 [uwsgi]
2 chdir = /mnt/python/demosite
3 master = True
4 vacuum = True
5 http = 0.0.0.0:8001
6 pidfile = /var/run/uwsgi8001.pid
7 wsgi-file = /mnt/python/demosite/demosite/wsgi.py

启动uwsgi

1 uwsgi --ini uwsgi.ini

在浏览器中访问8001 端口,出现It worked!表明启动成功

nginx 配置

安装nginx

1 apt-get install -y  nginx-full

新建 nginx.conf 内容如下

 1 upstream django {
 2     server 0.0.0.0:8001; # for a web port socket (we'll use this first)
 3 }
 4 
 5 # configuration of the server
 6 server {
 7     listen      8000;
 8     charset     utf-8;
 9     location / { 
10         uwsgi_pass  django;
11         uwsgi_connect_timeout 30; 
12         include     /etc/nginx/uwsgi_params;
13     }   
14 }

将nginx.conf 软链接到 /etc/nginx/sites-enabled 目录下
重启nginx

1 /etc/init.d/nginx restart

然后将上面uwsgi.ini 配置文件中的配置

1 http = 0.0.0.0:8001

改成

1 socket = 0.0.0.0:8001

重启下 uwsgi 

1 uwsgi --ini uwsgi.ini

接着在浏览器中访问 8000端口 ,出现It worked!表明配置成功

posted @ 2017-10-22 17:50  这个冬天有点冷灬  阅读(335)  评论(0)    收藏  举报