最近使用django寫了一些項目,不過部署到服務器上碰到一些問題,還有靜態文件什麼的一堆問題,這裏總結一下碰到的問題和解決方案,整體思路是按照官方文檔走的。html
原文地址:http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.htmlpython
講的很清楚,不過仍是須要一些注意的地方nginx
對於uwsgi+nginx的部署方式,它的訪問關係大概是:web
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
若是你須要使用virtualenv:django
virtualenv uwsgi-tutorial cd uwsgi-tutorial source bin/activate
安裝django:ubuntu
pip install Django django-admin.py startproject mysite cd mysite
這裏假設的你域名是:example.com,在後面的你能夠換成你的域名或者IP地址。vim
原教程中使用的是8000端口號,咱們這直接使用80端口。bash
安裝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
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
就能訪問到這個圖片。
還記得咱們建立的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端口不能使用可改用其餘端口號
前面咱們使用了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
微信公衆號: