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:
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.?
2
Answers
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 thewebsite
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: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:
As for the
.htaccess
file, its also valid and would work — assuming you havemod_rewrite
enabled on your server. Check out this post for instructions on enabling that in case you haven’t already.I changed the shebang at the top of the
main.cgi
file, and it worked.Before:
#!/usr/bin/python
After:
#!/usr/bin/python3.7