Apache 网络服务器和 Flask 应用程序
Apache webserver and Flask app
我在 Ubuntu 14.04 上安装了 apache2 网络服务器 运行ning。
默认情况下,当我在 http://localhost
上启动 apache Web 服务器时,我可以看到 "Apache2 Ubuntu Default Page" 从 /var/www/html/index.html 文件启动。
我在 /home/ubuntu/myprojects/ 位置有一个烧瓶应用程序。 Flask 应用程序在 virtualenv 上 运行ning 并且具有适当的文件夹结构来呈现 html 文件。
下面是文件夹结构
/home/ubuntu/myprojects/hello/hello.py (flask app rendering a html)
/home/ubuntu/myprojects/hello/templates/hello.html
flask app代码如下:
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')
当我 运行 http://localhost:5000
呈现 hello.html 时。
当调用 http://localhost
时没有指定任何端口号,我想从 hello.py flask app 渲染 hello.html。
为此,我添加了以下代码:
app.run(host='0.0.0.0', port=80)
但是,当我 运行 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
我不知道我做错了什么。在 http://localhost
,来自 /var/www/html/ 的 index.html 正在渲染。
除了上面,当我使用 mod_wsgi 我添加了下面的代码
添加了 application.wsgi
import os, sys, logging
logging.basicConfig(stream=sys.stderr)
PROJECT_DIR = '/home/ubuntu/myproject/hello'
activate_this = os.path.join(PROJECT_DIR, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
sys.path.append(PROJECT_DIR)
from hello import app as application
在/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>
我还把 hello.py 改回了 :
app.run(host='0.0.0.0')
但是当我尝试启动时收到 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
在端口 80 上启动 flask 需要 root 权限,但在这种情况下它对你不起作用,因为 Apache 已经 运行 在那个端口上,你不能(通常)有两个不相关的进程侦听同一端口。
我假设您想为一般情况执行此操作 testing/development 并且执行此操作的一种方法是使用反向 http 代理,这样您就不必担心在编写代码时一直重启 apache已更改。
我建议在非 5000 的特定端口上启动应用程序;尝试 8000
app.run(host='0.0.0.0', port=8000)
在您的默认站点配置(应该是 /etc/apache2/sites-available/default
)中,您可以对其进行编辑以包括以下内容:
<Location />
ProxyPass http://localhost:8000
ProxyPassReverse http://localhost:8000
</Location>
这将接管您的默认站点的根目录。如果这是不可取的,你想定义一个单独的虚拟主机(最好在一个单独的文件中),然后调用 a2ensite <filename>
。此外,要使上述配置起作用,您还需要调用 a2enmod proxy_http
。这两个命令都需要 root 权限。
对于生产用途,您可能希望使用 mod_wsgi
之类的东西,您可以查看边栏上的相关问题列表以帮助您入门。
- Hello World - Flask / Apache / mod_wsgi - no response from Apache
谷歌搜索了一下后我解决了。
Apache 2.4 及更高版本启用了额外的安全性,语法已更改:
在 Apache 2.2 中,如果我们想要允许访问文件夹,例如站点文件,然后我们给出以下选项:
<Directory /path/to/site/files>
Order deny,allow
Allow from all
</Directory>
但如果是 Apache 2.4,我们需要提供以下选项:
<Directory /path/to/site/files>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
这允许我访问并解决了 403 禁止错误,并且 mod_wsgi 帮助我创建了一个烧瓶应用程序,我可以通过 http://localhost
而不是 http://localhost:5000
我在 Ubuntu 14.04 上安装了 apache2 网络服务器 运行ning。
默认情况下,当我在 http://localhost
上启动 apache Web 服务器时,我可以看到 "Apache2 Ubuntu Default Page" 从 /var/www/html/index.html 文件启动。
我在 /home/ubuntu/myprojects/ 位置有一个烧瓶应用程序。 Flask 应用程序在 virtualenv 上 运行ning 并且具有适当的文件夹结构来呈现 html 文件。 下面是文件夹结构
/home/ubuntu/myprojects/hello/hello.py (flask app rendering a html) /home/ubuntu/myprojects/hello/templates/hello.html
flask app代码如下:
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')
当我 运行 http://localhost:5000
呈现 hello.html 时。
当调用 http://localhost
时没有指定任何端口号,我想从 hello.py flask app 渲染 hello.html。
为此,我添加了以下代码:
app.run(host='0.0.0.0', port=80)
但是,当我 运行 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
我不知道我做错了什么。在 http://localhost
,来自 /var/www/html/ 的 index.html 正在渲染。
除了上面,当我使用 mod_wsgi 我添加了下面的代码 添加了 application.wsgi
import os, sys, logging
logging.basicConfig(stream=sys.stderr)
PROJECT_DIR = '/home/ubuntu/myproject/hello'
activate_this = os.path.join(PROJECT_DIR, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
sys.path.append(PROJECT_DIR)
from hello import app as application
在/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>
我还把 hello.py 改回了 :
app.run(host='0.0.0.0')
但是当我尝试启动时收到 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
在端口 80 上启动 flask 需要 root 权限,但在这种情况下它对你不起作用,因为 Apache 已经 运行 在那个端口上,你不能(通常)有两个不相关的进程侦听同一端口。
我假设您想为一般情况执行此操作 testing/development 并且执行此操作的一种方法是使用反向 http 代理,这样您就不必担心在编写代码时一直重启 apache已更改。
我建议在非 5000 的特定端口上启动应用程序;尝试 8000
app.run(host='0.0.0.0', port=8000)
在您的默认站点配置(应该是 /etc/apache2/sites-available/default
)中,您可以对其进行编辑以包括以下内容:
<Location />
ProxyPass http://localhost:8000
ProxyPassReverse http://localhost:8000
</Location>
这将接管您的默认站点的根目录。如果这是不可取的,你想定义一个单独的虚拟主机(最好在一个单独的文件中),然后调用 a2ensite <filename>
。此外,要使上述配置起作用,您还需要调用 a2enmod proxy_http
。这两个命令都需要 root 权限。
对于生产用途,您可能希望使用 mod_wsgi
之类的东西,您可以查看边栏上的相关问题列表以帮助您入门。
- Hello World - Flask / Apache / mod_wsgi - no response from Apache
谷歌搜索了一下后我解决了。
Apache 2.4 及更高版本启用了额外的安全性,语法已更改:
在 Apache 2.2 中,如果我们想要允许访问文件夹,例如站点文件,然后我们给出以下选项:
<Directory /path/to/site/files>
Order deny,allow
Allow from all
</Directory>
但如果是 Apache 2.4,我们需要提供以下选项:
<Directory /path/to/site/files>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
这允许我访问并解决了 403 禁止错误,并且 mod_wsgi 帮助我创建了一个烧瓶应用程序,我可以通过 http://localhost
而不是 http://localhost:5000