tanteng
V2EX  ›  Flask

关于 Flask 在 Apache 运行的问题,求助!

  •  1
     
  •   tanteng ·
    tanteng · Dec 11, 2015 · 3749 views
    This topic created in 3831 days ago, the information mentioned may be changed or developed.

    已经配置好可以在 Apache 上通过 wsgi 运行,但是必须要在网址上加端口如 8080 ,改成 80 无法启动

    我的 vhost 配置如下:

    <VirtualHost *:8000>
    ServerName www.learn-flask.com

    WSGIDaemonProcess learn-flask threads=5
    WSGIScriptAlias / /Users/tanteng/pythonflask/index_loacl.wsgi
    
    <Directory /Users/tanteng/pythonflask>
        WSGIProcessGroup flask
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
    

    </VirtualHost>

    wsgi 文件如下:

    import sys
    sys.path.insert(0, '/Users/tanteng/pythonflask/')

    from run import app as application

    入口 py 文件如下:

    from website import app
    
    if __name__ == '__main__':
        app.run(debug=True,host='0.0.0.0',port=8000)
    

    我现在只能这样设置才能正常运行,但是网址必须是 http://www.learn-flask.com:8000/这样的,我想去掉这个 8000 端口怎么去掉!!求助!

    Supplement 1  ·  Dec 11, 2015
    10 replies    2015-12-14 10:50:06 +08:00
    phantomer
        1
    phantomer  
       Dec 11, 2015   ❤️ 1
    可以用 apache 反代出来。你把 flask 起在 127.0.0.1:8000 ,然后用 apache 反代这个。
    <VirtualHost *:8000>
    ServerName www.learn-flask.com

    WSGIDaemonProcess learn-flask threads=5
    WSGIScriptAlias / /Users/tanteng/pythonflask/index_loacl.wsgi
    ProxyRequests Off
    <Directory /Users/tanteng/pythonflask>
    WSGIProcessGroup flask
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
    </Directory>
    ProxyPass / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/
    </VirtualHost>

    我自己的博客是 nginx 反代 ghost 出来的。 apache 的配置文件我查了查,应该这么写吧。如果错了,勿喷。
    tanteng
        2
    tanteng  
    OP
       Dec 11, 2015
    ProxyRequests Off
    ProxyPass / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/
    你多加了这几个,我回去试试
    phantomer
        3
    phantomer  
       Dec 11, 2015   ❤️ 1
    @tanteng 对。![]( )我 nginx 这样反代的。 apache 我查了一下应该是那样反代的。
    tanteng
        4
    tanteng  
    OP
       Dec 11, 2015
    @phantomer nginx 这样写 Flask 文档上介绍了, apache 那样写回去验证。我 Mac 上装的是 MAMP 的环境,本来是 PHP 的集成环境,它的 apache 自动带了 wsgi 需要的模块并且默认开启,所以我启动这个 apache 来运行 Flask 了。
    phantomer
        5
    phantomer  
       Dec 11, 2015
    @tanteng 那你试试吧。成功了告诉我一声。
    julyclyde
        6
    julyclyde  
       Dec 11, 2015
    原 po 这种做法其实和 apache 就没啥关系啊,并不是“在 apache 上”运行,而是独立运行的
    tanteng
        7
    tanteng  
    OP
       Dec 11, 2015
    @phantomer

    Not Found

    The requested URL / was not found on this server.

    还是要加端口 8000 访问
    tanteng
        8
    tanteng  
    OP
       Dec 11, 2015
    @phantomer

    <VirtualHost *:80>
    ServerName www.learn-flask.com

    WSGIDaemonProcess learn-flask threads=5
    WSGIScriptAlias / /Users/tanteng/pythonflask/index_loacl.wsgi
    ProxyRequests Off

    <Directory /Users/tanteng/pythonflask>
    WSGIProcessGroup flask
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
    </Directory>
    ProxyPass / http://127.0.0.1:8080
    ProxyPassReverse / http://127.0.0.1:8080
    </VirtualHost>


    这样就 OK 了。其中<VirtualHost *:80>这里必须是 80 端口,然后后面 ProxyPass 才是设置的端口。非常感谢提供帮助!

    其实我也不知道具体的原理,总之这样就可以了。
    tanteng
        9
    tanteng  
    OP
       Dec 11, 2015
    <VirtualHost *:80>
    ServerName www.learn-flask.com

    WSGIDaemonProcess learn-flask threads=5
    WSGIScriptAlias / /Users/tanteng/pythonflask/index_loacl.wsgi
    ProxyRequests Off

    <Directory /Users/tanteng/pythonflask>
    WSGIProcessGroup flask
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
    </Directory>
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
    </VirtualHost>

    还有一个地方 ProxyPass 和下面那行必须要加'/',这个你给的例子是对的。以上就是完整正确版。
    phantomer
        10
    phantomer  
       Dec 14, 2015
    @tanteng 对,必须 80 ,我擦,我光是看下面,没注意那边你写的还是 8000
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2823 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 04:31 · PVG 12:31 · LAX 21:31 · JFK 00:31
    ♥ Do have faith in what you're doing.