最近使用django寫了一些項目,不過部署到服務器上碰到一些問題,還有靜態文件什么的一堆問題,這里總結一下碰到的問題和解決方案,總體思路是按照官方文檔走的。
原文地址:http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
講的很清楚,不過還是需要一些注意的地方
對於uwsgi+nginx的部署方式,它的訪問關系大概是:
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
如果你需要使用virtualenv:
virtualenv uwsgi-tutorial cd uwsgi-tutorial source bin/activate
安裝django:
pip install Django django-admin.py startproject mysite cd mysite
這里假設的你域名是:example.com,在后面的你可以換成你的域名或者IP地址。
原教程中使用的是8000端口號,我們這直接使用80端口。
基於uwsgi
安裝uwsgi
pip install uwsgi
先做個測試uwsgi是否能正常使用,創建一個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:
uwsgi --http :8000 --wsgi-file test.py
參數解釋:
http :8000:指示用的是8000端口
wsgi-file test.py:加載指定文件 test.py
然后你就可以嘗試訪問了:
http://example.com:8000
接下來我們在django項目上嘗試一下
新建的django項目需要先:
python manage.py migrate
python manage.py runserver
如果能夠運行:
uwsgi --http :8000 --module mysite.wsgi
參數:
module mysite.wsgi :指定wsgi
嘗試訪問:
http://example.com:8000
現在的結構類似於:
the web client <-> uWSGI <-> Django
基於 nginx
安裝nginx
sudo apt-get install nginx sudo /etc/init.d/nginx start # start nginx
也可以用nginx服務命令比如
sudo service nginx start sudo service nginx stop sudo service nginx restart
現在訪問http://127.0.0.1/就能看到默認的nginx主頁
然后我們來配置nginx文件,先對原始配置文件做個備份
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak
然后編輯配置文件
sudo vim /etc/nginx/sites-available/default
# default # the upstream component nginx needs to connect to upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001; # for a web port socket (we'll use this first) } # configuration of the server server { # the port your site will be served on listen 80 default_server; listen [::]:80 default_server ipv6only=on; # the domain name it will serve for server_name .example.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /path/to/your/mysite/media; # your Django project's media files - amend as required } location /static { alias /path/to/your/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed }
一下是我的配置信息供參考
upstream django { server unix:///home/ubuntu/blogsite/mysite.sock; # for a file socket #server 127.0.0.1:8001; # for a web port socket (we'll use this first) } server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; # Make site accessible from http://localhost/ server_name localhost; charset utf-8; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; client_max_body_size 75M; location /media { alias /home/ubuntu/blogsite/media; } location /static { alias /home/ubuntu/blogsite/static; } location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules uwsgi_pass django; include /etc/nginx/uwsgi_params; }
下面我們對django進行一下配置,編輯django配置文件mysite/settings.py 加上:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
這些配置信息具體用處可在我的另一篇文章中看到。
然后執行命令
python manage.py collectstatic
然后重啟nginx
sudo /etc/init.d/nginx restart
現在可以在django項目中放幾個靜態文件,看是否能訪問:
比如將一個media.png的圖片放在mysite/media 文件夾中(沒有media文件夾可以自己創建一個)
然后訪問
http://example.com/media/media.png
就能訪問到這個圖片。
nginx,uwsgi和test.py
還記得我們創建的test.py文件,現在我們再讓它發揮一下作用
再test.py 的目錄下執行命令:
uwsgi --socket :8001 --wsgi-file test.py
這里使用的8001端口號跟上面的配置文件中的
upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001; # for a web port socket (we'll use this first) }
相對應,我們現在是用的端口socket所以使用下面的配置,以后會使用到文件socket
然后我們訪問網站就能看到test.py 文件返回的內容了
這次我們的訪問順序類似於:
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
注:如果上文中的8001端口不能使用可改用其他端口號
使用unix sockets文件代替端口
前面我們使用了tcp端口,十分簡單,但是我們最好使用sockets文件,這樣能減少資源消耗
我們將default的配置稍作修改
server unix:///path/to/your/mysite/mysite.sock; # for a file socket # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
然后在django目錄中使用命令
uwsgi --socket mysite.sock --wsgi-file test.py
然后訪問網站,
注:如果不能訪問(一般來說訪問不了!!),我們check一下nginx的錯誤日志,
vim /var/log/nginx/error.log
如果在里面看到類似於
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission denied)
那是權限問題,我們改用下面的命令
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666
如果能正常訪問了,那我們來試試使用wsgi來訪問django項目
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666
然后我們使用.ini文件來配置uwsgi(差不多就能完成了),在項目目錄下創建mysite_uwsgi.ini
# mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /path/to/your/project # Django's wsgi file module = project.wsgi # the virtualenv (full path) #home = /path/to/virtualenv # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe socket = /path/to/your/project/mysite.sock # ... with appropriate permissions - may be needed chmod-socket = 666 # clear environment on exit vacuum = true
一下是我的配置,供參考
[uwsgi] # Django-related settings # the base directory (full path) chdir = /home/ubuntu/blogsite # Django's wsgi file module = blogsite.wsgi # the virtualenv (full path) # home = /path/to/virtualenv # process-related settings # master master = true # maximum number of worker processes processes = 2 # the socket (use the full path to be safe socket = /home/ubuntu/blogsite/mysite.sock # ... with appropriate permissions - may be needed chmod-socket = 666 # clear environment on exit vacuum = true
然后,跑起來
uwsgi --ini mysite_uwsgi.ini
以上,使用uWSGI+nginx部署Django項目就算是完成了,還有其它的配置可參考官方文檔(比如怎樣服務開機自啟)
http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
歡迎多來訪問博客:http://liqiongyu.com/blog
微信公眾號: