I have python CGI script that runs perfect on my local Apache 2.2.22 server. It outputs correct result and so on. But when I try to execute it on virtual hosting, there is error 500.
I really have no idea why it does not works.
Apache error log looks like:
[Wed Jul 12 16:06:54 2013] [error] [client 89.223.235.12] Premature end of script headers: /home/u67442/rrrrr.com/cgi-bin/test.py
[Wed Jul 12 16:09:31 2013] [error] [client 89.223.235.12] Premature end of script headers: /home/u67442/rrrrr.com/cgi-bin/test.py
I've already tried following things:
I'm sure that path to interpreter is correct - #!/usr/local/bin/python. Another CGI script works fine with this path.
I have set chmod 755 to this script.
I have set end-of-line characters in UNIX-format.
I use correct HTTP-header: print "Content-type:text/html\n\n"
Output section of script:
print "Content-type:text/html\n\n"
print "<html>"
print "<head>"
print "<title>Results</title>"
print "<head><h2 align=center>Results</h2></head>"
print "</head>"
print '<body bgcolor="#e8f3d8">'
print "<hr>"
print "<b>Result = %s </b>" % str(round(total_sum, 5))
print "</body>"
print "</html>"
Funny that another VERY similar script with the same path, header, EOL, output and so on works perfect on local server and virtual hosting. And it's very strange that this script works fine on my local apache, but on virtual web hosting it crashes with 500 internal server error. I really don't know what to do. In technical support says that there is problem in my script.
There is only one idea - timeout of waiting of output. Data processing in my script takes about 15-25 seconds.
What can you advice?
Check your error_log.
If you can, run the script from the command line of the virtual host.
If you can, su - webserveruser and do it again.
Are you trying to import a module that is not present on the server?
Does the webserver process have permission to fetch the data that ends up in total_sum?
To narrow down the problem, try hard-coding a value for total_sum and commenting out the code that fetches data and computes it. Does the rest work then?
Does the virtual host run the same version of python as your local server? If not, check that your code works in both versions.
Problem solved. Problem was in the script. I replace function for data extraction from *.dat files from one of 'while' loops.
Anyway, I still do not understand why it worked on local server and didn't worked on virtual hosting.
Related
I have this Python script to use within a PHP page, and I have found out the code for PHP to run it. This is my code:
<?php
$command = '3.8/bin/python3 googleNewsScraper.py CNN';
$output = exec($command, $output, $status);
echo $status;
echo $output;
?>
When I use the command php index.php, it works fine and echos out the output of the command, which is good. But, it doesn't output anything on my XAMPP local server, so I echo'ed the status. When I use the full path to the python interpreter, it gives me 127, which is because it doesn't know that command. But, with the current command as shown above, I get a status of 2. I have tried using the full path to each file, tried using a simple file that can't return errors, tried editing httpd.conf to include AddHandler cgi-script .py, restarted XAMPP multiple times, but nothing is happening or working.
Just to be clear, I am 100% sure that this is not a problem with paths as I was able to print out the contents of the file using the exact same path (just used the command echo file_get_contents("googleNewsScraper.py");. Any help is greatly appreciated.
EDIT: I checked the error log and this is the error that is occurring:
[Mon Jun 29 05:44:17.177250 2020] [core:notice] [pid 652] AH00094: Command line: '/opt/lampp/bin/httpd -E /opt/lampp/logs/error_log -D SSL -D PHP'
sh: 1: 3.8/bin/python3: Exec format error
EDIT 2: If I use the full path I get this error:
sh: 1: /Users/rosen59250/.bitnami/stackman/machines/xampp/volumes/root/htdocs/PHP API Page/3.8/bin/python3: not found
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.
I am taking over a django project which another developer maintained. The service is run on an Ubuntu machine. ZEO is used for content caching. Ajax/Dajax is used for asynchronious content. Celery is used for task management and Django is used for the project itself.
The service is usually reached via a specific IP address which limits access to specific URLs. http://my_server_ip. Without knowingly changing anything, this started to not work. Instead of taking me to the splash page, entering the IP would hang, unsuccesfully connecting. I don't get a 404, 500 or some other error, it just sits and continually tries to load as if waiting to connect or to receive content.
I attempted to restart the service in the hopes that this would solve the problem, it did not. I performed a system reboot and followed the following commands, as per the prior developer's documentation, to reboot the server.
From within the django project:
runzeo -a localhost:8090 -f /path/to/operations_cache.fs
su project_owner
python manage.py celery worker -n multiprocessing_worker --loglevel=debug -Q multiprocessing_queue
python manage.py celery worker --concurrency=500 --pool=eventlet --loglevel=debug -Q celery -n eventlet_worker
The two celery commands had to be run as the owner of the project directory.
Finally I ran sudo service apache2 restart. Upon completion, I tried to navigate to the webpage but received the same response: hung on connecting. The trac pages do work at http://my_server_ip/trac.
The following is all I have found in the apache log files.
error.log
[Fri Feb 06 16:01:11 2015] [error] /usr/lib/python2.7/dist-packages/configobj.py:145: DeprecationWarning: The compiler package is deprecated and removed in Python 3.x.
[Fri Feb 06 16:01:11 2015] [error] import compiler
[Fri Feb 06 16:01:11 2015] [error]
access.log
my.ip - user [06/Feb/2015:15:55:40 -0500] "GET / HTTP/1.1" 500 632 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:35.0 Gecko/20100101 Firefox/35.0"
I have tried looking into the django logs, but nothing appears there. Perhaps I am not finding the correct logs.
As a good start, where can I find out what the system is hanging on. How do I determine if it is an issue with django, apache, the interaction between the two, etc. This would help me zero in on specifically what is happening.
Edit
I was able to resolve my problem, though I cannot say for sure what resolved the issue. I suspect that the issue had to do with permissions on the static files folder. I serve my content through the www-user. After going through the steps described above I then ran yes yes | python manage.py collectstatic as the user www-data. The server was able restart and I was able to access the trac content as well as the django content.
I am hesitant to post this as an answer because I do not know with certainty whether my solution described here is the step which solved the problem.
I am trying getting starting with python web programming but I have a problem. I want to work with CGI
I installed Ampps web-server on pc (Linux). According to this instruction Apache Tutorial: Dynamic Content with CGI. I put my file index.pl in the folder '/usr/local/ampps/www/cgi-bin' and tried to accesses it
http://localhost/cgi-bin/index.py
Here is code of my file:
#!/usr/local/ampps/python/bin/python
print "Content-type: text/html\n\n"
print "Hello World from PythonStandard Hello World from a Python"
But it gives me mistake Sorry, the page you are looking for is currently unavailable.
Here is error log of Apache:
[Tue Jul 22 20:05:45.019799 2014] [cgi:error] [pid 11952] [client 127.0.0.1:38359] AH01215: (13)Permission denied: exec of '/usr/local/ampps/www/cgi-bin/index.py' failed
[Tue Jul 22 20:05:45.020262 2014] [cgi:error] [pid 11952] [client 127.0.0.1:38359] End of script output before headers: index.py
I corrected conf of Apache
AddHandler cgi-script .cgi .pl .py
it looked ok too
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "{$path}/www/cgi-bin/"
What can be wrong else?
Your script needs to have the executable bit set (using chmod) and the Apache user or group (usually named 'Apache' or 'www-data' depending on your distro - you can check /etc/passwd to find out) must have the 'execute' permission on it.
I was getting similar errors. It seemed that the AMPPS installation of Python was missing one of the DLLs. I downloaded the 2.7 .msi package from ActiveState and installed it.
A couple of things before you start: the location, and the permissions.
I may be wrong on this, but Apache will be looking for the Python executable in the directory that was created for it in the AMPPS installation -- c:\whatever\whatever\ampps\python. I tried installing it in c:\whatever\whatever\ampps\python2.7, but it didn't work. I suppose I could have edited the httpd.conf file, but since the original installation wasn't working anyway, I overwrote the c:\w\w\ampps\python files.
I also ran the .msi as an administrator. This was tricky, because my company-issued machine doesn't let me simply right-click on an .msi and select "run as administrator." I finally went into the task manager (option 8 here: http://www.eightforums.com/tutorials/9564-run-administrator-windows-8-a.html), and put the full path to the downloaded file in the entry. And voila, "hello world."
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