nginx based uwsgi deploy Django (stand-alone building)
Reference Links: https://blog.51cto.com/wangfeng7399
https://blog.51cto.com/wangfeng7399/2341281
https://blog.csdn.net/shylonegirl/article/details/83030024
Installation nignx
yum -y install nginx (需要epel源)
Installation dependencies
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
AnSo uwsgi, django
pip install --upgrade pip
pip install uwsgi
pip install django==1.11.11
Create a django project
mkdir /data;cd /data
django-admin startproject myapp #在/data下创建django项目myapp
Creating app
python manage.py startapp web
Modify the myapp / settings.py
myapp is the home directory django project
28 ALLOWED_HOSTS = ['*']
40 'web',
121 STATIC_URL = '/static/'
122 STATICFILES_DIRS=[os.path.join(BASE_DIR, 'static')]
Modify the myapp / urls.py
from web.views import *
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/',index)
]
Modify the 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服务
starting program
uwsgi --http :8000 --module myapp.wsgi
#此时uwsgi程序已经开启,切换到django根目录myapp,输入网址 http://<服务器ip地址>:8000, 如果配置正确,可以访问web服务
New uwsgi profile
If after uwsgi turn can access the web service, continue to configure uwsgi profile
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
Start uwsgi profile
uwsgi --ini /etc/uwsgi_nginx.ini
Preparation Profile
New File /etc/nginx/uwsgi.conf, the purpose is to let nginx uwsgi and communicate with each other
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;
Modify the configuration file permissions
chown nginx.root uwsgi.conf
Nginx modify configuration files
/etc/nginx/nginx.conf
location / {
include /etc/nginx/uwsgi.conf;
proxy_pass http://127.0.0.1:8000;
root html;
index index.html index.htm;
}
ps: nginx There are three ways to connect 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;
Verify that the configuration is successful
输入网址: http://<服务器ip地址>/index #正常可以访问web服务