关于 Flask 在 Apache 运行的问题,求助! - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tanteng
V2EX    Flask

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

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

    已经配置好可以在 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     Solana     2959 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 13:38 PVG 21:38 LAX 06:38 JFK 09:38
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86