在Apache上将多个WSGI应用程序作为不同的虚拟主机提供服务

时间:2017-06-14 20:40:48

标签: django apache virtualenv mod-wsgi wsgi

我有一台EC2 AWS服务器,我想在其上托管几个Django应用程序。这些应用程序中的每一个都有自己的URL。例如,

example1.com example2.com

单独使用example1.com。问题是让example2.com同时使用它。

当我访问example2.com时,出现错误:

DisallowedHost at /
Invalid HTTP_HOST header: 'example2.com'. You may need to add 'example2.com' to ALLOWED_HOSTS.
Request Method: GET
Request URL:    http://example2.com
Django Version: 1.9.13
Exception Type: DisallowedHost
Exception Value:    
Invalid HTTP_HOST header: 'example2.com'. You may need to add 'example2.com' to ALLOWED_HOSTS.
Exception Location: /var/www/vhosts/example1/example1-env/lib/python3.5/site-packages/django/http/request.py in get_host, line 109
Python Executable:  /usr/bin/python3
Python Version: 3.5.1
Python Path:    
['/usr/lib64/python3.5',
 '/usr/lib64/python3.5/plat-linux',
 '/usr/lib64/python3.5/lib-dynload',
 '/usr/local/lib64/python3.5/site-packages',
 '/usr/local/lib/python3.5/site-packages',
 '/usr/lib64/python3.5/dist-packages',
 '/usr/lib/python3.5/dist-packages',
 '/var/www/vhosts/example1/',
 '/var/www/vhosts/example1/example1-env/lib/python3.5/site-packages']
Server time:    Wed, 14 Jun 2017 20:31:27 +0000

正如您所看到的,Apache正在尝试使用example1.com服务example2.com的虚拟环境。我怎么能纠正这个?每一个都应该有自己的virtualenv。

这是Apache配置文件:

    <VirtualHost *:80>
        # This is name based virtual hosting. So place an appropriate server name
        #   here. Example: django.devsrv.local
        ServerName  example1.com

        WSGIDaemonProcess example1 python-home=/var/www/vhosts/example1/example1-env
        WSGIProcessGroup %{GLOBAL}

        # Insert the full path to the wsgi.py-file here
        WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py

        <Directory /var/www/vhosts/example1/>
            Require all granted
        </Directory>

        Alias /static/ /var/www/vhosts/example1/static/

        <Directory /var/www/vhosts/example1/static/>
        Order deny,allow
        Allow from all
        </Directory>

        Alias /media/ /var/www/vhosts/example1/media/
        <Directory /var/www/vhosts/example1/media/>
        Order deny,allow
        Allow from all
        </Directory>

    </VirtualHost>

    <VirtualHost *:80>
        # This is name based virtual hosting. So place an appropriate server name
        #   here. Example: django.devsrv.local
        ServerName  example2.com
        WSGIDaemonProcess example2 python-home=/var/www/vhosts/example2/example2-env
        WSGIProcessGroup %{GLOBAL}

        # Insert the full path to the wsgi.py-file here
        WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py

        <Directory /var/www/vhosts/example2/>
            Require all granted
        </Directory>

        Alias /static/ /var/www/vhosts/example2/static/

        <Directory /var/www/vhosts/example2/static/>
        Order deny,allow
        Allow from all
        </Directory>

        Alias /media/ /var/www/vhosts/example2/media/
        <Directory /var/www/vhosts/example2/media/>
        Order deny,allow
        Allow from all
        </Directory>
  </VirtualHost>

修改 在评论中阅读了一些建议后,我就来了。这仍然不起作用。

ServerName  example1.com

WSGIDaemonProcess example1 display-name=%{GROUP} python-path=/var/www/vhosts/example1/ python-home=/var/www/vhosts/example1/example1-env/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example1

# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py process-group=example1

...

ServerName  example2.com

WSGIDaemonProcess example2 display-name=%{GROUP} python-home=/var/www/vhosts/example2/example2-env/ python-path=/var/www/vhosts/example2/
WSGIApplicationGroup %{GLOBAL}
WSGIProcessGroup example2

# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py process-group=example2

1 个答案:

答案 0 :(得分:0)

以下配置对我有用。简而言之,它使用各自的虚拟环境在example1.comexample2.com提供两个不同的Django应用程序。

正如您所看到的,插入ServerAlias AND ServerName完全不同,以及社区建议的其他一些更正。

Apache配置:

<IfModule !wsgi_module>
LoadModule wsgi_module modules/mod_wsgi.so
</IfModule>

<VirtualHost *:80>

    ServerName  www.example1.com
    ServerAlias example1.com

    WSGIDaemonProcess example1 display-name=%{GROUP} python-path=/var/www/vhosts/example1/ python-home=/var/www/vhosts/example1/example1-env/
    WSGIApplicationGroup %{GLOBAL}
    WSGIProcessGroup example1


    # Insert the full path to the wsgi.py-file here
    WSGIScriptAlias / /var/www/vhosts/example1/example1/wsgi.py process-group=example1

    <Directory /var/www/vhosts/example1/>
        Require all granted
    </Directory>

    Alias /static/ /var/www/vhosts/example1/static/

    <Directory /var/www/vhosts/example1/static/>
    Order deny,allow
    Allow from all
    </Directory>

    Alias /media/ /var/www/vhosts/example1/media/
    <Directory /var/www/vhosts/example1/media/>
    Order deny,allow
    Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>

    ServerName  www.example2.com
    ServerAlias example2.com

    WSGIDaemonProcess example2 display-name=%{GROUP} python-home=/var/www/vhosts/example2/example2-env/ python-path=/var/www/vhosts/example2/
    WSGIApplicationGroup %{GLOBAL}
    WSGIProcessGroup example2

    # Insert the full path to the wsgi.py-file here
    WSGIScriptAlias / /var/www/vhosts/example2/example2/wsgi.py process-group=example2

    <Directory /var/www/vhosts/example2/>
        Require all granted
    </Directory>

    Alias /static/ /var/www/vhosts/example2/static/

    <Directory /var/www/vhosts/example2/static/>
    Order deny,allow
    Allow from all
    </Directory>

    Alias /media/ /var/www/vhosts/example2/media/
    <Directory /var/www/vhosts/example2/media/>

</VirtualHost>