尝试将Flask应用程序部署到我的LAMP服务器时,我从flipflop(一个FastCGI/WSGI网关)收到一个错误,该错误使我的应用程序能够使用FastCGI协议(protocol)。
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
,它也仍然不起作用: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
最佳答案
我已经成功运行了您的示例,但是需要进行一些调整才能使其正常工作。
您可能需要更改系统上的路径,因为从日志看来,您正在使用的系统运行的是python2.6
和较旧的apache
版本,该版本仍使用httpd
文件。
如果可能的话,我建议您升级环境。
这是一个分步工作的解决方案:
1.安装virtualenvwrapper
:
sudo -EH pip2 install virtualenvwrapper
2.激活它:
source /usr/local/bin/virtualenvwrapper.sh
3,创建虚拟环境
mkvirtualenv minimal
4,安装
flask
和flup
:pip install -U flask flup
flipflop
对我不起作用,但是它是自述文件因此您可以放心使用它。
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的FastCGI
开始查看error.log
:[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)
关于apache - 触发器的“OSError: [Errno 88] Socket operation on non-socket”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45703959/