Flask app on non-root url of website with Apache2

半腔热情 提交于 2020-12-12 07:44:06

问题


I have a Flask app that I would like to host on a subdomain / non-root url of our lab website. For example, I want mylab.com/portal to lead to the flask app. I followed numerous guides but I keep getting a 404 error.

My Directory structure:

/var/www/
    -html/
         -Stuff for mylab.com
    -FlaskApp/
         -FlaskApp.wsgi
         -FlaskApp/
              -__init__.py
              -static/
              -templates/
              -...

FlaskApp.wsgi:

#!/usr/bin/python3

activate_this = '/home/cogsci-cnddcollab/FlaskApp/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

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

from FlaskApp import app as application

/etc/apache2/sites-available/000-default.conf

WSGIRestrictStdout Off
WSGIScriptReloading On

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName mylab.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    Redirect permanent / https://mylab.com/

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf

    <Directory "/var/www/html">
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
    </Directory>



    ## FlaskApp
    WSGIDaemonProcess FlaskApp_wsgi user=cogsci-cnddcollab group="domain users" threads=5
    WSGIScriptAlias /portal /var/www/FlaskApp/FlaskApp.wsgi
    <Directory /var/www/FlaskApp/FlaskApp/>
        WSGIProcessGroup FlaskApp_wsgi
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
    </Directory>

    Alias /portal/static /var/www/FlaskApp/FlaskApp/static
    <Directory /var/www/FlaskApp/FlaskApp/static>
        Require all granted
    </Directory>

    Alias /portal/templates /var/www/FlaskApp/FlaskApp/templates
    <Directory /var/www/FlaskApp/FlaskApp/templates>
        Require all granted
    </Directory>

</VirtualHost>

Now, when I restart the apache2 service and go to mylab.com/portal, I am hit with a 404 error. I have added APPLICATION_ROOT=/portal to my config.py file.

Any help will be greatly appreciated.

来源:https://stackoverflow.com/questions/50748048/flask-app-on-non-root-url-of-website-with-apache2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!