1

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?

user760900
  • 298
  • 1
  • 12

1 Answers1

1

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
Saeed
  • 3,255
  • 4
  • 17
  • 36
  • tried adding both onto the `.htaccess`. still getting `404` – user760900 Sep 22 '20 at 19:25
  • When you open link like yoursite.com/script.py, does it work and open in the web browser? Or it shows you the plain code without being executed? Also check your files permissions are 0755. – Saeed Sep 22 '20 at 19:50
  • Yes, the permissions are 0755. I get a `No such file or directory` error. It does load, but it goes to the 404 page. – user760900 Sep 22 '20 at 20:00
  • stop your Flask project and write a simple .py file with only `print('hello')` to see if you can run a simple file on your host. – Saeed Sep 22 '20 at 20:44
  • hmmm. that seems to be it. It's still giving me a 404 error for some reason if I try to access `*.py` but works for `*.html`. Let me look into that. – user760900 Sep 22 '20 at 20:49
  • it's odd that you still get 404 error. Check your hosting settings and also see if any .php file works. – Saeed Sep 22 '20 at 20:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221897/discussion-between-user760900-and-saeed). – user760900 Sep 22 '20 at 21:01
  • did you manage to solve this? I have the same problem – Jonathan Lindgren Feb 11 '21 at 20:15
  • Yes, it works for me as I'm now using this `.htaccess` rules in my website. Be sure your rules are executed before. – Saeed Feb 12 '21 at 09:08