nginx基於uwsgi部署Django (單機搭建)

nginx基於uwsgi部署Django (單機搭建)

參考連接: https://blog.51cto.com/wangfeng7399html

https://blog.51cto.com/wangfeng7399/2341281python

https://blog.csdn.net/shylonegirl/article/details/83030024nginx

安裝nignx

yum -y install nginx (須要epel源)

安裝依賴包

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

安裝uwsgi,django

pip install --upgrade pip
pip install uwsgi
pip install django==1.11.11

建立django項目

mkdir /data;cd /data  
django-admin startproject myapp #在/data下建立django項目myapp

建立app

python manage.py startapp web

修改myapp/settings.py

myapp是django項目的主目錄web

28 ALLOWED_HOSTS = ['*']
 40     'web', 
121 STATIC_URL = '/static/'                                                             
122 STATICFILES_DIRS=[os.path.join(BASE_DIR, 'static')]

修改myapp/urls.py

from web.views import *
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/',index)
]

修改 myapp/web/view.py

from django.shortcuts import render,HttpResponse

def index(request):
    return HttpResponse('停車坐愛楓林晚,霜葉紅於二月花')
python manage.py runserver 0.0.0.0:8080  
#輸入網址 http://<服務器ip地址>:8080/index, 若是配置正確,能夠訪問web服務

啓動程序

uwsgi --http :8000 --module myapp.wsgi
#此時uwsgi程序已經開啓,切換到django根目錄myapp,輸入網址 http://<服務器ip地址>:8000, 若是配置正確,能夠訪問web服務

新建uwsgi配置文件

若是uwsgi開啓後能夠正常訪問web服務,則繼續配置uwsgi配置文件sql

uwsgi支持ini、xml等多種配置方式,本文以 ini 爲例, 在/etc/目錄下新建uwsgi_nginx.ini,添加以下配置:
[uwsgi]
http = 127.0.0.1:8000
#the local unix socket file than communicate to Nginx
socket = /data/myapp/mysit.socket
#the base directory(full path)
chdir = /data/myapp
#Django's wsgi file
wsgi-file = myapp/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/myapp/uwsgi.log
py-autoreload=1

啓動uwsgi配置文件

uwsgi --ini /etc/uwsgi_nginx.ini

準備配置文件

新建文件 /etc/nginx/uwsgi.conf, 目的是讓uwsgi和nginx互相連通shell

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;

修改配置文件權限

chown nginx.root uwsgi.conf

修改nginx配置文件

/etc/nginx/nginx.confdjango

location / {
             include /etc/nginx/uwsgi.conf;
             proxy_pass http://127.0.0.1:8000;
             root html;
             index index.html index.htm;
        }

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;

驗證是否配置成功

輸入網址: http://<服務器ip地址>/index  #正常能夠訪問web服務
相關文章
相關標籤/搜索