架構(gòu):使用Nginx處理靜態(tài)頁(yè)面,使用uWSGI處理Django程序

本案例使用的服務(wù)器IP為:192.168.0.103,文檔里所有的url地址都使用該ip
01 安裝Python36
yum install python36
02 安裝django模塊
pip3 install django==3.0.4 -i https://pypi.tuna.tsinghua.edu.cn/simple
03 創(chuàng)建新項(xiàng)目myweb
創(chuàng)建文件夾,進(jìn)入 /opt/django/
mkdir /opt/django/
cd /opt/django/
創(chuàng)建項(xiàng)目
django-admin startproject myweb
cd myweb
django默認(rèn)使用的是sqlite3,由于centos7自帶sqlite3的版本過(guò)低,會(huì)報(bào)如下錯(cuò)誤,

在myweb/settings.py中DATABASES項(xiàng)配置使用MySQL數(shù)據(jù)庫(kù)myweb,并開(kāi)放外部IP訪(fǎng)問(wèn)
ALLOWED_HOSTS = ["*"] #允許所有外部IP訪(fǎng)問(wèn)
DATABASES = {
#'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#}
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myweb',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
自行修改數(shù)據(jù)連接參數(shù)
04 配置數(shù)據(jù)庫(kù)
所需軟件安裝:
yum install gcc #裝下gcc
yum install mysql-devel #裝下mysql的開(kāi)發(fā)庫(kù)
yum install python36-devel
安裝python的mysql模塊
pip3 install mysqlclient -i https://pypi.tuna.tsinghua.edu.cn/simple
05 創(chuàng)建應(yīng)用myapp
python3 manage.py startapp myapp #創(chuàng)建應(yīng)用
遷移,生成數(shù)據(jù)庫(kù)表
python3 manage.py makemigrations
python3 manage.py migrate
06 WSGI方式運(yùn)行服務(wù)
關(guān)閉防火墻,啟動(dòng)django服務(wù):監(jiān)聽(tīng) 0.0.0.0:8000,允許外部訪(fǎng)問(wèn)
service firewalld stop #關(guān)閉防火墻
python3 manage.py runserver 0.0.0.0:8000 #啟動(dòng)服務(wù)
外部使用:http://192.168.0.103:8000 訪(fǎng)問(wèn),訪(fǎng)問(wèn)成功。

然后Ctrl + C 關(guān)閉服務(wù),下面我們改用 Nginx + uWSGI 架構(gòu)
07 uWSGI 方式運(yùn)行服務(wù)
安裝uWSGI
pip3 install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple
配置uWSGI,在項(xiàng)目目錄下創(chuàng)建uwsgi.ini文件,配置如下:
[uwsgi]
#使用nginx連接時(shí)使用
#socket=127.0.0.1:8080
#直接做web服務(wù)器使用
http=0.0.0.0:8080
#項(xiàng)目目錄
chdir=/opt/django/myweb
#項(xiàng)目中wsgi.py文件的目錄,相對(duì)于項(xiàng)目目錄
wsgi-file=myweb/wsgi.py
processes=4
threads=2
master=True
pidfile=uwsgi.pid
daemonize=uwsgi.log
uWSGI 方式啟動(dòng)服務(wù)
uwsgi --ini uwsgi.ini
確保外部可以訪(fǎng)問(wèn):

先停止服務(wù)
uwsgi --stop uwsgi.pid
08 Nginx + uWSGI 架構(gòu)配置Django
安裝nginx配置源:
新建文件 :/etc/yum.repos.d/nginx.repo,添加并保存內(nèi)容:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
安裝nginx:
yum install nginx
修改nginx配置文件 /etc/nginx/conf.d/default.conf
在server節(jié)點(diǎn)下修改location / 項(xiàng),指向uwsgi的ip與端口
location / {
#將所有的參數(shù)轉(zhuǎn)到uwsgi下
include uwsgi_params;
#uwsgi的ip與端口
uwsgi_pass 127.0.0.1:8080;
}
如圖:

在項(xiàng)目目錄下修改uwsgi.ini文件,將配置中啟用socket,禁用http

關(guān)閉SELINUX,修改/etc/selinux/config文件中設(shè)置SELINUX=disabled ,然后重啟服務(wù)器
如圖:

這個(gè)步驟千萬(wàn)不能漏掉,否則頁(yè)面無(wú)法正常訪(fǎng)問(wèn)
啟動(dòng) uwsgi 服務(wù)器
uwsgi --ini uwsgi.ini
外部使用:http://192.168.0.103 訪(fǎng)問(wèn),訪(fǎng)問(wèn)成功。

09 Nginx 配置靜態(tài)頁(yè)面
修改nginx配置文件 /etc/nginx/conf.d/default.conf,添加代碼, 用于處理靜態(tài)頁(yè)面:
location /static {
alias /opt/django/myweb/static/; #靜態(tài)頁(yè)面的根目錄
}
如圖:

建用靜態(tài)文件夾/opt/django/myweb/static/和新建一個(gè)用于訪(fǎng)問(wèn)的靜態(tài)文件test.html:
mkdir /opt/django/myweb/static/ #新建文件夾
echo "test" > /opt/django/myweb/static/test.html #新建test.html文件
重啟nginx服務(wù)
service nginx restart
訪(fǎng)問(wèn) http://192.168.0.103/static/test.html

成功