V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tanteng
V2EX  ›  Flask

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

  •  1
     
  •   tanteng ·
    tanteng · 2015-12-11 01:09:04 +08:00 · 3068 次点击
    这是一个创建于 3061 天前的主题,其中的信息可能已经有所发展或是发生改变。

    已经配置好可以在 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 端口怎么去掉!!求助!

    第 1 条附言  ·  2015-12-11 22:49:03 +08:00
    10 条回复    2015-12-14 10:50:06 +08:00
    phantomer
        1
    phantomer  
       2015-12-11 11:01:42 +08:00   ❤️ 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
       2015-12-11 13:38:29 +08:00
    ProxyRequests Off
    ProxyPass / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/
    你多加了这几个,我回去试试
    phantomer
        3
    phantomer  
       2015-12-11 16:56:15 +08:00   ❤️ 1
    @tanteng 对。![]( )我 nginx 这样反代的。 apache 我查了一下应该是那样反代的。
    tanteng
        4
    tanteng  
    OP
       2015-12-11 17:19:12 +08:00
    @phantomer nginx 这样写 Flask 文档上介绍了, apache 那样写回去验证。我 Mac 上装的是 MAMP 的环境,本来是 PHP 的集成环境,它的 apache 自动带了 wsgi 需要的模块并且默认开启,所以我启动这个 apache 来运行 Flask 了。
    phantomer
        5
    phantomer  
       2015-12-11 17:36:03 +08:00
    @tanteng 那你试试吧。成功了告诉我一声。
    julyclyde
        6
    julyclyde  
       2015-12-11 21:23:05 +08:00
    原 po 这种做法其实和 apache 就没啥关系啊,并不是“在 apache 上”运行,而是独立运行的
    tanteng
        7
    tanteng  
    OP
       2015-12-11 21:43:47 +08:00
    @phantomer

    Not Found

    The requested URL / was not found on this server.

    还是要加端口 8000 访问
    tanteng
        8
    tanteng  
    OP
       2015-12-11 22:03:26 +08:00
    @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
       2015-12-11 22:07:43 +08:00
    <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  
       2015-12-14 10:50:06 +08:00
    @tanteng 对,必须 80 ,我擦,我光是看下面,没注意那边你写的还是 8000
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   957 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:24 · PVG 03:24 · LAX 12:24 · JFK 15:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.