为独特域托管多个Flask应用

时间:2017-11-03 22:30:28

标签: python apache flask dns virtualhost

我有两个独特的域名(例如domain1.comdomain2.com)。我正在尝试使用Flask在一个linode服务器(例如12.34.567.890)上托管两个独特的网页(通过两个独立的烧瓶应用程序)。

目前,domain1.com有效,但domain2.com为我提供了Apache2默认页面。

这是我到目前为止所拥有的:

等\ apache2的\ ports.conf:

Listen 80
Listen 8080

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


等\ apache2的\站点可用\ 000-default.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:8080>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


等\ apache2的\站点可用\ FlaskApp.conf:

<VirtualHost *:80>
            ServerName domain1.com
            ServerAdmin admin@email.com
            WSGIScriptAlias / /var/www/AppOne/flaskapp.wsgi
            <Directory /var/www/AppOne/FlaskApp/>
                    Order allow,deny
                    Allow from all
            </Directory>
            Alias /static /var/www/AppOne/FlaskApp/static
            <Directory /var/www/AppOne/FlaskApp/static/>
                    Order allow,deny
                    Allow from all
            </Directory>
            ErrorLog ${APACHE_LOG_DIR}/error.log
            LogLevel warn
            CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:8080>
            ServerName domain2.com
            ServerAdmin admin@email.com
            WSGIScriptAlias / /var/www/AppTwo/flaskapp.wsgi
            <Directory /var/www/AppTwo/FlaskApp/>
                    Order allow,deny
                    Allow from all
            </Directory>
            Alias /static /var/www/AppTwo/FlaskApp/static
            <Directory /var/www/AppTwo/FlaskApp/static/>
                    Order allow,deny
                    Allow from all
            </Directory>
            ErrorLog ${APACHE_LOG_DIR}/error.log
            LogLevel warn
            CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


每个应用的__init__.py是相同的(但在不同的目录中):

from flask import Flask, render_template, flash, request, url_for, redirect, session
from content_management import Content
#...etc...
import gc

TOPIC_DICT = Content()

app = Flask(__name__)

@app.route('/')
def homepage():
    return render_template("home.html")

if __name__ == "__main__":
    app.run()


\ var \ www \目录如下所示:

\var\www
|
└─── AppOne
|    |    flaskapp.wsgi
|    |
|    └─── FlaskApp
|         |  __init__.py
|         |
|         └─── static
|         └─── templates
|         |        home.html
|         |
|         └─── venv
|
└─── AppTwo                  #same as AppOne
     |    flaskapp.wsgi
     |
     └─── FlaskApp
          |  __init__.py
          |
          └─── static
          └─── templates
          |        home.html
          |
          └─── venv

很抱歉,如果我有点冗长,但任何指导都会有所帮助!

1 个答案:

答案 0 :(得分:0)

无需侦听端口8080.AppTwo的WSGI文件未指向正确的目录。

ports.conf:

Listen 80

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>


000-default.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


FlaskApp.conf:

<VirtualHost *:80>
        ServerName domain1.com
        ServerAdmin admin@email.com
        WSGIScriptAlias / /var/www/AppOne/flaskapp.wsgi
        <Directory /var/www/AppOne/FlaskApp/>
                Order allow,deny
                Allow from all
        </Directory>
        Alias /static /var/www/AppOne/FlaskApp/static
        <Directory /var/www/AppOne/FlaskApp/static/>
                Order allow,deny
                Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
        ServerName domain2.com
        ServerAdmin admin@email.com
        WSGIScriptAlias / /var/www/AppTwo/flaskapp.wsgi
        <Directory /var/www/AppTwo/FlaskApp/>
                Order allow,deny
                Allow from all
        </Directory>
        Alias /static /var/www/AppTwo/FlaskApp/static
        <Directory /var/www/AppTwo/FlaskApp/static/>
                Order allow,deny
                Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


AppOne / flaskapp.wsgi:

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/AppOne/")

from FlaskApp import app as application
application.secret_key = 'PasswordGoesHere'


AppTwo / flaskapp.wsgi:

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/AppTwo/")

from FlaskApp import app as application
application.secret_key = 'PasswordGoesHere'