How to configure Apache to run Python on Mac (iOS)

Step 1: configure httpd.conf

change

<Directory "/Applications/XAMPP/xamppfiles/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

to

<Directory "/Applications/XAMPP/xamppfiles/cgi-bin"> 
    AddHandler cgi-script .cgi .py
    AllowOverride All
    Options +Indexes +FollowSymLinks +ExecCGI
    Order allow,deny 
    Allow from all 
    Require all granted 
</Directory>

If no “Require all granted”, it will show “Access Forbidden” when you visit your file on browser.

Step 2: put your python file e.g. test.py in the folder cgi-bin

Example is as below:

#!/usr/bin/python

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

print "Hello, world!"

use sudo chmod 755 test.py to make it executable.

Step 3: then go to browser, type “http://localhost/cgi-bin/test.py”, you will see “Hello World”.

Please don’t forget “Content-type: text/html\n”, otherwise it will show:

Server error!

The server encountered an internal error and was unable to complete your request.

Error message:
malformed header from script ‘test.py’: Bad header: Hello, world!

In addition, you may need to change the permission of cgi-bin and test.py.

The commands are: sudo chmod 775 cgi-bin

and: sudo chmod 755 cgi-bin/test.py

 

Leave a Reply

Your email address will not be published. Required fields are marked *