當(dāng)我們?cè)谟胐jango開發(fā)的web項(xiàng)目時(shí),開發(fā)測(cè)試過(guò)程中用到的是django自帶的測(cè)試服務(wù)器,由于其安全及穩(wěn)定等性能方面的局限性,django官方并不建議將測(cè)試服務(wù)器用在實(shí)際生產(chǎn)。
nginx+uwsgi+django是我們常用的django部署方式。nginx作為最前端的服務(wù)器,他負(fù)責(zé)接收所有的客戶端請(qǐng)求,對(duì)于請(qǐng)求的靜態(tài)文件,由nginx服務(wù)器自己完成,因?yàn)樗哂泻芎锰幚盱o態(tài)文件的能力,性能進(jìn)行過(guò)優(yōu)化,支持高并發(fā)量;uWSGI服務(wù)器作為支持服務(wù)器,是用來(lái)服務(wù)nginx的,nginx將請(qǐng)求的動(dòng)態(tài)文件交給uWSGI進(jìn)行處理。uWSGI實(shí)現(xiàn)了uwsgi、wsgi和http協(xié)議,uwsgi協(xié)議是uWSGI自定義的協(xié)議,定義的是框架(django)和服務(wù)器對(duì)接的接口。
1.安裝項(xiàng)目環(huán)境
系統(tǒng)環(huán)境:ubuntu16.04
python環(huán)境:python3.5.2
Django版本:django1.11.7
nginx環(huán)境:nginx_1.10.3
虛擬環(huán)境:virtualenv15.1.0
uwsgi版本:uwsgi2.0.17.1
安裝并進(jìn)入項(xiàng)目虛擬環(huán)境:
sudo apt-get install virtualenv
virtualenv -p python3 env_my_project
source env_my_project/bin/activate
pip install -r requirements.txt
2.項(xiàng)目配置及運(yùn)行測(cè)試
修改項(xiàng)目配置文件:
cp my_project/settings_local.py.example my_project/settings_local.py
修改es配置文件:
cp rs_es/es_settings.py.example rs_es/es_settings.py
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings_local")
application = get_wsgi_application()
項(xiàng)目運(yùn)行測(cè)試:
python manage.py collectstatic # 收集靜態(tài)文件
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 0.0.0.0:8001
3.NGINX和UWSGI相關(guān)配置
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/my_project
sudo ln -s /etc/nginx/sites-available/my_project /etc/nginx/sites-enabled/
sudo vim /etc/nginx/sites-enabled/my_project
nginx配置:
upstream my_project{
server unix:///var/run/my_project.sock;
}
server {
listen 8001; //服務(wù)的端口號(hào) 服務(wù)通過(guò)nginx與uwsgi通信來(lái)啟動(dòng)
server_name 192.168.xx.xx; //nginx代理的ip
charset utf-8;
# max upload size
client_max_body_size 10M;
# send all non-media requests to the Django server.
location / {
uwsgi_pass my_project;
include /etc/nginx/uwsgi_params;
}
location /static/ {
root /home/ubuntu/my_project;
}
}
Uwsgi配置:
sudo mkdir /var/log/uwsgi
sudo chmod -R 777 /var/log/uwsgi
uwsgi.ini:
[uwsgi]
chdir=/home/ubuntu/my_project
home=/home/ubuntu/my_project/env_my_project
module=my_project.wsgi:application
socket=/var/run/my_project.sock
chmod-socket = 666
master=True
processes = 5
max-requests=5000
# clear environment on exit
vacuum=True
pidfile=/var/run/my_project.pid
daemonize=/var/log/uwsgi/my_project.log
# git pull 自動(dòng)重啟服務(wù)
touch-reload=.git/index
4.配置Emperor mode監(jiān)聽和系統(tǒng)自動(dòng)啟動(dòng)uwsgi
配置Emperor mode監(jiān)聽
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
sudo ln -s /home/ubuntu/my_project/uwsgi.ini /etc/uwsgi/vassals/
系統(tǒng)自動(dòng)啟動(dòng)uwsgi
sudo vim /etc/rc.local
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals
5.通過(guò)uwsgi啟動(dòng)django服務(wù)
啟動(dòng)uwsgi
重啟nginx
sudo service nginx restart
啟動(dòng)django服務(wù)
sudo uwsgi --reload /var/run/my_project.pid
此時(shí)在瀏覽器通過(guò)ngnix代理出來(lái)的ip和端口即可訪問(wèn)服務(wù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。