1

I keep receiving a 500 Internal Server Error with my Python Flask/CGI program.

I am running it on shared hosting, so I followed this tutorial: https://medium.com/@dorukgezici/how-to-setup-python-flask-app-on-shared-hosting-without-root-access-e40f95ccc819

This is my main python script: (~/website/mainApp.py)

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
        return "123 :)"

if __name__ == "__main__":
        app.run()

This is my CGI script (~/website/main.cgi)

#!/usr/bin/python

import sys
sys.path.insert(0, "~/.local/lib/python3.7/site-packages")

from wsgiref.handlers import CGIHandler
from mainApp import app
CGIHandler().run(app)

and this is my .htaccess file (~/website/.htaccess):

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /main.cgi/$1 [L]

This is basically a file tree of it: Image of file tree 2.0

This is the error I am getting:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

Does anyone see where an error may be?

Thanks!

Edit: It now has a weird .pyc file in it. I didn't add it though.?

costaparas
  • 5,047
  • 11
  • 16
  • 26

2 Answers2

1

The contents of your .cgi and .py files look fine.

The main problem is that the permissions on your .cgi file are not correct. Possibly also, the same goes for the website directory -- its permissions are not visible in the file view you posted.

You need execution (and read!) permission on the CGI file, and on any directories leading up to it.

In theory, the following, when run from inside the website directory, should be sufficient:

chmod a+rx . main.cgi

Notice the . to also apply the command to the current (website) directory. This adds read permission and execute permission to the owner, group and others.

If you don't want any permissions applied for the group, then this is sufficient:

chmod uo+rx . main.cgi

As for the .htaccess file, its also valid and would work -- assuming you have mod_rewrite enabled on your server. Check out this post for instructions on enabling that in case you haven't already.

costaparas
  • 5,047
  • 11
  • 16
  • 26
  • I update the permissions for the python file, the cgi file, and the directory they are in. I also added `RewriteBase /` to the .htaccess file. However, whenever I go to the URL, it brings me to the default page that IONOS servers, which has sedo parking on it. For some reason, it's not running the FLask. I updated the post. –  Jan 08 '21 at 13:01
  • I'm not really familiar with IONOS setup. But you need to make sure the `DocumentRoot` is set correctly - to point to `~/website/`. – costaparas Jan 08 '21 at 13:06
  • By `DocumentRoot`, do you mean `RewriteBase`? @costaparas –  Jan 08 '21 at 13:08
  • They are two different things - `DocumentRoot` is set in the (global) config, which should point to the server's root directory. (But I suppose you can't change that with IONOS). Maybe try `RewriteBase /website` in your `.htaccess` file instead. – costaparas Jan 08 '21 at 13:14
  • I did it, and I still get the original problem where it brings me to IONOS's sedo parking page. BTW, in IONOS you can change the website 'target', which is currently pointing towards /website . @costaparas –  Jan 08 '21 at 13:17
1

I changed the shebang at the top of the main.cgi file, and it worked.

Before: #!/usr/bin/python

After: #!/usr/bin/python3.7

  • Great! Glad it worked for you. Its odd that you didn't see any error message for this in the logs though? And instead, you were redirected to a default IONOS page; strange setup they have. – costaparas Jan 09 '21 at 01:07