Reputation: 247
I'm trying to deploy a flask app on a shared server (hostgator) using CGI but I'm getting a URL not found error when I use the base URL (mysite.com/) and all others. I've set up a virtual environment in the public_html/ folder with python 2.7.8 and Flask 0.12.4 (newer versions of flask not supported on python 2.7.8 because they use SSLContext).
I've determined that the .htaccess file and cgi-bin/main.cgi file are configured correctly by writing a small flask hello_world app in the public_html/ folder that works and runs the website. And I've tried checking the error logs on cpanel which are empty and couldn't find any other useful logs in the file system. I've also set all the permissions to 755 which is what hostgator recommended to no avail.
My file system is this:
public_html/
flask_hello_world.py
some .html files for testing
cgi-bin/
main.cgi
server/
app.py
__init__.py
views.py
models.py (sqlalchemy models)
static/
some .html files
My .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home4/MYSITE/public_html/cgi-bin/main.cgi/$1 [L]
Addhandler cgi-script .py .cgi
main.cgi:
#!/home4/MYSITE/public_html/venv/bin/python
import sys
sys.path.insert(0, '/home4/MYSITE/public_html/venv/lib/python2.7/site-packages/')
sys.path.insert(1, '/home4/MYSITE/public_html/server/')
from wsgiref.handlers import CGIHandler
from app import app
class ProxyFix(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['SERVER_NAME'] = ""
environ['SERVER_PORT'] = "80"
environ['REQUEST_METHOD'] = "GET"
environ['SCRIPT_NAME'] = ""
environ['QUERY_STRING'] = ""
environ['SERVER_PROTOCOL'] = "HTTP/1.1"
return self.app(environ, start_response)
if __name__ == '__main__':
app.wsgi_app = ProxyFix(app.wsgi_app)
CGIHandler().run(app)
And when I changed main.cgi to run flask_hello_world.py it ran just fine. Here's that code:
import flask
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return flask.send_from_directory('/home4/MYSITE/public_html/','index.html')
@app.route('/hello')
def hello():
return flask.send_from_directory('/home4/MYSITE/public_html/','hello.html')
Moving my main project .py files into the public_html folder did not solve the problem. Heres app.py and init.py from the actual project:
app.py:
import os
basedir = os.path.abspath(os.path.dirname(__file__))
from flask import Flask
##other imports including sqlalchemy
app = Flask(__name__)
app.config["CACHE_TYPE"] = "null"
# sqlalchemy setup
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db')
##some other configurations
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# flask login setup
login_manager = LoginManager()
login_manager.login_view = "login_view"
login_manager.init_app(app)
init.py:
from app import app, db, login_manager
import models
import views
My views.py has no import errors when I just run it using 'python views.py' and the route for '/' is identical to the hello world app. So I think there is a problem with app.py, init.py, or the fact that my routes aren't in the app.py file.
Upvotes: 0
Views: 519
Reputation: 247
The problem might have been that the views are not imported in app.py or init.py, so are unknown to the application. Thus the CGI script threw the URL not found.
Upvotes: 0