14

I'm trying to get started with Python but can't get my server setup up correctly for localhost(using Ampps). Python is running just fine through IDLE and command line, however, when I open up the file in the browser the code is displayed and not run.

I followed this http://www.imladris.com/Scripts/PythonForWindows.html tutorial for getting cgi set up, but it's not working.

Here's the code for my "hello world" program, if that makes any difference.

#!/usr/bin/env python
# -*#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

Any suggestions?

user1104854
  • 2,137
  • 12
  • 51
  • 74
  • Have you made the file executable? Have cgi-scripts been enabled in the server? – Some programmer dude Apr 12 '12 at 13:13
  • 2
    After you get this working - check Python's syntax for multiline strings using triple-quotes like `""" something """` – jsbueno Apr 12 '12 at 13:23
  • @JoachimPileborg I believe I enabled the cgi-scripts on the server, but I'm not sure about making the file executable. When I view the file (the icon for file itself, not the code) it has the python logo on it, so I'm not sure if that means it's executable. – user1104854 Apr 12 '12 at 14:22
  • If my answer was helpful to you, and if you believe it to be correct, please consider marking it as the accepted answer for this question. Thanks. – Ben Burns Oct 12 '14 at 18:18

2 Answers2

6

Your web server is treating your python code as a static file rather than an executable. The goal here is that you want Apache to execute python and send the stdout back to the user's browser.

I'm not familiar with Ampps, but the basic Apache flow to getting this setup is something like this.

  1. Edit your Options line of httpd.conf to include ExecCGI
  2. Register your python file with httpd.conf as a cgi handler by adding the following line:
    AddHandler .py
  3. Restart Apache
  4. Be sure that your shebang line (the #!/usr/bin/env python bit on top) actually points at the path to your python executable. For python2.6 living on C:, this might read:
    #!\Python26\python
  5. If your scripts must be portable, instead of modifying the shebang line add the line ScriptInterpreterSource registry to your httpd.conf, and make sure that windows opens *.py files with Python.exe by default. This can be tested by double-clicking a script. If it doesn't execute, right-click it, choose open-with, then other, and then browse to your python.exe. Make sure to check the "always use this program to open files of this type" box. There's a Microsoft Knowledge Base article on this here.

Finally, mod_wsgi or FastCGI is the preferred method for getting Apache to execute python, with the prior holding preference to lower traffic sites (10's of thousands of requests per day). I'd also advise looking into web frameworks such as web.py (very lightweight) or django (heavier weight, but saves you tons of time if you're collecting user input or interfacing with a database).

Ben Burns
  • 14,978
  • 4
  • 35
  • 56
  • Ampps was the first package I saw that had python built into it so I chose it. Are there any others that you would recommend? I use xampp when I'm programming with PHP, not sure if that supports it. – user1104854 Apr 12 '12 at 13:17
  • I didn't realize that "Ampps" was the windows flavor of Apache -- I'm mostly a Linux guy, and I mostly use Lighttpd. See edited answer, please. – Ben Burns Apr 12 '12 at 13:20
  • 1
    Also, you'd be better off using mod-wsgi than cgi. I'll edit the answer again to reflect that, and add links. – Ben Burns Apr 12 '12 at 13:20
  • 2
    I'm having one hell of a time getting this to run. I'm pretty experienced with PHP and have set up apache about a million times. There has to be some easier method out there. Everything I've read requires compiling source code and all kinds of junk. If python is this difficult to set up, no wonder why more people don't bother learning how to program. PHP is a breeze to get going, not sure about Python. I've been going at it for like 2 hours now and have accomplished nothing. – user1104854 Apr 12 '12 at 16:06
  • 1
    Without getting into the win/lin debate, Apache was designed from the ground up for unix-flavored systems, and getting it to run on Windows well is kind of a contortion. I hate making this suggestion, but if Windows is a must, you might instead try the more win-friendly route of using IIS - see this KB article http://support.microsoft.com/kb/276494 – Ben Burns Apr 12 '12 at 17:15
  • Also, this link appears to have downloadable binaries for Windows Apache mod_wsgi. http://brugbart.com/Tutorials/installing-mod-wsgi-apache-windows – Ben Burns Apr 12 '12 at 17:17
2

The given solution worked for me on raspberry pi 3 B+:

1) try to edit: sudo nano /etc/apache2/sites-enabled/000-default.conf

2) Add the below code inside this file:

    <Directory /var/www/html>
            Options +ExecCGI
            AddHandler cgi-script .py
            # DirectoryIndex index.py
    </Directory>

3) restart apache2 : sudo service apache2 restart

4) in your code just add the few lines above:

#!/usr/bin/env python3
import cgitb
cgitb.enable()
print("Content-Type:text/html;charset=utf-8")
print()
print("Hello world")

5) Your code should work ;)