在Ubuntu中运行多个独立的Flask应用 [英] Run multiple independent Flask apps in Ubuntu

查看:143
本文介绍了在Ubuntu中运行多个独立的Flask应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apache在单独的虚拟目录中运行两个或多个Flask应用程序,例如/var/www/myapps/app1 http://localhost/site1/的http://localhost/site2 var/www/myapps/app2 .每个应用程序在 env 目录下都有自己的虚拟环境.

I'm trying to run two or more Flask applications in separate virtual directories with Apache, like http://localhost/site1 for /var/www/myapps/app1 and http://localhost/site2 for /var/www/myapps/app2. Each app has its own virtual environment under an env directory.

我首先使用Apache2(v2.4.7)重新安装了Ubuntu 14.04,然后使用sudo a2dissite 000-default删除了默认站点配置,并为两个应用程序添加了配置.

I started with a fresh install of Ubuntu 14.04 with Apache2 (v2.4.7), removed the default site configuration with sudo a2dissite 000-default and added configuration for my two apps.

这是位于/etc/apache2/sites-available/app1.conf 的app1的conf文件. app2的配置相同,将app2替换为app1(将site2替换为site1.)

Here's the conf file for app1 at /etc/apache2/sites-available/app1.conf. The cofiguration for app2 is identical, replacing app2 for app1 (and site2 for site1.)

<VirtualHost *:80>
    ServerName localhost
    WSGIProcessGroup site1
    WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-path=/var/www/myapps/app1:/var/www/myapps/app1/env/lib/python2.7/site-packages
    WSGIScriptAlias /site1 /var/www/myapps/app1/application.wsgi
    WSGIScriptReloading On
    <Directory /var/www/mysites/app1>
        WSGIApplicationGroup site1
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

然后,我使用sudo a2ensite app1(和app2)启用了每个站点,然后使用sudo apache2ctl restart重新启动了服务器.

Then I enabled each site with sudo a2ensite app1 (and app2) then restarted the server with sudo apache2ctl restart.

每个应用程序的根目录中都有以下application.wsgi文件:

Each of these apps has the following application.wsgi file in the root dir:

# put a copy of this file in the root dir of each app instance
import os, sys
# activate the virtual environment
app_dir = os.path.dirname(__file__)
# removed next two lines after a comment left below
# activate_this = os.path.join(app_dir, 'env', 'bin', 'activate_this.py')
# execfile(activate_this, dict(__file__=activate_this))
sys.path.insert(0, app_dir)
from myapp import app as application

发生的情况是,其中一个站点运行良好,当我点击它时会以正确的页面进行响应.另一个给了我一个Apache 404页面. conf文件中没有错别字.

What happens is that one of the sites runs fine, responding with the correct page when I hit it. The other gives me an Apache 404 page. There are no typos in the conf files.

看来,这些应用程序的配置正在互相干扰,一个正在取胜".我已经花了很多时间来调整配置,但是唯一可行的方法是将 localhost2 添加到我的 hosts 文件并将ServerName更改为localhost2在其中一个应用程序配置中,对于我而言,这是不希望的.有人可以指出我配置Apache的正确方法吗?还是我走错路了?

It seems the configurations for the apps are clobbering each other and one is "winning." I've spent a lot of time tweaking the configurations but the only way I could make it work was if I added localhost2 to my hosts file and changed the ServerName to localhost2 in one of the app configs, which isn't desirable in my case. Can someone point me to the correct way to configure Apache? Or am I going the wrong way about this?

理想情况下,我希望配置文件不在乎使用哪个主机名来访问它们,可能会成为在负载均衡器后面运行的该服务器的多个副本.

Ideally I'd want the configuration files to not care which host name was used to reach them, probably becoming multiple copies of this server running behind a load balancer.

推荐答案

我在此上花费了更多时间,并且,如果我开始更好地了解Apache术语和配置,则无法将虚拟主机用于此目的. VirtualHost部分旨在提供不同的主机名(多个域或子域).

I spent more time on this and, if I'm starting to understand Apache terminology and configuration a little better, I cannot use virtual hosts for this purpose. VirtualHost sections are intended for serving different host names (multiple domains or subdomains.)

为了将并行应用程序配置为子目录,我可以改用Directory部分.我也没有意识到配置文件中的某些WSGI*指令可能会出现多次.这些新知识使我能够生成以下符合我想要的配置文件.因此,除了为每个应用程序启用一个Apache站点之外,我还可以启用其中配置了目录的单个站点.

For configuring parallel applications as subdirs I could have used Directory sections instead. I also didn't realize some of the WSGI* directives in the config file could appear more than once. This new knowledge allowed me to produce the following single config file that does what I wanted. So instead of enabling one Apache site for each app, I could enable a single site with the directories configured in it.

# this goes in /etc/apache2/sites-available/
<VirtualHost *:80>
    ServerName localhost

    # logs configuration
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app1:/var/www/myapps/app1/env/lib/python2.7/site-packages
    WSGIScriptAlias /site1 /var/www/myapps/app1/application.wsgi
    <Directory /var/www/myapps/app1>
        WSGIApplicationGroup site1
        WSGIProcessGroup site1
        Order deny,allow
        Allow from all
    </Directory>

    WSGIDaemonProcess site2 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app2:/var/www/myapps/app2/env/lib/python2.7/site-packages
    WSGIScriptAlias /site2 /var/www/myapps/app2/application.wsgi
    <Directory /var/www/myapps/app2>
        WSGIApplicationGroup site2
        WSGIProcessGroup site2
        Order deny,allow
        Allow from all
    </Directory>

</VirtualHost>

我后来遵循了 Graham Dumpleton 的建议,并从应用程序中删除了activate_this内容. wsgi 并将WSGIDaemonProcess指令行更改为:

I later followed Graham Dumpleton's suggestion and removed the activate_this stuff from application.wsgi and changed the WSGIDaemonProcess directive lines to:

WSGIDaemonProcess site1 user=myserviceuser group=myserviceuser threads=5 python-home=/var/www/myapps/app1/env

这篇关于在Ubuntu中运行多个独立的Flask应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆