Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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
</Directory>

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 127.0.0.1] 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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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