2012-09-08 181 views
5

我想在nGINX服務器上部署Django應用程序。我正在使用uWSGI。我查了很多教程,但都沒有工作。 Django應用程序可以作爲獨立應用程序完美運行。在nGINX上運行相同應用的最簡單方法是什麼?在nGINX上部署Django應用程序

我被困在這裏想辦法解決.. :-(

我的WWW文件夾是/usr/share/nginx/www

我啓用的網站-N conf.d而且都在/etc/nginx/

我沒有安裝uWSGI但找不到任何名爲uwsgi的文件夾,它已安裝了應用程序文件夾/文件

+0

您可以發佈你的配置文件? – j0nes

+0

你在使用什麼服務器操作系統?爲了幫助您,需要查看的三個重要文件是nginx.conf,啓用站點的文件和uWSGI vassal配置。 – aychedee

+0

我目前在我自己的系統上測試部署(Ubuntu 12.04 LTS) –

回答

12

一旦你創建了dJango應用程序。只需按照下列步驟操作:

第1步。在您的Django項目目錄中創建一個文件說uwsgi.ini。即除了管理。PY

[uwsgi] 
# set the http port 
http = :<port_no> 

# change to django project directory 
chdir = <project directory> 

# add /var/www to the pythonpath, in this way we can use the project.app format 
pythonpath = /var/www 

# set the project settings name 
env = DJANGO_SETTINGS_MODULE=<project_name>.settings 

# load django 
module = django.core.handlers.wsgi:WSGIHandler() 

步驟2.的/ etc/nginx的/位點可用添加.conf文件

server { 
listen 84; 
server_name example.com; 
access_log /var/log/nginx/sample_project.access.log; 
error_log /var/log/nginx/sample_project.error.log; 

# https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production 
location /static/ { # STATIC_URL 
    alias /home/www/myhostname.com/static/; # STATIC_ROOT 
    expires 30d; 
        } 

     } 

步驟3.在nginx.conf,傳遞請求到您的Django應用程序

在服務器{}塊下,

location /yourapp { 
      include uwsgi_params; 
      uwsgi_pass <your app address, eg.localhost>:<portno>; 
        } 

STEP 4.運行uwsgi.ini

> uwsgi --ini uwsgi.ini 

現在,任何你Nginx的請求將請求傳遞到您的Django應用程序通過uwsgi ..享受:)

1

最簡單的方法在那)將使用Gunicorn,除非你需要堅持uWSGI。他們有很好的文檔,它很快且很容易部署。

我有幾個網站(包括生產)和像這樣的工作:

website_gunicorn.conf.py(任何地方你喜歡):

import multiprocessing 
daemon = False 
bind = "unix:/tmp/gunicorn.sock" 
workers = multiprocessing.cpu_count() * 2 + 1 
timeout = 60 

相應NGINX配置(部分,包括主配置):

upstream gunicorn { 
    server unix:/tmp/gunicorn.sock fail_timeout=0; 
} 

server { 
    listen 80; 
    server_name example.com; 
    access_log /var/log/access.log combined; 
    error_log /var/log/error.log error; 

    keepalive_timeout 5; 

    # path for static files 
    root /path/to/your/static/files; 

    location @django { 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_ignore_client_abort off; 
     proxy_buffering off; 
     proxy_redirect off; 
     proxy_pass http://gunicorn; 
     proxy_read_timeout 60; 
    }   

    location/{ 
     try_files $uri @django; 
    } 
} 

那麼你應該能夠在安裝後Gunicorn開始像這樣(當然 - pip install gunicorn):

gunicorn_django -c /path/to/website_gunicorn.conf.py 

和NGINX應該連接到socket併爲網站提供服務(靜態文件將由NGINX直接提供,節省你一些內存)。

欲瞭解更多詳情,請參閱Gunicorn文檔deploymentconfiguration。 請注意,我在Gunicorn配置中有daemon=False。這是因爲我用Supervisor來控制它。你可能會也可能不想要擺脫那條線。

+0

謝謝..但我需要堅持與uWSGI的原因。請回復,如果你有任何解決方案.. :) –

+0

對不起,沒有使用uWSGI所以不能幫助:) – kgr

1

儘量避免與發行版相關的howtos,他們可以輕易地將您推向錯誤的方向。

按照快速入門的位置:

http://projects.unbit.it/uwsgi/wiki/Quickstart

(後續,我的意思是, '理解' 不可複製&糊;)

開始以簡單的結構,其中nginx的前鋒所有的請求以uWSGI。

提供靜態文件是另一回事,它不依賴於應用程序服務器,因此您可以按照官方的Django文檔進行操作。

+0

謝謝.. 我基本需要的是 dJANGO ---> uWSGI --- > nGINX (在nGINX服務器上託管dJANGO網絡服務) –

相關問題