Run python on Ampps (windows)

AMPPS is a WAMP, MAMP and LAMP stack of Apache, MySQL, MongoDB, PHP, Perl & Python. Having said that you need to do couple of following steps to run python scrips, but first make take a backup of the conf file before you make any changes.

  1. Open httpd.conf ( Apache > configuration )
  2. Include the AddHandler with .py files to the .conf file so that apache knows that .py belongs to that group of file types that needs to be processed by special ‘handler’
    • eg. AddHandler cgi-script .cgi .pl  becomes AddHandler cgi-script .cgi .pl .py
  3. Add ExecCGI to the “Options Indexes” line in the .conf. I added the line inside <Directory “{$path}/www”> block. ExecCGI (Execution of CGI) is necessary for the apache server to assign .py file to its handler. In our case, the python interpreter.
    • eg. Options Indexes FollowSymLinks becomes Options Indexes FollowSymLinks ExecCGI 
  4. After steps 1-3, save the httpd.conf file, stop the apache server and restart again.
  5. If the apache server restarts without a problem process you have successfully setup ampps to run python, otherwise restore the conf file from backup. In ampps there is an option to restore default configuration.
  6. There are couple of things to consider before you run your first python script on apache.
    • Add a shebang line to the top of the python file to direct to the Python interpreter. The path should be a direct link to the python executable. For example on windows, it might be
      1. #!C:\Users\Laxman\Anaconda2\python.exe
    • After shebang line, make sure the main python script has print(“Content-type: text/html\n”). This is very important for a CGI script. The test python file would be similar as below
      • #!C:\Users\Laxman\Anaconda2\python.exe
        print(“Content-type: text/html\n”)
        print(‘Hi there!’

wsgi_module must be installed for apache to run python script via execCGI. On ampps this is installed and added to conf file by default.