These are the configuration steps for setting up and running a pyhton script on Apache in Windows environment
- Install Apache from – http://www.apache.org/.
Plz see my blog entry
How to set up Apache on Windows
- Install Python from http://www.python.org/download/. (windows installer)
- Go to httpd.conf directory. On my system it is under – C:\Program Files\Apache Software Foundation\Apache2.2\conf
- Under httpd.conf , uncomment the following line-
<br />AddHandler cgi-script .cgi<br />
This allows us to map cgi file extensions to handlers
- Now we have to set permissions of the root directory, or the directory which will contain the python files. In my case the root folder is C:\Program Files\Apache Software Foundation\Apache2.2\htdocs. I will keep my python files there.
Modify the directive in httpd.conf to look like this-<br /><Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"><br />Options ExecCGI<br /> Order allow,deny<br />Allow from all<br />SetHandler cgi-script<br /><Directory><br />
Two important lines which will help rendering python scripts on Apache are -
- Options ExecCGI
- SetHandler cgi-script - You have to restart the Apache server for changes to take effect
- Now we will create the python file. Open a notepad and type the following
<br />#!c:\Python27\python.exe<br />print "Content-Type: text/html\n\n"<br />print 'Hello, world!\n'<br />
- The first line points to the location of the python files. In my case it is
c:\Python27\python.exe.
This is a comment and preceded by a ‘#’
For python 2.7, the comment is – #!c:\Python27\python.exe,
For python 2.6, the comment is – #!c:\Python26\python.exe
- The second line describes the content type – ‘text/html’
- The third line prints out the sentence ‘Hello World’ - Save this file as ‘test.py’ under document root. (C:\Program Files\Apache Software Foundation\Apache2.2\htdocs in my case)
- Type the following url in your browser-
http://localhost/test.py
- If everything goes fine – you will see ‘Hello World’ on the screen.
TROUBLESHOOTING
Where to see causes for errors
The errors are logged here – C:\Program Files\Apache Software Foundation\Apache2.2\logs\error.log. This is the best place to start debugging.
——————————————————————————————————–
I want to place my python files inside a subdirectory under root directory
Say your root directory is ‘htdocs’. You create a directory named ‘sub’ under it. Save the ‘test.py’ file under ‘sub’.
Now type the following url in browser – http://localhost/sub/test.py. You will see ‘Hello, World’
——————————————————————————————————–
I get this error message on the browser -
Forbidden,
You don’t have permission to access /test.py on this server.
First check if you have Options ExecCGI set for htdocs
<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"> Options ExecCGI Order allow,deny Allow from all SetHandler cgi-script </Directory>
If that is present, and you still have error -
Check if there is some other Option that is overriding Options ExecCGI, eg. Options Indexes FollowSymLinks
If there are 2 Options directives, Apache overrides the first one, and only implements the second. If there are 2 directives one below the other eg.
Options ExecCGI Options Indexes FollowSymLinks
Here Options Indexes FollowSymLinks would override Options ExecCGI. To fix this add a plus sign in front of Options ExecCGI
Options Indexes FollowSymLinks
Options +ExecCGI
This way both the options will be in effect.
——————————————————————————————————-
The page looks like this -
<br />#!C:\Python27\python.exe<br />print "Content-Type: text/html\n\n"<br />print 'Hello, world!\n'<br />
Make sure that ‘SetHandler cgi-script’ is set in httpd.conf for the parent directory.
<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs> Options ExecCGI Order allow,deny Allow from all SetHandler cgi-script </Directory>
——————————————————————————————————–
I see the following message in ‘error.log’ -
Bad file descriptor: don’t know how to spawn child process: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test.py
Make sure that this line
#!C:\Python27\python.exe – is the first line in test.py, and there are no empty lines above.
——————————————————————————————————–
I see this error message in the error log
Options ExecCGI is off in this directory: C:/Program Files /Apache Software Foundation/Apache2.2/htdocs/test.py
First check if you have Options ExecCGI set for htdocs
<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"> Options ExecCGI Order allow,deny Allow from all SetHandler cgi-script <Directory>
Then check if there is some other Option that is overriding Options ExecCGI, eg. Options Indexes FollowSymLinks
If there are 2 Options directives, Apache overrides the first one, and only implements the second. If there are 2 directives one below the other eg.
Options ExecCGI Options Indexes FollowSymLinks
then Options Indexes FollowSymLinks would override Options ExecCGI. To fix this add a plus sign in front of Options ExecCGI
Options Indexes FollowSymLinks Options +ExecCGI
This way both the options will be in effect.
—————————————————-
Good Resource for Apache
Apache Server Frequently Asked Questions
[...] For this, refer to my blog entry How to configure Apache to run Python on Windows [...]