Roo*_*kie 6 python apache flask ubuntu-14.04
我有在 Ubuntu 14.04 上运行的 apache2 Web 服务器。默认情况下,当我启动 apache Web 服务器时,http://localhost我可以看到从 /var/www/html/index.html 文件启动的“Apache2 Ubuntu 默认页面”。
我在 /home/ubuntu/myprojects/ 位置有一个烧瓶应用程序。Flask 应用程序在 virtualenv 上运行,并具有适当的文件夹结构来呈现 html 文件。下面是文件夹结构
/home/ubuntu/myprojects/hello/hello.py(flask 应用程序渲染 html)/home/ubuntu/myprojects/hello/templates/hello.html
Flask 应用程序代码如下:
from flask import Flask
from flask import render_template
from flask import request
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template("hello.html")
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)
当我运行http://localhost:5000hello.html 时呈现。我想在http://localhost没有指定任何端口号的情况下调用hello.py flask 应用程序时呈现 hello.html 。为此,我添加了以下代码:
app.run(host='0.0.0.0', port=80)
Run Code Online (Sandbox Code Playgroud)
但是,当我运行 Flask 应用程序时,存在错误:
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
Traceback (most recent call last):
File "hello.py", line 21, in <module>
app.run(host='0.0.0.0', port=80)
File "/home/ubuntu/myproject/venv/local/lib/python2.7/site-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/home/ubuntu/myproject/venv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 618, in run_simple
test_socket.bind((hostname, port))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 13] Permission denied
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么。在 上http://localhost,来自 /var/www/html/ 的 index.html 正在呈现。
除了上述之外,当我使用 mod_wsgi 时,我添加了下面的代码 添加了 application.wsgi
app.run(host='0.0.0.0', port=80)
Run Code Online (Sandbox Code Playgroud)
在 中/etc/apache2/sites-available/000-default.conf,我在下面添加了:
<VirtualHost *:80>
WSGIDaemonProcess FunRoute user=ubuntu group=root threads=5
WSGIScriptAlias / /home/ubuntu/myproject/FunRoute/application.wsgi
<Directory /home/ubuntu/myproject/FunRoute/>
WSGIProcessGroup FunRoute
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
我也将 hello.py 改回:
app.run(host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)
但是403 Forbidden当我尝试启动http://localhost时收到错误
Forbidden
You don't have permission to access / on this server.
Apache/2.4.7 (Ubuntu) Server at 52.8.217.39 Port 80
Run Code Online (Sandbox Code Playgroud)
经过一点谷歌搜索,我得到了解决。
Apache 2.4 及更高版本启用了额外的安全性,其中语法已更改:
在 Apache 2.2 中,如果我们想允许访问文件夹,例如站点文件,那么我们提供以下选项:
<Directory /path/to/site/files>
Order deny,allow
Allow from all
</Directory>
Run Code Online (Sandbox Code Playgroud)
但是在 Apache 2.4 的情况下,我们需要提供以下选项:
<Directory /path/to/site/files>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Run Code Online (Sandbox Code Playgroud)
这允许我访问并且解决了 403 禁止错误,并且 mod_wsgi 还帮助我创建了一个 Flask 应用程序,我可以通过http://localhost而不是访问它http://localhost:5000