简体   繁体   English

尝试将 Flask 应用程序部署到 Apache 时出现 404

[英]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.

相关问题 Apache Web服务器在尝试在其上部署Flask时给出404 - Apache web server giving 404 when trying to deploy Flask on it 尝试在 GoDaddy 上部署 Flask 应用程序时出现 403 - 403 when trying to deploy Flask app on GoDaddy 尝试在 AWS EC2 上部署 Flask 应用程序时权限被拒绝 - Permission denied when trying to deploy Flask app on AWS EC2 尝试将Flask App部署到AWS Elastic Beanstalk时出错 - Error when Trying to Deploy Flask App to AWS Elastic Beanstalk 尝试将Python /烧瓶应用程序部署到Heroku时无法绑定到$ PORT - Failed to bind to $PORT when trying to deploy Python/flask app to Heroku 尝试使用 PythonAnyWhere 部署 Web Flask 应用程序时出现 WSGI 错误 - WSGI Error when Trying to Deploy Web Flask App with PythonAnyWhere 找不到404请求的URL(Apache上的flask应用程序) - 404 Requested URL Not Found ( flask app on apache) 在Apache中将Flask应用部署为单个脚本 - Deploy Flask app as single scripts in Apache 尝试部署Flask应用程序时,从beantalk中获取错误:“没有名为flask的模块” - Getting error from beanstalk when trying to deploy flask app: “no module named flask” 尝试在 CherryPy 服务器上部署 Flask 应用程序 - Trying to deploy a Flask app on CherryPy server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM