当尝试将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
答案 0 :(得分:5)
我已经成功运行了你的榜样,但还是有一些调整才能让它发挥作用
您可能需要更改系统上的路径,因为您的日志似乎正在使用运行python2.6
的系统以及仍使用apache
文件的旧httpd
版本。<登记/>
如果可能,我会建议您升级您的环境。
这是一个循序渐进的工作解决方案:
1.安装virtualenvwrapper
:
sudo -EH pip2 install virtualenvwrapper
2.Activte it:
source /usr/local/bin/virtualenvwrapper.sh
3.创建虚拟环境:
mkvirtualenv minimal
4.安装flask
和flup
:
pip install -U flask flup
flipflop
不适合我,但是因为它的README状态
这个模块是由艾伦·萨迪(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.创建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&#39; 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)
答案 1 :(得分:1)
您无法从终端运行fastcgi脚本。该脚本应该由Apache执行。通常,您在Apache配置文件中的ScriptAlias
指令中配置它。
答案 2 :(得分: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
运行它(详情请参阅{{3} }}或使用Flask-Script
runserver
命令在调试服务器中运行您的应用程序(参见Flask documentation中的示例
答案 3 :(得分:1)
首先,看起来你已经在端口5000上运行/监听了一些应用程序。
您可能希望找到sudo sockstat |grep 5000
,然后配置Apache,或使用localhost:5000
终止进程/服务。
其次,看起来您的虚拟主机未被考虑/未完全配置。