【问题标题】:Django app deployment on nGINX在 nGINX 上部署 Django 应用程序
【发布时间】:2012-09-02 00:40:24
【问题描述】:

我想在 nGINX 服务器上部署 Django 应用程序。我正在使用 uWSGI。我查了很多教程,但没有一个有效。 Django 应用程序作为独立应用程序完美运行。在 nGINX 上运行同一个应用程序的最简单方法是什么?

我被困在这里,想要一个解决方案.. :-(

我的 www 文件夹在 /usr/share/nginx/www

我的站点已启用 n conf.d 并且都在 /etc/nginx/

我确实安装了 uWSGI,但找不到任何名为 uwsgi 的文件夹,其中包含 apps-installed 文件夹/文件

【问题讨论】:

  • 你能发布你的配置文件吗?
  • 您使用的是什么服务器操作系统?我们需要查看的三个重要文件是 nginx.conf、启用站点的文件和 uWSGI vassal 配置。
  • 我目前正在我自己的系统(Ubuntu 12.04 LTS)上测试部署
  • 我已经配置了 nginx.cong 和启用站点的文件 不知道 uWSGI vassal 配置.. :-o 我在哪里可以找到它?

标签: python django web-services nginx uwsgi


【解决方案1】:

创建 dJango 应用程序后。只需按照以下步骤操作:

步骤 1. 在您的 Django 项目目录中创建一个 uwsgi.ini 文件。即除了manage.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/sites-available 下添加 .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>;
                   }

步骤 4. 运行 uwsgi.ini

> uwsgi --ini uwsgi.ini

现在对您的 nGINX 的任何请求都会通过 uwsgi 将请求传递给您的 Django 应用程序。享受吧:)

【讨论】:

    【解决方案2】:

    除非您需要坚持使用 uWSGI,否则最简单的方法(并且非常有效)是使用 Gunicorn。他们有很好的文档,而且部署起来非常快速且非常容易。

    我的网站很少(包括生产),类似这样的工作:

    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 应该连接到套接字并为网站提供服务(静态文件将由 NGINX 直接提供,从而为您节省一些内存)。

    有关更多详细信息,请参阅 deployment 和 configuration 上的 Gunicorn 文档。 请注意,我在 Gunicorn 配置中有 daemon=False。这是因为我使用Supervisor 来控制它。你可能想也可能不想去掉那条线。

    【讨论】:

    • 谢谢.. 但我需要坚持使用 uWSGI 是有原因的。如果您对此有任何解决方案,请回复.. :)
    • 抱歉,没用过uWSGI所以帮不上忙:)
    【解决方案3】:

    步骤 1. 在您的系统中安装 Nginx。

    sudo apt-get install nginx
    

    步骤 2. 编辑 Django 项目中的 setting.py 文件

    ALLOWED_HOSTS = []

    到

    ALLOWED_HOSTS = ['*']

    第 3 步。 安装 gunicorn

    pip3 安装 gunicorn

    步骤 4. 创建一个 .service 文件并编写一些自己的配置

        sudo vi /etc/systemd/system/gunicorn.service
    

    [Unit]
    Description=gunicorn daemon
    After=network.target
    
    [Service]
    User=root
    Group=www-data
    WorkingDirectory=<your project root directory>
    ExecStart=<path of your project>/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:<path of your wsgi file>/restapi.sock rest_Api.wsgi:application
    [Install]
    WantedBy=multi-user.target

    第 5 步。 运行给定的命令

      sudo systemctl enable gunicorn
    
      sudo systemctl start gunicorn
    

    第 6 步。 在可用站点中创建一个文件

     sudo vi /etc/nginx/sites-available/gunicorn
    

    步骤 7. 在上述文件中写入给定配置,即 gunicorn

    server {
        listen <port on which you want to configure>;
        server_name <ip address> or <name of the server>;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location <path of your static directory> {
            root <path of your root project>;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:<path of your restapi.sock file>/restapi.sock;
        }
    }

    步骤 8. 现在注释默认文件中的所有行。

        sudo vi /etc/nginx/sites-available/default
    

    第 9 步。运行这些命令进行测试

      sudo ln -s /etc/nginx/sites-available/gunicorn1 /etc/nginx/sites-enabled/
    
      sudo nginx -t
    

    步骤 10. 重启 Nginx 服务器

       sudo systemctl restart nginx
    

    【讨论】:

      【解决方案4】:

      尽量远离与发行版相关的指南,它们很容易将你推向错误的方向。

      按照此处的快速入门:

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

      (对于跟随,我的意思是“理解”而不是复制和粘贴;)

      从一个简单的配置开始,nginx 将所有请求转发到 uWSGI。

      提供静态文件是另一回事,它不依赖于应用程序服务器,因此您可以遵循官方 Django 文档。

      【讨论】:

      • 谢谢.. 我基本上需要的是 dJANGO ---> uWSGI ---> nGINX(在 nGINX 服务器上托管一个 dJANGO Web 服务)
      猜你喜欢
      • 2012-03-16
      • 2011-12-05
      • 2016-09-05
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 2010-10-04
      • 2017-02-01
      • 1970-01-01
      相关资源
      最近更新 更多