logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Other Software View previous topic :: View next topic
Reply to topic   Topic: Can't run Python .cgi script(s) in Apache 2.2
Author
Echo



Joined: 25 May 2011
Posts: 5

PostPosted: Wed 25 May '11 4:29    Post subject: Can't run Python .cgi script(s) in Apache 2.2 Reply with quote

Hello All

I'm fairly new in Python and using Apache 2.2 running on windows XP.

I've installed Apache 10 days ago and installed it in default directory:
c:\program files\apache foundation..\apache2.2.

I haven't change anything in 'httpd.conf' file and accepted the 'cgi-bin' directory as a default directory to run all .cgi files.

I've been reading a lot on how to run cgi on apache and feel so hopeless cuz i've tried so many things but still i can't run .cgi scripts neither from default cgi-bin nor default htdocs .

my apache's directory setup it's weird but during installation everything went so smooth without any error at all. but reading through diff forum, the cgi-bin directory must be under web root (htdocs ) but mine after installation is:
apache 2.2 > bin > cgi-bin > conf > error > htdocs > icons > logs > manual.

when i try to run my python .cgi script(s) from 'htdocs' (using DocumentRoot) as a folder to put my .cgi scripts.
(i'm not worry about security risk from running scripts from 'htdocs ' cuz it's for educational purpose n home use only).

when i run .cgi script from 'htdocs' i get following error:
-------------------
404 Not Found

The requested URL /Hello2.cgi was not found on this server.
----------------------
(the URL i'm using is: http://127.0.0.1/Hello2.cgi or http://localhost/Hello2.cgi

(i only have one entry in host file which is: 127.0.0.1 localhost )

when i read the 'error' log is says:
------------------------------------------
[error] [client 127.0.0.1] File does not exist: C:/apache

I don't know why it points to this weird directory: c:\apache??

In my 'http.conf' file my settin for 'htdocs' are:
-----------------------------------------------------
DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
<Directory />

Options FollowSymLinks +ExecCGI
AllowOverride None
Order allow,deny
Allow from all
</Directory>
AddHandler cgi-script .cgi .pl .py
-----------------------------------------------------
when i try to run the same script from default cgi-bin directory which is activated in 'httpd.cof' by default i get following error:

500 Internal Server Error
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request..........(didn't want to wast more space)

The URL i'm using is: http://127.0.0.1/cgi-bin/Hello2.cgi or localhost/....

the error log says:

[client 127.0.0.1] C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/Hello2.cgi
is not executable; ensure interpreted scripts have "#!" or "'!" first line.

[client 127.0.0.1] (9)Bad file descriptor: don't know how to spawn child process: C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/Hello2.cgi

but the Hello2.cgi is like this:
-----------------------------------

#!c:/Python27/python.exe -u

print "Content-type: text/html\n\n";


print "Hello World! <br><br>";
print "<br><br>\n";
------------------------------------
in the 'http.conf' setting for default 'cgi-bin' is:

ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"
<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/">
AllowOverride None
Options Includes +ExecCGI
Order allow,deny
Allow from all
</Directory>
<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/">
AddHandler default-handler .html .htm .shtml
</Directory>

by default there's index.html in cgi-bin directory which i also copied to 'htdocs' just to make sure it's there.

Once i've tried creating 'cgi-bin' under DocumentRoot (htdocs) and copy n pasted all my script there but i get 'an access error'.

I don't know what i'm doing wrong?
Please help me out to get my python .cgi script running in Apache.
I'm so frustrated after reading so many forums (which i've also learn a lot) still not being able to nail down the problem.

I can run any .cgi script from command prompt but not from browser.

I've also tried to use this in order to eliminated using 'shebang' line which is really making me sick cuz i can't find anything in there.
I added this to 'http.conf':

ScriptInterpreterSource Registry
PassEnv PYTHONPATH
SetEnv PYTHONUNBUFFERED 1

still not working.
I'm running out of options here.
Your help is much appreciated.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Wed 25 May '11 11:02    Post subject: Reply with quote

AFAIK the CGI path in the phyton script is not correct. It shouldn't be

#!c:/Python27/python.exe -u
but
#!/Python27/python
Back to top
Echo



Joined: 25 May 2011
Posts: 5

PostPosted: Wed 25 May '11 23:31    Post subject: Can't run Python .cgi script(s) in Apache 2.2 Reply with quote

Hello

Thanks for swift response.
from the beginning reading so many forum i've been changing the 'shebang' line and once again as you recommended i did change the shebang to:

#!/Python27/python

but I still get the 500 Internal Server Error.
reading the error log it still says:
--------------------------------------
[client 127.0.0.1] C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/Hello2.cgi is not executable; ensure interpreted scripts have "#!" or "'!" first line
[Wed May 25 14:21:42 2011] [error] [client 127.0.0.1] (9)Bad file descriptor: don't know how to spawn child process: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/Hello2.cgi

I have same .cgi scripts in both 'htdocs' and 'cgi-bin' as a default setting in 'httpd.conf'.

when i do :
http://localhost/Hello2.cgi OR http://localhost/cgi-bin/Hello2.cgi

still getting 500 error.

One thing i notice when i put the:
---------------------------------------
ScriptInterpreterSource Registry

PassEnv PYTHONPATH
SetEnv PYTHONUNBUFFERED 1

in 'registery' to eliminate using 'shebang' the browser goes on n on for more then few minutes like 4-5 min and then i get server time-out.

in 'registery' the 'PYTHONPATH' is set to: C:\Python27. is this right?

Thank you all for your help but seems nothing working so far.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Thu 26 May '11 16:15    Post subject: Reply with quote

I tested it now myself.

Installed Phyton 2.7

Added to my httpd.conf
Code:

AddHandler cgi-script .cgi .pl .py

NameVirtualHost *:80

<VirtualHost *:80>
   ServerName localhost
   DocumentRoot "/mario/Apache22/htdocs"
   <Directory "/mario/Apache22/htdocs">
      Options Indexes Includes FollowSymLinks ExecCGI
      AllowOverride All
      Order Allow,Deny
      Allow From All
   </Directory>
   LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
   #CustomLog logs/localhost.log combined
</virtualhost>


works! Ok this is not in cgi-bin, but I must confess that I never was a friend of that Wink

Sadly I wasn't albe to get it running over fcgid


---- edit ----

Note the -u following the interpreter path; this is very important. It puts the python interpreter in "unbuffered" mode. Trying to run python cgi scripts in the (default) buffered mode will either result in a complete lack of return value from your cgi script (manifesting as a blank html page) or a "premature end of script headers" error.

maybe it is better to use SetEnv PYTHONUNBUFFERED 1 in httpd.conf instead of -u parameter.
Back to top
Echo



Joined: 25 May 2011
Posts: 5

PostPosted: Fri 27 May '11 0:49    Post subject: Can't run Python .cgi script(s) in Apache 2.2 Reply with quote

Thanks for your help.

I don't have a virtual host set.
I'm simply trying to use either default 'htdocs' or default 'cgi-bin' to put my .cgi scripts in and runing them for either one.

The setup in 'httpd.conf' for 'htdocs' is:
---------------------------------------------
<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs">
Options Indexes FollowSymLinks +ExecCGI
AllowOverride None
Order allow,deny
Allow from all
</Directory>
AddHandler cgi-script .cgi .pl .py

The in 'httpd.conf' for 'cgi-bin' is:
------------------------------------
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/">

AllowOverride None
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>

URL i'm using is:http://127.0.0.1/cgi-bin/Hello2.cgi OR http://127.0.0.1/Hello2.cgi

BUT keep getting same error over two weeks of trying:

[client 127.0.0.1] C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/Hello2.cgi is not executable; ensure interpreted scripts have "#!" or "'!" first line
[Thu May 26 15:41:14 2011] [error] [client 127.0.0.1] (9)Bad file descriptor: don't know how to spawn child process: C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/Hello2.cgi

the 'shebang' line is: #!/Python27/python (normally) but when i try #!/Python27/python -u with following lines in .conf file the browser goes on n on over 10 minutes.

ScriptInterpreterSource Registry
PassEnv PYTHONPATH
SetEnv PYTHONUNBUFFERED 1

thanks
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Fri 27 May '11 10:20    Post subject: Reply with quote

in your directory config for cgi-bin the ExecCGI is missing Wink

ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/">

AllowOverride None
Options Indexes FollowSymLinks ExecCGI
Order allow,deny
Allow from all
</Directory>
Back to top
Echo



Joined: 25 May 2011
Posts: 5

PostPosted: Sat 28 May '11 0:46    Post subject: Can't run Python .cgi script(s) in Apache 2.2 Reply with quote

Hi

I've tried it many times with so many diff setting in 'httpd.conf'

If you don't mind give me ur email address then I could attache the
httpd.conf' file then you can review settings for both 'htdocs' and 'cgi-bin'.

otherwise Please give me other options to play with cuz i'm still getting the 500 Internal error and in error log file it says:
.. file isn't executable.
... bad file descriptor.

same error as i posted last time.

thanks
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Sat 28 May '11 18:13    Post subject: Reply with quote

Your error message sounds like the shebang line isn't really the very first line in Hello2.cgi.
It doesn't seem like an httpd.conf problem.

If you normally use a different editor, you could try opening Hello2.cgi using Windows Notepad instead of your regular editor, and check that the shebang line really is the first line. There must not be any blank lines before the shebang line; and the # must be the very first character of the line.

like this:
Code:
#!/Python27/python.exe -u

print "Content-type: text/html\n\n";

print "Hello World! <br><br>";
print "<br><br>\n";

not like this:
Code:

#!/Python27/python.exe -u

print "Content-type: text/html\n\n";

print "Hello World! <br><br>";
print "<br><br>\n";

If it still doesn't work with http://127.0.0.1/cgi-bin/Hello2.cgi, another test you could try is running your Python program from the command-line without Apache, like this:
Code:
C:\Python27\python.exe -u "C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\Hello2.cgi"
Note that when you run your program this way, you need quotes around the filename because it has spaces in it, and you use regular Windows back-slashes instead of forward-slashes like Apache uses.

Hope this helps,
-tom-
Back to top
Echo



Joined: 25 May 2011
Posts: 5

PostPosted: Sun 29 May '11 1:20    Post subject: Can't run Python .cgi script(s) in Apache 2.2 Reply with quote

Hi

Thanks a looooooooooooooooooot for the heads up about not having a blank line before 'shebang'.

That pretty much wrapped out my dilemma after struggling for almost 18 days reading almost all the forums google returns which was in other side really educational as well.

I was so hopeless about finding the issue and more importantly I couldn't ignore this 'cgi' topic and move on to next chapter cuz in my study, the next chapter is 'database' and it's all 'cgi' stuff.

thanks a lot.
Back to top


Reply to topic   Topic: Can't run Python .cgi script(s) in Apache 2.2 View previous topic :: View next topic
Post new topic   Forum Index -> Other Software