Django-Nginx-uwsgi

weixin_34236869發表於2017-01-05
在開發環境下除錯好python專案之後,把專案遷移到nginx上面
首先把django admin的靜態檔案目錄複製一份到你設定的static檔案目錄下:
python manage.py collectstatic
關閉settings.py中的debug,關閉debug之後staticfiles就不生效了,需要在nginx配置靜態檔案的訪問。
安裝uwsgi
pip install uwsgi
建立wsgi檔案:
root@server1 testweb]# pwd

/Django/testweb

[root@server1 testweb]# cat wsgi.py

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testweb.settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()
建立ini配置檔案:
[root@server1 testweb]# pwd

/Django/testweb

[root@server1 testweb]# cat app.ini

[uwsgi]

chdir = /Django/testweb/

;wsgi-file = /Django/testweb/wsgi.py

module = testweb.wsgi

socket = 127.0.0.1:3400

;socket = /var/log/%(project).sock

chmod-socket = 664

;http = 0.0.0.0:8001

;stats = 0.0.0.0:8001

master = true

processes = 4

threads = 2

max-requests = 6000

vacuum = true

pidfile = /var/log/uwsgi.pid;

daemonize = /var/log/uwsgi.log
啟動uwsgi:
[root@server1 testweb]# uwsgi --ini app.ini 
也可編寫uwsgi啟動指令碼:
[root@server1 ~]# cat /etc/init.d/uwsgi 

uwsgi_path=/usr/bin/uwsgi

uwsgi_ini=/Django/testweb/app.ini

uwsgi_pid=/var/log/uwsgi.pid

if [ ! -n $1 ]

then

 echo "Usages: [start|stop|restart]"

 exit 0

fi

if [ $1 = start ]

then

 psid=`ps aux | grep "zh" | grep -v "grep" | wc -l`

 if [ $psid -gt 4 ]

 then

 echo "uwsgi is running!"

 exit 0

 else

 uwsgi --ini $uwsgi_ini

 echo "Start uwsgi service [OK]"

 fi

elif [ $1 = stop ];then

 killall -s INT uwsgi

 echo "Stop uwsgi service [OK]"

elif [ $1 = restart ];then

 #killall -s INT uwsgi

 #uwsgi --ini uwsgi_ini

 kill -HUP $uwsgi_pid

 echo "Restart uwsgi service [OK]"

else

 echo "Usages: [start|stop|restart]"

fi

[root@server1 ~]# cat /etc/init.d/uwsgi 

uwsgi_path=/usr/bin/uwsgi

uwsgi_ini=/Django/testweb/app.ini

uwsgi_pid=/var/log/uwsgi.pid

if [ ! -n $1 ]

then

 echo "Usages: [start|stop|restart]"

 exit 0

fi

if [ $1 = start ]

then

 psid=`ps aux | grep "zh" | grep -v "grep" | wc -l`

 if [ $psid -gt 4 ]

 then

 echo "uwsgi is running!"

 exit 0

 else

 uwsgi --ini $uwsgi_ini

 echo "Start uwsgi service [OK]"

 fi

elif [ $1 = stop ];then

 killall -s INT $uwsgi_path

 echo "Stop uwsgi service [OK]"

elif [ $1 = restart ];then

 #killall -s INT uwsgi

 #uwsgi --ini uwsgi_ini

 kill -HUP $uwsgi_pid

 echo "Restart uwsgi service [OK]"

else

 echo "Usages: [start|stop|restart]"

fi
配置nginx.conf檔案,新增下面一行內容:
 include /usr/local/nginx/conf/conf.d/*;
在conf.d新建app.conf檔案:
[root@server1 conf.d]# cat app01.conf 

server {

listen 80;

server_name localhost; 

charset utf-8;

access_log logs/testweb/app01/access_log;

error_log logs/testweb/app01/error_log;

client_max_body_size 75M; # adjust to taste

#location /admin {

#alias /Django/testweb/static/admin

# }

location /static {

alias /Django/testweb/static; # your Django project's static files - amend as required

}

# Finally, send all non-media requests to the Django server.

location / {

 uwsgi_pass 127.0.0.1:3400;

 include uwsgi_params; # the uwsgi_params file you installed

}

}
重新啟動Nginx:
[root@server1 conf.d]# /etc/init.d/nginx stop

[root@server1 conf.d]# /etc/init.d/nginx start
訪問頁面測試:
4262139-c396569a123c865f.png

4262139-a6aaeea6463181f2.png

除錯過程中,報錯多多檢視日誌排錯。