使用 Nginx、Apache、mod_wsgi 部署 Django 应用程序

JCW*_*ong 14 deployment nginx django mod-wsgi apache-2.2

我有一个可以使用标准开发环境在本地运行的 django 应用程序。我现在想将其移至 EC2 进行生产。django 文档建议使用 apache 和 mod_wsgi 运行,并使用 nginx 加载静态文件。

我在 Ec2 机器上运行 Ubuntu 12.04。我的 Django 应用程序“ddt”包含一个带有 ddt.wsgi 的子目录“apache”

import os, sys
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append('/usr/lib/python2.7/site-packages/django/')
sys.path.append('/home/jeffrey/www/ddt/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ddt.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Run Code Online (Sandbox Code Playgroud)

我从 apt 安装了 mod_wsgi。我的 apache/httpd.conf 包含

NameVirtualHost *:8080

WSGIScriptAlias / /home/jeffrey/www/ddt/apache/ddt.wsgi
WSGIPythonPath /home/jeffrey/www/ddt

<Directory /home/jeffrey/www/ddt/apache/>
<Files ddt.wsgi>
Order deny,allow
Allow from all
</Files>
</Directory>
Run Code Online (Sandbox Code Playgroud)

在 apache2/sites-enabled 下

<VirtualHost *:8080>
ServerName www.mysite.com
ServerAlias mysite.com
<Directory /home/jeffrey/www/ddt/apache/>
    Order deny,allow
    Allow from all
</Directory>
LogLevel warn
ErrorLog  /home/jeffrey/www/ddt/logs/apache_error.log
CustomLog /home/jeffrey/www/ddt/logs/apache_access.log combined
WSGIDaemonProcess datadriventrading.com user=www-data group=www-data threads=25
WSGIProcessGroup datadriventrading.com
WSGIScriptAlias / /home/jeffrey/www/ddt/apache/ddt.wsgi
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

如果我是对的,上面的这3 个文件应该可以正确地允许我的 django 应用程序在端口8080上运行。

我有以下 nginx/proxy.conf 文件

proxy_redirect              off;
proxy_set_header            Host $host;
proxy_set_header            X-Real-IP $remote_addr;
proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size        10m;
client_body_buffer_size     128k;
proxy_connect_timeout       90;
proxy_send_timeout          90;
proxy_read_timeout          90;
proxy_buffer_size           4k;
proxy_buffers               4 32k;
proxy_busy_buffers_size     64k;
proxy_temp_file_write_size  64k;
Run Code Online (Sandbox Code Playgroud)

在 nginx/sites-enabled 下

server {
  listen 80;
  server_name www.mysite.com mysite.com;
  access_log /home/jeffrey/www/ddt/logs/nginx_access.log;
  error_log /home/jeffrey/www/ddt/logs/nginx_error.log;
  location / {
    proxy_pass http://127.0.0.1:8080;
    include     /etc/nginx/proxy.conf;
  }
  location  /media/ {
   root /home/jeffrey/www/ddt/;
  }
}      
Run Code Online (Sandbox Code Playgroud)

如果我是正确的,这两个文件应该设置 nginx 来接受 HTTP 端口 80 上的请求,然后将请求直接请求到在端口 8080 上运行 django 应用程序的 apache。如果我去 mysite.com,我看到的只是欢迎使用 Nginx !

关于如何调试这个有什么建议吗?

yog*_*hal 0

首先请确保您可以在 127.0.0.1:8080 上访问您的应用程序并请发布 nginx_error.log 的内容。尝试复制粘贴以下 nginx conf 文件并检查它是否正常工作。我在 python 应用程序中使用相同的配置。

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";


    ##
    # Virtual Host Configs
    ##

    server {
        listen 80;

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://127.0.0.1:8080;
        }

    location /static {
        root /home/ubuntu/www/myproject/webapp;
    }

    }


    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

}
Run Code Online (Sandbox Code Playgroud)