第一步。
sudo apt-get update
sudo apt-get upgrade
先更新。 。
Django的主流部署方式:nginx+uwsgi+django
第二步,安裝nginx
sudo apt-get install nginx
#安裝nginx,如果需要安裝最新的nginx需從官網下載原始碼包手動編譯。
nginx的大致檔案結構。
1.設定檔:/etc/nginx
2.程式:/usr/sbin/nginx
3.日誌:/var/log/nginx/access. log - error.log
第三步,安裝uwsgi
sudo apt-get install python3-dev
sudo apt-get install python3- pip
sudo pip3 install uwsgi(此步驟之前,可以換下pip來源以提高下載速度。在~/.pip下建立pip.conf寫入
[global]
trusted-host = pypi.douban.com
index-url = )
uwsgi是一個web伺服器,它實現了WSGI協定、uwsgi、http等協議。 Nginx中HttpUwsgiModule的作用是與uWSGI伺服器進行交換。
大致流程是 : 客戶端<==>nginx<==>uwsgi<==>Django。靜態請求由Nginx自己處理。非靜態請求透過uwsgi傳遞給Django,由Django來處理,從而完成一次WEB請求。
建立Django測試項目,django-admin startproject mysite,cd mysite,python manage.py startapp demo1。
第四步,測試uwsgi
在mysite目錄下新測試檔案,nano test.py.
寫入:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')])return ["Hello World"]
執行:
uwsgi --http :8001 --plugin python --wsgi-file test.py
訪問正常。
第五步,測試Django
python manage.py runserver 0.0.0.0:8002
存取正常。
連接Django和uwsgi。
uwsgi --http:8001 --plugin python --module mysite.wsgi
存取正常。
第六步,設定uwsgi
uwsgi支援透過多種設定檔形式啟動,這裡採用ini設定檔的方法.
新uwsgi:nano uwsgi.ini
# mysite_uwsgi.ini file[uwsgi] socket = 127.0.0.1:3400# Django-related settings # the django project directory (full path) chdir = /home/ubuntu/mysite # Django's wsgi filemodule = mysite.wsgi # process-related settings # master master = true# maximum number of worker processes processes = 2threads = 2max-requests = 6000# ... with appropriate permissions - may be neededchmod-socket = 664# clear environment on exit vacuum = true
##訪問時報錯誤,
invalid request block size: <span class="hljs-built_in">21573 (<span class="hljs-number">max <span class="hljs-built_in">4096)...skip<span class="hljs-number"></span></span></span></span>。
daemonize = /home/ubuntu/mysite/uwsgi.log
server { # the port your site will be served on listen 80; # the domain name it will serve forserver_name 127.0.0.1; # substitute your machine's IP address or FQDNcharset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /home/ubuntu/mysite/media; # your Django project's media files - amend as required } location /static { alias /home/ubuntu/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { include uwsgi_params; # the uwsgi_params file you installed uwsgi_pass 127.0.0.1:8001;#此处跟uwsgi配置文件保持一致 } }
以上是Ubuntu14.04的部署實例介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!