nginx 基于uwsgi部署Django

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx 基于uwsgi部署Django相关的知识,希望对你有一定的参考价值。

1.安装nginx

yum install -y nginx(需要epel源)

2.安装环境

可以考虑使用虚拟化环境,本处不再使用

3.安装uwsgi

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
pip install uwsgi

4.安装django

pip install django==1.11

5.创建django项目

django-admin startproject mysite

6.创建app

python manage.py startapp app01

7.修改mysite/settings.py

ALLOWED_HOSTS = [‘*‘]

8.修改mysite/urls.py

from app01 import views
urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r‘^hello_django/‘, views.hello),
]

9.修改app01/views.py

from django.shortcuts import render,HttpResponse

# Create your views here.
def hello(request):
    print(‘request is :‘,request)
    return HttpResponse(‘django is ok ‘)

10.启动程序

uwsgi --http :8000 --module mysite.wsgi

11.uwsgi配置文件

uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/目录下新建uwsgi_nginx.ini,添加如下配置:

[uwsgi]
http = :8000
#the local unix socket file than commnuincate to Nginx
socket = 0.0.0.0:8000
# the base directory (full path)
chdir = /data/mysite
# Django‘s wsgi file
wsgi-file = mysite/wsgi.py
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
# clear environment on exit
vacuum          = true
daemonize = /data/mysite/uwsgi.log
py-autoreload=1

11.准备配置文件

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

12.修改配置文件权限

chown nginx.root uwsgi_params

13.修改nginx配置文件

location / {
        include  /data/mysite/conf/uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
            root   html;
            index  index.html index.htm;
        }
        }
location /static{
     alias /data/mysite/static;   
 }
     #nginx处理媒体资源
location /media{
     alias /data/mysite/media;  
 }

14.执行命令迁移nginx静态文件

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
python manage.py collectstatic

15.查看效果

以上是关于nginx 基于uwsgi部署Django的主要内容,如果未能解决你的问题,请参考以下文章

跨过Nginx上基于uWSGI部署Django项目的坑

nginx基于uwsgi部署Django

CentOS 7 下Django项目部署教程(基于uwsgi和Nginx)

nginx 基于uwsgi部署Django

使用Nginx+uWSGI部署Django项目

基于Django的Nginx+uwsgi的服务器前置环境部署