2012-09-08 176 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网络服务) –

相关问题