【问题标题】:"OSError: [Errno 88] Socket operation on non-socket" by flipflop触发器的“OSError:[Errno 88] 非套接字上的套接字操作”
【发布时间】:2018-01-24 00:43:19
【问题描述】:

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

~/minimal/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,还是不行:

~/minimal/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

【问题讨论】:

    标签: apache sockets flask fastcgi mod-fcgid


    【解决方案1】:

    我已成功运行您的示例,但需要进行一些调整才能使其正常运行。
    您可能需要更改系统上的路径,因为从您的日志看来,您正在使用运行 python2.6 的系统和仍然使用 httpd 文件的旧 apache 版本。
    如果可能的话,我会建议你升级你的环境。

    这是一个分步的工作解决方案:

    1.安装virtualenvwrapper:

    sudo -EH pip2 install virtualenvwrapper
    

    2.激活它:

    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.使minimal.fcgi可执行:

    sudo chmod +x minimal.fcgi
    

    10.创建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.重启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)
    

    【讨论】:

    • 感谢您写出如此详细的答案!顺便说一句,我使用的是 CentOS 6.8,它的包管理器 yum 依赖于 Python 2.6,所以将 Python 升级到 2.7 可能会导致一些不好的事情......
    【解决方案2】:

    首先,看起来您已经在端口 5000 上运行/侦听了一些应用程序。 您可能希望使用sudo sockstat |grep 5000 找到哪个,然后配置Apache,或者使用localhost:5000 终止进程/服务。

    其次,您的虚拟主机似乎没有被考虑/没有完全配置。

    【讨论】:

      【解决方案3】:

      一般你应该使用mod_fastcgi 和类似的配置:

      <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 documentation 中阅读更多详细信息)或使用Flask-Script @987654331 @ 命令在调试服务器中运行您的应用程序(参见Flask-Script documentation 中的示例

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 感谢先生的回答,但是当我尝试让Apache执行它时再次出现此错误。有关详细信息,请参阅我更新的问题。
        • 您好,先生。也许你只是错过了我的最后一条消息,但是这个答案确实不能完全解决这个问题。我已经在这个问题上开始了 500-rep 的赏金以引起一些注意。 再看看。
        • ScriptAlias 用于 CGI 脚本,而不是 FASTCGI。如果在 Apache 中加载了 mod_fcgid 模块,请参阅 httpd.apache.org/mod_fcgid/mod/mod_fcgid.html 的文档
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-21
        • 2014-09-10
        • 1970-01-01
        • 2017-06-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多