1. 程式人生 > >nginx基於uwsgi部署Django

nginx基於uwsgi部署Django

location set sys mys 目錄 max zlib min pin

1.安裝nginx

yum install -y nginx(需要epel源)

2.安裝uwsgi

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

3.安裝django

pip install django==1.11

4.創建django項目

#/data   目錄下創建mysite項目
django-admin startproject mysite

5.創建app

python manage.py startapp app01

6.修改mysite/settings.py

ALLOWED_HOSTS = [*]

7.關閉防火墻

關閉內置防火墻selinux

#暫時停止selinxu
setenforce 0
#永久關閉selinux
vi /etc/selinux/conf 
修改如下行
SELINUX=enforcing
重啟機器,使得selinx永久關閉

關閉軟件防火墻

iptables -F    #清空防火墻規則
systemctl stop firewalld #關閉軟件防火墻 systemctl disable firewalld #刪除iptables的開機自啟

8、uwsgi配置文件

uwsgi支持ini、xml等多種配置方式,本文以 ini 為例, 在/etc/目錄下新建uwsgi.ini,添加如下配置:
#三種配置方法可任選其中一種,選擇哪種則將哪種的註釋打開即可
[uwsgi]
http = 0.0.0.0:8000      #第一種配置方法
#socket = 0.0.0.0:8000    #第二種配置方法
#the local unix socket file than commnuincate to Nginx
#socket = /data/mysite/mysite.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

第一種使用http = 0.0.0.0:8000,對應nginx配置文件如下:

vi /etc/nginx/nginx.conf

location / {
        include  /etc/nginx/uwsgi_params;
        proxy_pass http://127.0.0.1:8000;
        }

第二種使用socket = 0.0.0.0:8000,對應nginx配置文件如下:

vi /etc/nginx/nginx.conf

location / {
        include  /etc/nginx/uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        }

第三種使用socket = /data/mysite/mysite.socke,對應nginx配置文件如下:

vi /etc/nginx/nginx.conf

location / {
        include  /etc/nginx/uwsgi_params;
        uwsgi_pass unix:/data/mysite/mysite.socket;
        }

啟動uwsgi

uwsgi --ini /etc/uwsgi.ini

啟動nginx

systemctl start nginx

此時可以通過主機地址:80端口訪問我們的django項目啦!

nginx基於uwsgi部署Django