I\'m trying to get Python scripts, called from a web browser, to work. I keep getting the error:
500 Internal Server Error
When I check my err
I tried many approaches to get Python working with Apache properly and finally settled with using Apache + mod_WSGI + web.py . It sounds like a lot, but it is much simpler than using the complicated frameworks like Django out there.
(You're right, don't bother with mod_python)
Note, I am using Apache2 , but mod_wsgi works on 1.3 as well, based on the modwsgi page.
If you are on Redhat, I believe you have yum, so make sure to get the apache wsgi module and other python packages:
$ yum update
$ yum install gcc gcc-c++ python-setuptools python-devel
$ yum install httpd mod_wsgi
And get web.py for your version of python. For example, using easy_install. I have v2.6.
$ easy_install-2.6 web.py
Create a directory for your python scripts : /opt/local/apache2/wsgi-scripts/
In your httpd.conf :
LoadModule wsgi_module modules/mod_wsgi.so
# note foo.py is the python file to get executed
# and /opt/local/apache2/wsgi-scripts/ is the dedicated directory for wsgi scripts
WSGIScriptAlias /myapp /opt/local/apache2/wsgi-scripts/foo.py/
AddType text/html .py
Order allow,deny
Allow from all
Note that web.py uses a "templates directory". Put that into the wsgi directory , /opt/local/apache2/wsgi-scripts/templates/
.
Create a file /opt/local/apache2/wsgi-scripts/templates/mytemplate.html
:
$def with (text)
Hello $text.
Add appropriate permissions.
$ chown -R root:httpd /opt/local/apache2/wsgi-scripts/
$ chmod -R 770 /opt/local/apache2/wsgi-scripts/
In your python file, foo.py :
import web
urls = ( '/', 'broker',)
render = web.template.render('/opt/local/apache2/wsgi-scripts/templates/')
application = web.application(urls, globals()).wsgifunc()
class broker:
def GET(self):
return render.mytemplate("World")
The above will replace the special web.py $text variable in the mytemplate with the word "World" before returning the result .
http://ivory.idyll.org/articles/wsgi-intro/what-is-wsgi.html