简体   繁体   English

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

[英]Serving Multiple WSGI Applications As Different Virtual Hosts on Apache

I have an EC2 AWS server on which I would like to host a couple of Django applications. 我有一台EC2 AWS服务器,我想在其上托管几个Django应用程序。 Each of these apps has its own URL. 每个应用程序都有自己的URL。 For instance, 例如,

example1.com example2.com example1.com example2.com

By itself, example1.com works. example1.com The problem is getting example2.com to work with it at the same time. 问题是让example2.com同时使用它。

When I visit example2.com , I get an error: 当我访问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

As you can see, somehow Apache is trying to use the virtual environment of example1.com when it serves example2.com . 如您所见,Apache在为example2.com提供服务时以某种方式尝试使用example1.com的虚拟环境。 How could I correct that? 我该如何纠正? Each one should be served with its own virtualenv. 每个人都应该拥有自己的virtualenv。

Here is the Apache configuration file: 这是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>

Edit: Having read some suggestions in the comments, I have come to this. 编辑:阅读了评论中的一些建议后,我来了。 This still does not work. 这仍然行不通。

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

The following configuration worked for me. 以下配置对我有用。 In short, it serves two different Django applications at example1.com and example2.com using their respective virtual environments. 简而言之,它使用各自的虚拟环境在example1.comexample2.com为两个不同的Django应用程序提供服务。

As you can see, inserting the ServerAlias AND ServerName made all the difference, alongside a couple of other corrections by suggested by the community. 如您所见,插入ServerAlias AND ServerName带来很大的不同,以及社区建议的其他一些更正。

Apache configuration: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM