nginx 基於uwsgi部署Django

1.安裝nginxcss

yum install -y nginx(須要epel源)

2.安裝環境html

能夠考慮使用虛擬化環境,本處再也不使用

3.安裝uwsgipython

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.安裝djangonginx

pip install django==1.11

5.建立django項目sql

django-admin startproject mysite

6.建立appdjango

python manage.py startapp app01

7.修改mysite/settings.pyapp

ALLOWED_HOSTS = ['*']

8.修改mysite/urls.pysocket

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

9.修改app01/views.pyui

from django.shortcuts import render,HttpResponse # Create your views here. def hello(request): print('request is :',request) return HttpResponse('django is ok ')

10.啓動程序url

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

11.uwsgi配置文件

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

[uwsgi]
http = 0.0.0.0:8000
#the local unix socket file than commnuincate to Nginx socket = /data/mysite/mysit.socket # 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; proxy_pass http://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;   }

ps: nginx 鏈接uwsgi一共有三種方式

方式一: uwsgi.ini 裏面指定爲http = 127.0.0.1:8000 nginx的配置文件裏面須要寫 proxy_pass http://127.0.0.1:8000; 方式二: uwsgi.ini裏面指定爲socket = 127.0.0.1:8000 nginx的配置文件須要寫 include /etc/nginx/uwsgi.conf; uwsgi_pass 127.0.0.0:8000; 方式三: uwsgi.ini裏面指定爲socket = /data/mysite/mysite.socket nginx的配置文件須要寫 include /etc/nginx/uwsgi.conf; uwsgi_pass unix:/data/mysite/mysite.socket; 

14.執行命令遷移nginx靜態文件

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

15.查看效果

相關文章
相關標籤/搜索