1

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?

user1592380
  • 34,265
  • 92
  • 284
  • 515

2 Answers2

2

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)
user305883
  • 1,635
  • 2
  • 24
  • 48
  • Thank you for looking at this. What do you mean by '# move absolute path here' ? Does that mean sys.path.insert(0, '/home/username/public_html') – user1592380 Nov 28 '16 at 16:55
  • That is a mockup taken from WSGI wrapper, you should adapt it. Try importing the path and wsgi modules in the module app itself. An alternative is use flask templating, which allows you to serve files in "public" static folders - public relative to the path of your app. This links shows how to serve files from filestystem loader: https://buxty.com/b/2012/05/custom-template-folders-with-flask/ also look at: http://flask.pocoo.org/docs/0.11/tutorial/folders/ and http://stackoverflow.com/a/28758723/305883 may help you with logic. Sorry I can't do more for my laptop is now under maintanance :/ – user305883 Nov 28 '16 at 21:05
1

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.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • Thank you for answering. I tried : ' apachectl -t -D DUMP_MODULES' and got -bash: apachectl: command not found. I can ask the support people to run this for me , but is there another way to get this info? – user1592380 Nov 29 '16 at 15:31
  • You're better off asking the support people to confirm that you're using Apache and tell you whether CGI or FCGI is enabled. – cwallenpoole Nov 29 '16 at 16:40
  • They said that neither CGI or FCGI is enabled. – user1592380 Nov 30 '16 at 14:40