1. 程式人生 > >mysql + Python3.5.2 + Django + Uwsgi + Nginx實現生產環境

mysql + Python3.5.2 + Django + Uwsgi + Nginx實現生產環境

ast 配置 static var pst ads sgi 服務 關閉進程

官方文檔:http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html

前面已經安裝好mysql數據庫,Python3.5.2, pip, django1.8.5, nginx(前面安裝腳本需要刪除:--without-http_uwsgi_module)


安裝uwsgi:

pip install uwsgi

uwsgi --version

2.0.15


測試uwsgi:

cat test.py

# test.py

def application(env, start_response):

start_response('200 OK', [('Content-Type','text/html')])

return [b"Hello World"] # python3

#return ["Hello World"] # python2


運行:

uwsgi --http :8000 --wsgi-file test.py

瀏覽器訪問服務器IP:8000,返回Hello World,測試正常。

用uwsgi 啟動django測試:

uwsgi --http :8001 --chdir /root/blogproject/ --module blogproject.wsgi

瀏覽器訪問服務器IP:8001,返回網頁內容,測試正常。


把參數寫到配置文件裏:

[uwsgi]

# 項目目錄

chdir=/root/blogproject/

# 指定項目的application

module=blogproject.wsgi

# 進程個數

workers=5

pidfile=/root/blogproject/uwsgi.pid

# 指定IP端口

http= :8080

# 指定靜態文件

static-map=/static=/root/blogproject/static

# 啟動uwsgi的用戶名和用戶組

# uid=root

# gid=root

# 啟用主進程

master=true

# 自動移除unix Socket和pid文件當服務停止的時候

vacuum=true

# 序列化接受的內容,如果可能的話

thunder-lock=true

# 啟用線程

enable-threads=true

# 設置自中斷時間

harakiri=30

# 設置緩沖

post-buffering=4096

# 設置日誌目錄

daemonize=/root/blogproject/script/uwsgi.log

# 指定sock的文件路徑

socket=/root/blogproject/script/uwsgi.sock

#socket = 127.0.0.1:8001

#設置sock文件權限

#chmod-socket = 664


啟動和關閉進程

uwsgi --ini uwsgi.ini # 啟動uwsgi配置

[uWSGI] getting INI configuration from uwsgi.ini

[uwsgi-static] added mapping for /static => /root/blogproject/static # 啟動成功,可以執行ps -ef |grep uwsgi 查看相關進程


uwsgi --stop uwsgi.pid # 關閉uwsgi

uwsgi --reload uwsgi.pid #重新加載配置


Nginx配置:

upstream django {

# server 127.0.0.1:8001;

server unix:///root/blogproject/script/uwsgi.sock;


}

server {

listen 80;

server_name www.victory168.top victory168.top;

access_log /var/log/nginx/access.log; # Nginx log file

error_log /var/log/nginx/error.log; # Nginx error log file

charset utf-8; # Nginx coding

gzip on; # gzip

gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # gzip type


error_page 404 /404.html; #404 file

error_page 500 502 503 504 /50x.html; # 50x file


# set uwsgi path

location / {

include uwsgi_params; # import Nginx module with uWSGI communication

# set uwsgi sock path file

# uwsgi_pass unix:/root/blogproject/script/uwsgi.sock;

uwsgi_pass django;

}


# set static path

location /static/ {

alias /root/blogproject/static/;

# index index.html index.htm;

}

}


nginx -t # 檢查nginx語法問題


django靜態文件收集


1.setting.py設置

DEBUG = False

STATIC_ROOT = os.path.join(BASE_DIR, 'statics')


2. 執行collectstatic命令:

python manage.py collectstatic


3.啟動Nginx和uwsgi程序的用戶應一致


遇到問題查看Nginx error.log和uwsgi.log,


mysql + Python3.5.2 + Django + Uwsgi + Nginx實現生產環境