user760900
user760900

Reputation: 606

Flask CGI htaccess 404 not found

I'm trying to get a minimum flask app up and running on a shared server so I tried following the basic flask quickstart instructions with the CGI deployment instructions (python 3.8.2). That was giving me the error descibed here and here; I couldn't really figure out the solution but I tried a bunch of stuff and finally adding the os commands in myapp.cgi ensures that running ./myapp.cgi doesn't error with the below code (I can't access cgi-bin, so I use .htaccess instead). However, when I go to my home directory, I am still getting a 404 not found error.

myapp.cgi

#!/usr/bin/python3

from wsgiref.handlers import CGIHandler
from hello import app
import os

os.environ['SERVER_NAME'] = ''
os.environ['SERVER_PORT'] = '80'
os.environ['REQUEST_METHOD'] = 'GET'

CGIHandler().run(app)

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
# Don't interfere with static files
RewriteRule ^(.*)$ /u/a/b/user/public/html/myapp.cgi/$1 [L]

hello.py

#!/usr/bin/python3

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

I tried removing the $1 as some SO thread suggested, but that doesn't work. I'm also not altogether sure if my solution for the myapp.cgi is correct, as while it seems to be a common problem, I can't find a proper solution.

The shared server is also setup such that if there is no index.html, it will display the file structure, but that it's happening either - it's going straight to the 404 error page.

What could be the issues here? How would I ensure I can run the flask app on the shared server?

Upvotes: 1

Views: 385

Answers (1)

Saeed
Saeed

Reputation: 4125

You should add this code to your .htaccess file in order to parse the codes of Python:

Options +ExecCGI
AddHandler cgi-script .py

Also you may change index priority to something like this if Flask you're using does not redirect:

DirectoryIndex index.py my_script.py test.php etc.extension

Upvotes: 1

Related Questions