1. 程式人生 > >linux下通過Nginx部署django專案

linux下通過Nginx部署django專案

安裝Nginx
sudo apt-get install nginx  #安裝
啟動Nginx
fnngj@ubuntu:~$ /etc/init.d/nginx start  #啟動
fnngj@ubuntu:~$ /etc/init.d/nginx stop  #關閉
fnngj@ubuntu:~$ /etc/init.d/nginx restart  #重啟
安裝uwsji
sudo python3 -m pip install uwsgi
測試uwsji
在Django專案下新建test.py檔案,
# test.py
def application(env, start_response)
:
start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"] # python2 #return [b"Hello World"] # python3 然後執行下列命令 uwsgi --http :8001 --plugin python --wsgi-file test.py 然後開啟ttp://localhost:8001看是否執行正常
然後連結Django和uwsgi,實現簡單的web伺服器,到Django專案目錄下執行shell:
uwsgi --http :8001 --plugin
python --module blog.wsgi blog為你的專案名。訪問http://localhost:8001,專案正常。注意這時專案的靜態檔案是不會被載入的,需要用nginx做靜態檔案代理。
#在專案目錄下建立uwsgi.ini檔案, 程式碼如下

# myweb_uwsgi.ini file
[uwsgi]

# Django-related settings

socket = :8000

# the base directory (full path)
chdir           = /home/blog

# Django s wsgi file
module          = myweb.wsgi

# process-related settings
# master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true

待續