Flask App路由不适用于mod_wsgi和虚拟主机

时间:2019-01-03 06:00:31

标签: apache flask virtualhost

我是编程新手,但是可以设法调用下面的两个flask应用程序,以在Apache 2.4的VPS服务器中托管的mod_wsgi下工作。

目录结构和文件:

    /var/www/html/firstapp
                | -myapp.wsgi
                | -app.py
    /var/www/html/secondapp
                | -myapp.wsgi
                | -app.py
                | -templates
                    |- sample01.html
                    |-sample02.html

现在,我很难弄清楚应该如何在secondapp目录下的app.py中管理路由,其中​​包括带有post方法的输入表单。

第二个应用目录中的

app.py:

    from flask import Flask, render_template, request
    app = Flask(__name__)
    @app.route('/')
    def index():
    message="Good Morning!"
    return render_template("sample01.html", message=message)

    @app.route('/add',methods=['POST'])
    def add():
    if request.method =='POST':
        result = request.form['new_num']
    return render_template("sample02.html", result=result)

    if __name__ == '__main__'
        app.run()

sample01.html:

    <body>{{message}}
    <form action="/add" method="post">
      <p>Input number:</p>
      <input type="text" name="new_num" >
      <input type="submit" value="Do">
    </form>
    </body>

sample02.html:

    <body>
    <p>Your input is as below. </p>
    {{result}}
    </body>

VirtualHost:

    LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so
    <VirtualHost *:80>
    ServerName abc.com

    DocumentRoot /var/www/html/firstapp
    WSGIDaemonProcess first
    WSGIScriptAlias /firstapp /var/www/html/firstapp/myapp.wsgi
    <Location /firstapp>
    WSGIProcessGroup first
    </Location>
    <Directory "/var/www/html/firstapp">
    Require all granted
    </Directory>

    DocumentRoot /var/www/html/secondapp

    WSGIDaemonProcess second
    WSGIScriptAlias /secondapp /var/www/html/secondapp/myapp.wsgi
    <Location /secondapp>
    WSGIProcessGroup second
    </Location>
    <Directory "/var/www/html/secondapp/">
    Options Includes ExecCGI FollowSymLinks
    Require all granted
    </Directory>
    </VirtualHost>

使用这些设置,当我按sample01.html中的“提交”按钮时,它会指向“ abc.com/add”的网址,而不是“ abc.com/secondapp/add”的网址。我猜我没有正确设置文档路径,但是我不知道该怎么做。

任何建议都非常感谢。

0 个答案:

没有答案