当我尝试将Flask应用程序部署到我的LAMP服务器时,我从flipflop收到错误,这是一个FastCGI / WSGI网关,它使我的应用程序能够说出FastCGI协议。

〜/最小/ run.py

from flask import Flask
from flipflop import WSGIServer

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'hello, world'

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

Apache配置文件的相关部分,即/etc/httpd/conf/httpd.conf

<VirtualHost *:80>
    ScriptAlias / /home/apps/minimal/run.py
    ErrorLog /var/log/httpd/error_log
</VirtualHost>

Apache / 2.2.15的错误报告:

[apps@kernod0 ~]$ sudo head -n 20 /var/log/httpd/error_log
[sudo] password for apps: 
[Wed Aug 16 16:39:16 2017] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Aug 16 16:39:16 2017] [notice] Digest: generating secret for digest authentication ...
[Wed Aug 16 16:39:16 2017] [notice] Digest: done
[Wed Aug 16 16:39:16 2017] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 configured -- resuming normal operations
[Wed Aug 16 16:39:16 2017] [error] [client 100.116.224.219] Traceback (most recent call last):
[Wed Aug 16 16:39:16 2017] [error] [client 100.116.224.219]   File "/home/apps/minimal/run.py", line 12, in <module>
[Wed Aug 16 16:39:16 2017] [error] [client 100.116.224.219]     WSGIServer(app).run()
[Wed Aug 16 16:39:16 2017] [error] [client 100.116.224.219]   File "/home/apps/minimal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
[Wed Aug 16 16:39:16 2017] [error] [client 100.116.224.219]     sock.getpeername()
[Wed Aug 16 16:39:16 2017] [error] [client 100.116.224.219] socket.error: [Errno 88] Socket operation on non-socket
[Wed Aug 16 16:39:16 2017] [error] [client 100.116.224.219] Premature end of script headers: run.py
[Wed Aug 16 16:39:17 2017] [error] [client 100.116.226.253] Traceback (most recent call last):
[Wed Aug 16 16:39:17 2017] [error] [client 100.116.226.253]   File "/home/apps/minimal/run.py", line 12, in <module>
[Wed Aug 16 16:39:17 2017] [error] [client 100.116.226.253]     WSGIServer(app).run()
[Wed Aug 16 16:39:17 2017] [error] [client 100.116.226.253]   File "/home/apps/minimal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
[Wed Aug 16 16:39:17 2017] [error] [client 100.116.226.253]     sock.getpeername()
[Wed Aug 16 16:39:17 2017] [error] [client 100.116.226.253] socket.error: [Errno 88] Socket operation on non-socket
[Wed Aug 16 16:39:17 2017] [error] [client 100.116.226.253] Premature end of script headers: run.py
[Wed Aug 16 16:39:17 2017] [error] [client 100.116.226.205] Traceback (most recent call last):
[Wed Aug 16 16:39:17 2017] [error] [client 100.116.226.205]   File "/home/apps/minimal/run.py", line 12, in <module>

此外,即使不使用flipflop ,它仍然不起作用:

〜/最小/ run.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'hello, world'

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

错误输出:

[apps@kernod0 ~]$ sudo cat /var/log/httpd/error_log
[Wed Aug 16 20:47:24 2017] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Aug 16 20:47:24 2017] [notice] Digest: generating secret for digest authentication ...
[Wed Aug 16 20:47:24 2017] [notice] Digest: done
[Wed Aug 16 20:47:24 2017] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 configured -- resuming normal operations
[Wed Aug 16 20:47:33 2017] [error] [client 100.116.226.182]  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] Traceback (most recent call last):
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "/home/apps/minimal/run.py", line 11, in <module>
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]     
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] app.run()
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "/home/apps/minimal/flask/lib/python2.6/site-packages/flask/app.py", line 841, in run
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]     
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] run_simple(host, port, self, **options)
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "/home/apps/minimal/flask/lib/python2.6/site-packages/werkzeug/serving.py", line 739, in run_simple
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]     
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] inner()
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "/home/apps/minimal/flask/lib/python2.6/site-packages/werkzeug/serving.py", line 699, in inner
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]     
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] fd=fd)
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "/home/apps/minimal/flask/lib/python2.6/site-packages/werkzeug/serving.py", line 593, in make_server
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]     
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] passthrough_errors, ssl_context, fd=fd)
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "/home/apps/minimal/flask/lib/python2.6/site-packages/werkzeug/serving.py", line 504, in __init__
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]     
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] HTTPServer.__init__(self, (host, int(port)), handler)
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "/usr/lib64/python2.6/SocketServer.py", line 412, in __init__
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]     
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] self.server_bind()
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "/usr/lib64/python2.6/BaseHTTPServer.py", line 108, in server_bind
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]     
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] SocketServer.TCPServer.server_bind(self)
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "/usr/lib64/python2.6/SocketServer.py", line 423, in server_bind
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]     
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] self.socket.bind(self.server_address)
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190]   File "<string>", line 1, in bind
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] socket
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] .
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] error
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] : 
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] [Errno 98] Address already in use
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] 
[Wed Aug 16 20:47:37 2017] [error] [client 100.116.226.190] Premature end of script headers: run.py
[Wed Aug 16 20:48:33 2017] [warn] [client 100.116.226.182] Timeout waiting for output from CGI script /home/apps/minimal/run.py
[Wed Aug 16 20:48:33 2017] [error] [client 100.116.226.182] Script timed out before returning headers: run.py
[Wed Aug 16 20:49:33 2017] [warn] [client 100.116.226.182] Timeout waiting for output from CGI script /home/apps/minimal/run.py

#1楼 票数:5

我已经设法运行你的例子,但是有一些调整使它工作。
您可能需要更改系统上的路径,因为从您的日志中可能看起来您正在使用运行python2.6系统和仍使用httpd文件的较旧的apache版本。
如果可能,我会建议您升级您的环境。

这是一个循序渐进的工作解决方案:

1.安装virtualenvwrapper

sudo -EH pip2 install virtualenvwrapper

2.Activte它:

source /usr/local/bin/virtualenvwrapper.sh

3.创建虚拟环境:

mkvirtualenv minimal

4.安装flaskflup

pip install -U flask flup

flipflop对我不起作用,但因为它是README状态

这个模块是一个简化的flup分支,由Allan Saddi编写。 它只有原始模块的FastCGI部分。

所以你可以安全地使用它。

5. apache2

sudo apt-get install apache2

6.安装libapache2-mod-fastcgi

sudo apt-get install libapache2-mod-fastcgi

7.创建/var/www/minimal/run.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'hello, world'

8.创建/var/www/minimal/minimal.fcgi

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)

activate_this = '/home/some_user/.virtualenvs/minimal/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

sys.path.insert(0,"/var/www/minimal/")

from flup.server.fcgi import WSGIServer
from run import app

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

9.Make minimal.fcgi可执行文件:

sudo chmod +x minimal.fcgi

10.Create minimal.conf文件( /etc/apache2/sites-available我的服务器上):

FastCgiServer /var/www/minimal/minimal.fcgi -idle-timeout 300 -processes 5

<VirtualHost *:80>
    ServerName YOUR_IP_ADDRESS
    DocumentRoot /var/www/minimal/

    AddHandler fastcgi-script fcgi
    ScriptAlias / /var/www/minimal/minimal.fcgi/

    <Location />
        SetHandler fastcgi-script
    </Location>
</VirtualHost>

11.启用新网站:

sudo a2ensite minimal.conf

12.更改/var/www/所有权到www-data用户:

sudo chown -R www-data:www-data /var/www/

13.Restart apache2

sudo /etc/init.d/apache2 restart 

瞧! :)

如果您访问服务器地址,您应该在浏览器中看到hello, world 在此输入图像描述

另外,当重新启动apache您可以在apache的error.log查看FastCGI

[Thu Aug 24 16:33:09.354544 2017] [mpm_event:notice] [pid 17375:tid 139752788969344] AH00491: caught SIGTERM, shutting down
[Thu Aug 24 16:33:10.414829 2017] [mpm_event:notice] [pid 17548:tid 139700962228096] AH00489: Apache/2.4.18 (Ubuntu) mod_fastcgi/mod_fastcgi-SNAP-0910052141 configured -- resuming normal operations
[Thu Aug 24 16:33:10.415033 2017] [core:notice] [pid 17548:tid 139700962228096] AH00094: Command line: '/usr/sbin/apache2'
[Thu Aug 24 16:33:10.415651 2017] [:notice] [pid 17551:tid 139700962228096] FastCGI: process manager initialized (pid 17551)
[Thu Aug 24 16:33:10.416135 2017] [:warn] [pid 17551:tid 139700962228096] FastCGI: server "/var/www/minimal/minimal.fcgi" started (pid 17556)
[Thu Aug 24 16:33:11.416571 2017] [:warn] [pid 17551:tid 139700962228096] FastCGI: server "/var/www/minimal/minimal.fcgi" started (pid 17618)
[Thu Aug 24 16:33:12.422058 2017] [:warn] [pid 17551:tid 139700962228096] FastCGI: server "/var/www/minimal/minimal.fcgi" started (pid 17643)
[Thu Aug 24 16:33:13.422763 2017] [:warn] [pid 17551:tid 139700962228096] FastCGI: server "/var/www/minimal/minimal.fcgi" started (pid 17651)
[Thu Aug 24 16:33:14.423536 2017] [:warn] [pid 17551:tid 139700962228096] FastCGI: server "/var/www/minimal/minimal.fcgi" started (pid 17659)

#2楼 票数:1

您无法从终端运行fastcgi脚本。 该脚本应该由Apache执行。 通常,您在Apache配置文件的ScriptAlias指令中配置它。

#3楼 票数:1

一般来说,你应该使用mod_fastcgi和配置simillar来:

<VirtualHost *:8091>
  ServerName helloworld.local
  DocumentRoot /home/fe/work/flipflop
  FastCgiServer /home/fe/work/flipflop/run.py
  ScriptAlias / /home/fe/work/flipflop/run.py
    <Location />
      Options none
    </Location>
</VirtualHost>

所以它会使你的脚本运行为FastCgi,但我不熟悉flipflop并且不能使它工作。

但是,如果您不限于flipflop您可以使用uwsgi运行您的应用程序, mod_wsgi使用Apache运行它(在Flask文档中阅读更多详细信息)或使用Flask-Script runserver命令在调试服务器中运行您的应用程序(请参阅Flask中的示例) -Script文档

#4楼 票数:1

首先,看起来你已经在端口5000上运行/监听了一些应用程序。你可能想要找到sudo sockstat |grep 5000 ,然后配置Apache,或者使用localhost:5000 sudo sockstat |grep 5000进程/服务。

其次,看起来您的虚拟主机未考虑/未完全配置。

  ask by nalzok translate from so

未解决问题?本站智能推荐:

1回复

通过网络浏览器运行快速CGI程序

我在C语言中创建了一个hello FastCGI prog 然后,我使用“ gcc -o hello1 hello1.c -lfcgi”编译了程序 这在我的主目录(在ubuntu中)中创建了“ hello1”可执行文件,当我运行该文件时,输出为: 我想从Firefox运行此
2回复

Apache上的Flask应用程序,CentOs抛出mod_fcgid:从FastCGI服务器读取数据时出错

我正在Apache和Mysql的CentOS 6.6上开发Flask应用程序。 它是从Flask Megatutorial修改而来的 。 我能够正常创建数据库,但是当我尝试从浏览器访问数据库时,我收到500个内部服务器错误,并且在error_log文件中显示以下错误: 这是在我将ru
1回复

Apache中的快速CGI:无法绑定unix域套接字

排除了琐碎的原因,如缺少写入文件的权限,我正在寻找原因。 文件fcgid.conf仅提供基本设置,我无法验证它尝试打开的端口。 我如何授予许可? 提示:我的系统可能阻塞了端口,但我没有发现端口没有提到。 在fcgid配置中。 这是Apache错误日志。 加载快速CGI脚本时:
1回复

php-fpm在启动时破坏了我的套接字权限

我已经在Fastent的Centos服务器上安装了带有FastCgi模块的Apache和PHP-fpm。 我使用套接字选项使用外部服务器配置FastCgi: 当我启动服务(httpd和php-fpm)时,请求helloWorld-test-page时出现错误500。 但是,如果我
1回复

FastCGI-套接字管道断开或由对等方重置连接(PHP5-FPM或php-fpm)

我遇到php-fpm的确定性问题。 每当我向Web服务器发送一个大约有100KB以上的大GET请求时, php-fpm中止套接字连接,从而破坏套接字管道。 Web服务器依次返回500 Internal Server Error。 Apache错误日志显示(32)损坏的管道: 我
1回复

由本地http服务器生成的自定义FCGI应用程序的套接字路径是什么? (即apache / mod_fcgid,lighttpd / mod_fastcgi等)

我正在研究用Objective-C / COCOA编写的fastcgi开发套件,该套件模仿AppKit / UIKit的行为和结构(应用程序释放,运行循环,事件队列等)。 我已经完成了关于fastcgi,libfcgi,使用libfcgi的fastcgi目标c实现的作业,可悲的是,要使其成为1
1回复

Apache上的Python flask和fast-cgi-500内部服务器错误(脚本头过早结束)

我想在apache产品服务器上设置我的flask应用程序。 我创建了以下.htaccess文件: 哪个应重定向到以下fcgi文件(设置为chmod 755): 作为回报,应启动以下烧瓶应用程序: 但是,当我尝试访问该网站时,显示内部服务器错误500。 并且以下行显示在
1回复

服务器正在发送格式错误的FCGI标头

我编写了一个非常简单的程序,从FastCGI服务器读取信息。 它所做的全部工作都接受文件描述符为0的套接字,并将从服务器接收的所有数据“推送”到raw.binary(1KB)中 。 我收到926个字节,但文件的长度为1014,因为缓冲区具有该大小。 我试图使用此 FastCGI规范手动