I'm getting Internal Server Error from my Python script on Apache.
The script has chmod 755 and is in a directory different from cgi-bin.
Its content is
#!/usr/bin/python
print "Content-Type: text/html\n"
print "test"
I'm on shared hosting with limited options. In particular I cannot view the Apache logfile.
Apache tries to execute the script, but fails to do so because it is missing Options +ExecCGI in the .htaccess located in the script's directory.
It is important to begin the script with the two lines
#!/usr/bin/python
print "Content-Type: text/html\n"
Without those, or without a trailing \n, Apache would throw an Internal Server Error, too.
Related
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.?
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.
I changed the shebang at the top of the main.cgi file, and it worked.
Before:
#!/usr/bin/python
After:
#!/usr/bin/python3.7
I have been trying to run a simple python script hello.py with CGI but am getting 500 Internal Server Error.
My python code.
#!/usr/bin/python2.7
print '<html>'
print '<head>'
print '<title>Hello World - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello World! This is my first CGI program</h2>'
print '</body>'
print '</html>'
The directory which i have the python script running is in /var/www/crunchworld.The conf file which i enabled is in `/etc/apache2/conf-available/crunchworld.conf
The conf file looks like
<Directory /var/www/crunchworld>
Options +ExecCGI
AddHandler cgi-script .cgi
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I have cgi enabled and given the necessary permission for the file hello.py but its still showing me internal server error.When i checked the logs i see
End of script output before headers: hello.py
I have researched about the error and give appropriate permissions for the file but it doesnt work.
Any help would be so much appreciated. Thanks in advance.
Further changes i have made.
I have added AddHandler cgi-script .cgi .py in my crunchworld.conf file.
2.I have given permission for the file hello.py
I have symlinked /etc/apache2/conf-available/crunchworld.conf in /etc/apache2/conf-enabled
4.I had already installed python2.7 on the path /usr/bin/python2.7 and i have also tried using #!/usr/bin/env python but still it doesnt work.
Upon checking the logs i found End of script output before headers: hello.py, referer: http://localhost/
Thank you for your recommendations but it is still showing 500 internal error.
Your CGI script must also output header information.
The minimum required is the Content-type header -- which in this case should be set to text/html.
Add this to the start of your CGI script (before you print anything else).
print 'Content-type: text/html\n'
Take note of the extra trailing newline -- its necessary to leave at least one blank line between the header(s) and the content itself.
Update:
For further troubleshooting, do the following:
Make sure your CGI script has the correct permissions set: chmod 0755 hello.py just to be sure.
Your script appears to be .py whereas your apache config appears to only specify .cgi files. Your AddHandler should be AddHandler cgi-script .cgi .py.
You should symlink your /etc/apache2/conf-available/crunchworld.conf file in /etc/apache2/conf-enabled if you haven't already done so. Do so by running the following: cd /etc/apache2/conf-enabled; sudo ln -s ../conf-available/crunchworld.conf.
Always remember to restart apache if you make any changes to your apache config: e.g. sudo service apache2 restart.
Check that your hashbang line is correct. Does /usr/bin/python2.7 exist? You can try setting instead to #!/usr/bin/env python or #!/usr/bin/env python2. (Or better yet, switch to python3 if possible on your system).
Check the apache error logs once again. E.g. tail -20 /var/log/apache2/error.log (or wherever your logs are).
You can attempt further debugging with the cgitb module (see https://docs.python.org/3/library/cgitb.html).
I have apache2 running on my ubuntu 14.04. The configuration for the virtual host is
`ScriptAlias /cgi-bin/ /home/(path to some myfolder)/cgi-bin/
<Directory "/home/(path to some myfolder)/cgi-bin">
DirectoryIndex index.py
Options +ExecCGI
AddHandler cgi-script .py
Require all granted
</Directory>`
Then index.py is not running as well as any other python script. Instead the text of the scripts is shown in the browser window.
I also tried to stop apache and use python -m CGIHTTPServer instead, but with the same result.
Also, the command chmod +x index.py was routinely applied to all python scripts I tried.
To run the script I used the url: 127.0.0.1/index.py, though I also tried
127.0.0.1:80/index.py and 127.0.0.1:80/(the whole pathe to the file in the cgi-bin folder)
I use port 80.
The script I use to test is
#!/usr/bin/python2.7
print "Content-type: text/html"
print "Hello, World."
python2.7 is in the folder /usr/bin so the path is correct
The best I found on the Internet were advices implemented under Directory tag above. Bwt, the result does not change if I launch the scripts in browsers as root. I tried Firefox and Chrome, all the same.
The question is how to make my python scripts run in a browser?
I am running to a problem a vps I recently moved to. I am trying to run a python cgi script, but I am getting an apache Premature end of script headers Error.
(I chmod +x the script file)
The script is pretty simple:
#!/usr/bin/env python
import cgi, cgitb
cgitb.enable()
print "Content-type: text/html"
print "<html><body>hello scritp</body></html>"
Now if I name the script as test**.py** it runs fine on server. But if I do it the correct way, calling it test**.cgi** I receive a Internal Server Error.
I run the script from the terminal
./test.cgi
I get no errors
Content-type: text/html
<html><body>hello scritp</body></html>
Did anyone encountered before this issue? And a solution for it? :)
Cheers
change the header as:
print "Content-type: text/html\n\n"
There must be at least an empty line between HTTP headers and body.
So
print "Content-type: text/html\n" will work just fine
Reference: Wikipedia
I have a system with Ubuntu10.04 Operating System.
I have my Apache tomcat(6.0.29) installed at /usr/local/tomcat/
I have one python script hello.py
I would like to run this python script inside cgi-bin.
Python is at /usr/bin/python
My application I am running is eegis which is running fine.
http://localhost:8080/eegis/index.html
I have already searched in net a lot it is saying you have to configure web.xml with following configuration.
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi</param-value>
</init-param>
<init-param>
<param-name>executable</param-name>
<param-value>/usr/bin/python</param-value>
</init-param>
<init-param>
<param-name>passShellEnvironment</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
I have uncommented this also
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
I have done this but still I am not able to run python script.
While trying http://localhost:8080/eegis/cgi-bin/hello.py
I am getting this error
HTTP Status 404 - /eegis/cgi-bin/
type Status report
message /eegis/cgi-bin/
description The requested resource (/eegis/cgi-bin/) is not available.
Apache Tomcat/6.0.29
The problem has been resolved now. Still I will share my experience. The first problem was with my python script I had forgot to insert "\n\n" (print "Content-type: text/html\n\n") [mid it \n\n immediatetle after header without any spaces (print "Content-type: text/html \n\n") this will not work]at the end of the content description header. Second I have saved the file as .py but it should be .cgi every thing else was fine. My scripts are executing now. Thanks