I need help setting up a local Apache2 to run Python. Got it to work just fine with html, php and mysql on my Mac running Mountain Lion.

Python running. Installed mod_wsgi through MacPorts and checked that it got loaded by Apache after adding the following to the httpd.conf:

LoadModule wsgi_module modules/mod_wsgi.so
<Directory /opt/local/apache2/htdocs>
AddHandler wsgi-script .py
Options +ExecCGI
Order deny,allow
Allow from all

Put my index.py file into htdocs:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

Gave chmod 777 permission to whole path /opt/local/apache2/htdocs/index.py

No matter what I do, I keep getting the "Forbidden. You don't have permission to access /index.py on this server." when going to localhost/index.py.

The Apache error-log says:

[Thu Aug 30 17:46:46 2012] [error] [client] Options ExecCGI is off in this directory: /opt/local/apache2/htdocs/index.py, referer: http://localhost/

How can I resolve this permission issue? What is Options ExecCGI?


Try using .wsgi extension instead of .py. You likely have a conflicting definition in your Apache setup somewhere that says that .py is a CGI script. Also use WSGIScriptAlias in preference to AddHandler method for setting up mod_wsgi. See the documentation:

http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines

  • You were right about the conflicting definition. I moved the <Directory /opt/local/apache2/htdocs>-stuff down to the bottom of the httpd.conf and it worked. Thank you! Why is it better to use WSGIScriptAlias instead of AddHandler? – MartinR Aug 31 '12 at 10:28
  • Because WSGIScriptAlias allows you to more easily mount stuff at root of web site. Use AddHandler and a part of your file system path and name of WSGI script file has to be in the URL. WSGIScriptAlias allows you to arbitrarily specify the mount point to what you want to expose it to. Also, use AddHandler and every WSGI script by default results in separate sub interpreter in the process which means more memory. You would be better of using a framework and do it all together rather than as separate scripts. – Graham Dumpleton Sep 1 '12 at 3:49

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.