使用 CGI 部署 Flask 应用程序
我使用 Flask 框架编写了一个小型应用程序。我尝试使用 cgi 来托管它。根据文档,我创建了一个包含以下内容的 .cgi 文件:
#!/usr/bin/python
from wsgiref.handlers import CGIHandler
from yourapplication import app
CGIHandler().run(app)
运行该文件会导致以下错误:
...
文件“/usr/lib/pymodules/python2.7/werkzeug/routing.py”,第 1075 行,bind_to_environ wsgi_server_name = environ.get('HTTP_HOST',environ['SERVER_NAME'])
关键错误:“SERVER_NAME”
状态:500 内部服务器错误
内容类型:文本/纯文本
内容长度:59
在我的应用程序中,我设置了:
app.config['SERVER_NAME'] = 'localhost:5000'
当我使用 Flask 开发服务器运行应用程序时,它运行得很好。 正如你所知,我对这些东西非常陌生,并且我搜索了其他有类似错误的人,但没有运气。感谢所有帮助。
I have written a small application using the Flask framework. I try to host this using cgi. Following the documentation I created a .cgi file with the following content:
#!/usr/bin/python
from wsgiref.handlers import CGIHandler
from yourapplication import app
CGIHandler().run(app)
Running the file results in following error:
...
File "/usr/lib/pymodules/python2.7/werkzeug/routing.py", line 1075, in bind_to_environ
wsgi_server_name = environ.get('HTTP_HOST', environ['SERVER_NAME'])
KeyError: 'SERVER_NAME'
Status: 500 Internal Server Error
Content-Type: text/plain
Content-Length: 59
In my application I have set:
app.config['SERVER_NAME'] = 'localhost:5000'
When I run the application with the Flask development server it works perfectly well.
As you can tell I'm very new to this stuff and I have search for others with similar errors but with no luck. All help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将尝试展示我所做的事情,并且它在 Godaddy 共享主机帐户中工作:
在 MYSITE 文件夹中的 cgi-bin 文件夹中,我添加了以下 cgi 文件:
如您所见 init MYSITE 文件夹中的文件有 Flask 应用程序。
最重要的是权限设置正确。我将此文件夹权限设置为 755 以及“/home/USERNAME/.local/bin/python3”文件夹!请记住,系统需要此权限才能打开 Flask。
要打开 cgi,我在 MYSITE 文件夹中有以下 .htaccess 文件:
因此,当有人进入您的页面时,它将呈现 cgi 文件。
I will try to show what I've done and it is working in Godaddy sharing host account:
In the cgi-bin folder in MYSITE folder, I added the following cgi file:
As you can see the init file in the MYSITE folder have the flask app.
The most important thing is to set the permissions right. I setted 755 to this folder permission AS WELL AS to "/home/USERNAME/.local/bin/python3" folder!! Remember that the system needs this permission to open flask.
To open the cgi I have the following .htaccess file in MYSITE folder:
So it will render the cgi file when someone enters your page.
为了完整起见,这是作为上述评论之后的答案发布的。
如上所述,cgi 脚本应该由某个服务器执行。以下是来自 CGI 1.1 RFC 的摘要:
对于环境变量(缺少并触发错误),请参阅 RFC 中的 sectuib 4.1。
This is posted as an answer following the comments above for the sake of completeness.
As discussed above, cgi scripts should execute by some server. Here's the abstract from CGI 1.1 RFC:
For the environment variables (which were missing and triggered the error) see sectuib 4.1 in the RFC.