在 Raspberry Pi 上尝试使用 WSGI 部署基本 Flask 应用程序时出现 404
404 when trying to deploy basic Flask application with WSGI on Raspberry Pi
我一直在按照 http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/ to get my Flask application to be deployed on Apache through WSGI on a Raspberry Pi running Raspbian, but whatever I try, I keep getting a 404-error on the location I've specified. I used the info on various webpages I could find (especially the instructions on http://www.ashokraja.me/post/Serving-a-Python-Flask-Web-Application-via-Apache-Webserver-in-Raspberry-Pi.aspx) 上的说明进行操作,但似乎没有任何效果。
以 root 身份登录,我创建了一个目录 /var/www/flasktest
,其中放置了两个文件:
flasktest.py
,包含:
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
和flasktest.wsgi
包含:
#!/usr/bin/python
import sys
sys.path.insert(0, '/var/www/flasktest')
from flasktest import app as application
然后我对两个文件都做了 chmod ugo+x
(不过不知道是否有必要)。
我还创建了 /etc/apache2/sites-available/001-flasktest.conf
包含
<VirtualHost *:80>
ServerName localhost
WSGIDaemonProcess flasktest threads=5
WSGIScriptAlias /flasktest/ /var/www/flasktest/flasktest.wsgi
<Directory /var/www/flask/flasktest>
WSGIProcessGroup flasktest
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
我执行了a2enmod wsgi
、a2ensite 001-flasktest.conf
和service apache2 reload
。每个命令都以 root 用户身份执行。
当我现在转到 http://localhost/flasktest
时,我收到 404 错误。 http://localhost
给我默认的 Apache 页面,显然 Apache 运行 是正确的。据我所知,Apache 根本不会生成任何错误消息。
我真的不知道出了什么问题:是什么让 404 出现?有人可以帮我吗?提前致谢!
仔细检查您的 WSGIScriptAlias 变量 - 应该是 (according to the docs)
WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi
而你有
WSGIScriptAlias /yourapplicaiton/ /var/www/yourapplication/yourapplication.wsgi
正如 Philip Tzou 也提到的,您的 Directory
路径可能也不正确。
此外,有时服务器名称 localhost
会给您带来问题。请改用 127.0.0.1:80
。
您似乎不小心将错误的路径传递给了 <Directory />
。尝试将 /etc/apache2/sites-available/001-flasktest.conf
更改为:
<Directory /var/www/flasktest>
...
</Directory>
您也可以查看 apache 错误日志以了解到底发生了什么。
我一直在按照 http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/ to get my Flask application to be deployed on Apache through WSGI on a Raspberry Pi running Raspbian, but whatever I try, I keep getting a 404-error on the location I've specified. I used the info on various webpages I could find (especially the instructions on http://www.ashokraja.me/post/Serving-a-Python-Flask-Web-Application-via-Apache-Webserver-in-Raspberry-Pi.aspx) 上的说明进行操作,但似乎没有任何效果。
以 root 身份登录,我创建了一个目录 /var/www/flasktest
,其中放置了两个文件:
flasktest.py
,包含:
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
和flasktest.wsgi
包含:
#!/usr/bin/python
import sys
sys.path.insert(0, '/var/www/flasktest')
from flasktest import app as application
然后我对两个文件都做了 chmod ugo+x
(不过不知道是否有必要)。
我还创建了 /etc/apache2/sites-available/001-flasktest.conf
包含
<VirtualHost *:80>
ServerName localhost
WSGIDaemonProcess flasktest threads=5
WSGIScriptAlias /flasktest/ /var/www/flasktest/flasktest.wsgi
<Directory /var/www/flask/flasktest>
WSGIProcessGroup flasktest
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
我执行了a2enmod wsgi
、a2ensite 001-flasktest.conf
和service apache2 reload
。每个命令都以 root 用户身份执行。
当我现在转到 http://localhost/flasktest
时,我收到 404 错误。 http://localhost
给我默认的 Apache 页面,显然 Apache 运行 是正确的。据我所知,Apache 根本不会生成任何错误消息。
我真的不知道出了什么问题:是什么让 404 出现?有人可以帮我吗?提前致谢!
仔细检查您的 WSGIScriptAlias 变量 - 应该是 (according to the docs)
WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi
而你有
WSGIScriptAlias /yourapplicaiton/ /var/www/yourapplication/yourapplication.wsgi
正如 Philip Tzou 也提到的,您的 Directory
路径可能也不正确。
此外,有时服务器名称 localhost
会给您带来问题。请改用 127.0.0.1:80
。
您似乎不小心将错误的路径传递给了 <Directory />
。尝试将 /etc/apache2/sites-available/001-flasktest.conf
更改为:
<Directory /var/www/flasktest>
...
</Directory>
您也可以查看 apache 错误日志以了解到底发生了什么。