[英]404 when trying to deploy Flask app to Apache
I am trying to deploy a Flask app to my Linux based server with no success.我正在尝试将 Flask 应用程序部署到基于 Linux 的服务器,但没有成功。 I have already looked through different approaches and entries on stackoverflow, but nothing seems to work.
我已经查看了 stackoverflow 上的不同方法和条目,但似乎没有任何效果。 To test it a little cleaner, I have created a simple test application that I am trying to run locally:
为了更简洁地测试它,我创建了一个尝试在本地运行的简单测试应用程序:
In /var/www/test
:在
/var/www/test
:
test.py测试.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index ():
return "Hello World!"
if __name__ == '__main__':
app.run()
test.wsgi测试.wsgi
from test import app as application
and an empty __init__.py
file.和一个空的
__init__.py
文件。
In /etc/apache2/sites-available
:在
/etc/apache2/sites-available
:
test.conf测试.conf
<VirtualHost *>
ServerName *Local IP address*
WSGIDaemonProcess test user=www-data group=www-data threads=5
WSGIScriptAlias / /var/www/test/test.wsgi
<Directory /var/www/test>
WSGIProcessGroup test
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
</VirtualHost>
The test.conf file was registered via a2ensite test.conf
and libapache2-mod-wsgi
was installed via apt on my local Debian Buster distribution I am testing this on (localhost). test.conf 文件是通过
a2ensite test.conf
注册的,并且libapache2-mod-wsgi
是通过 apt 安装在我的本地 Debian Buster 发行版上的,我正在(localhost)上进行测试。 Python2 and 3 are installed together with Flask for each of them. Python2 和 3 分别与 Flask 一起安装。 Every file and the
test
folder are owned by www-data with the group www-data.每个文件和
test
文件夹都由 www-data 拥有,组为 www-data。 And of course I am restarting the application after every change with service apache2 restart
.当然,每次更改后我都会使用
service apache2 restart
启动应用程序。
I can successfully run the application with python test.py
on localhost:5000, printing "Hello World.".我可以在 localhost:5000 上使用
python test.py
成功运行应用程序,并打印“Hello World.”。
I don't see anything suspicious in the Apache error-log either, just:我在 Apache 错误日志中也没有看到任何可疑之处,只是:
[Sun Oct 27 22:16:55.923721 2019] [mpm_event:notice] [pid 26336:tid 140548964586624] AH00489: Apache/2.4.38 (Debian) mod_wsgi/4.6.5 Python/2.7 configured -- resuming normal operations
[Sun Oct 27 22:16:55.923771 2019] [core:notice] [pid 26336:tid 140548964586624] AH00094: Command line: '/usr/sbin/apache2'
What am I missing?我错过了什么? Why am I getting a 404?
为什么我会收到 404? Is there any way to see, if the application is even running or just attempting to run?
有什么方法可以查看应用程序是否正在运行或只是尝试运行?
ServerName *Local IP address*
<- this. ServerName *Local IP address*
<- 这个。 You can't use an IP there, it has to be a hostname (that matches the one you use in the URL you access).您不能在那里使用 IP,它必须是一个主机名(与您在您访问的 URL 中使用的主机名匹配)。 If you use an IP to access your server, it will default to the first defined vhost, as it won't match any of the Servernames, which is probably not the one you just defined.
如果您使用 IP 访问您的服务器,它将默认使用第一个定义的虚拟主机,因为它不会匹配任何服务器名称,这可能不是您刚刚定义的名称。
So either put a hostname there and define it in your hosts
file if it's a name that doesn't resolve, or edit the default vhost (vhosts are loaded in alphanumerical order, which mean unless test.conf
is the first entry when listing sites-available
, it is not the first one and will not be used when accessing the server with an IP).因此,要么将主机名放在那里并在
hosts
文件中定义它,如果它是一个无法解析的名称,或者编辑默认虚拟主机(虚拟主机按字母数字顺序加载,这意味着除非test.conf
是列出sites-available
时的第一个条目 - sites-available
,它不是第一个,在使用IP访问服务器时不会使用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.