Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple CGI python webserver + php not working

Now similar question(s) has been asked at other places. However, I have tried the suggested solution(s) there and it did not help.

I am running following python CGI webserver:

#!/usr/bin/python
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
serve = HTTPServer(("",80),CGIHTTPRequestHandler)
serve.serve_forever()

In the "cgi-bin" directory, following php file "simple.php" is stored:

 #!/usr/bin/php
 <html>
 <head>
 <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
 </html>

This file has been given executable permission and executes properly from just the commandline on the server.

Now if I start the server and try to access the page using "http://server/cgi-bin/simple.php", I get just a blank page and I don't see any error on the server stdout.

I looked through the code for CGIHTTPServer.py and I think the problem might be occurring when a process is forked to execute the cgi-bin and the file descriptors (stdin,stdout) are manipulated using dup2. However, I don't know how to get it working. This simple thing has been puzzling me for a few hours and would really appreciate any kind of help.

The server machine is a ubuntu 9.04 box.

like image 271
Methos Avatar asked Sep 02 '26 15:09

Methos


1 Answers

#!/usr/bin/php doesn't make your script run as a CGI; it makes it run as a command-line script. If you have a php-cgi binary sitting around, use that instead; otherwise, you may have to hack something nasty into the script like:

#!/usr/bin/php
Content-Type: text/html

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?> 
</body>
</html>

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!