简体   繁体   中英

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. I have already looked through different approaches and entries on stackoverflow, but nothing seems to work. To test it a little cleaner, I have created a simple test application that I am trying to run locally:

In /var/www/test :

test.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index ():
        return "Hello World!"

if __name__ == '__main__':
        app.run()

test.wsgi

from test import app as application

and an empty __init__.py file.

In /etc/apache2/sites-available :

test.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). Python2 and 3 are installed together with Flask for each of them. Every file and the test folder are owned by www-data with the group www-data. And of course I am restarting the application after every change with service apache2 restart .

I can successfully run the application with python test.py on localhost:5000, printing "Hello World.".

I don't see anything suspicious in the Apache error-log either, just:

[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? Is there any way to see, if the application is even running or just attempting to run?

ServerName *Local IP address* <- this. You can't use an IP there, it has to be a hostname (that matches the one you use in the URL you access). 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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM