user1592380
user1592380

Reputation: 36205

flask: The requested URL was not found on this server

enter image description here

I'm working with a shared hosting account which uses apache 2.4 , trying to deploy a flask app using http://fgimian.github.io/blog/2014/02/14/serving-a-python-flask-website-on-hostmonster . I've put the code and the fcgi script in public_html folder The contents of the folder are in the screenshot above:

The manage_apache.fcgi script is:

#!/home/username/anaconda2/bin/python
import sys,os
from flup.server.fcgi import WSGIServer
sys.path.insert(0, '/home/username/public_html')
from myflaskapp.settings import Config, SharedConfig
from myflaskapp.app import create_app

if __name__ == '__main__':
    app = create_app(SharedConfig)
    WSGIServer(app).run()

I've gotten to the last step and while testing it at the command line using putty to SSH in:

[~/public_html]# ./manage_apache.fcgi

I can see the correct web page being generated, so I assume that fast cgi is supported by my host. I'm not getting any python errors.

The .htaccess file out of the article is :

AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ manage_apache.fcgi/$1 [QSA,L]

In the browser when I surf to mysite.org I am getting

Not Found

The requested URL /manage_apache.fcgi/ was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

according to support The .htaccess file is redirecting to manage_apache.fcgi/$1

-rwxr-xr-x 1 myusername myusername Nov 22 17:26 manage_apache.fcgi*

How can I fix this?

Upvotes: 1

Views: 2760

Answers (2)

user305883
user305883

Reputation: 1741

I suspect

sys.path.insert(0, '/home/username/public_html')

is an absolute path, but flask application is looking to a relative path respect to flask gateway, and cannot find it.

Have you tried to wrap the libraries in the app instance - move the absolute path in the app instance?

As an example, see http://werkzeug.pocoo.org/: from werkzeug.wrappers import Request, Response

@Request.application
def application(request):
    return Response('Hello World!')

if __name__ == '__main__':
    from werkzeug.serving import run_simple
    # move absolute path here
    run_simple('localhost', 4000, application)

Upvotes: 2

cwallenpoole
cwallenpoole

Reputation: 81988

I suspect that fcgi is not supported on that host. Just because a host lets you run a Python script on the command line does not mean that they have configured mod_fcgi in Apache.

Try this: apachectl -t -D DUMP_MODULES | grep cgi. You should see fcgi_module or fastcgi_module, or possibly a cgi_module.

If you only see a cgi_module, then you should be able to use AddHandler cgi-script .py instead of AddHandler fcgid-script .fcgi.

If you see none of those, then you can try wsgi: apachectl -t -D DUMP_MODULES | grep wsgi. If you see wsgi_module, then you know you can use wsgi. At that point, you might be able to follow instructions here under .htaccess.

Upvotes: 1

Related Questions