You are here

8 November 2015
Using Python in the web. Setup Apache the way that you can run Python backend on it.

Apache

You need mod_wsgi to run Python based web applications on top of the Apache web server.
Source code viewer
  1. # Add the following line to "/etc/httpd/conf/httpd.conf" or wherever your apache configuration file is.
  2. LoadModule wsgi_module modules/mod_wsgi.so
Programming Language: Apache configuration
Add to your vhosts.conf file, between "DirectoryMatch" tags:
Source code viewer
  1. Options ExecCGI
  2. AddHandler wsgi-script .wsgi
Programming Language: Text

Python

Create index.wsgi to your root directory and put this code into it.
Source code viewer
  1. def wsgi_app(environ, start_response):
  2. output = "<html><body><h1>WSGI working!</h1></body></html>\n".encode('utf-8')
  3. status = '200 OK'
  4. headers = [('Content-type', 'text/html'),
  5. ('Content-Length', str(len(output)))]
  6. start_response(status, headers)
  7. yield output
  8.  
  9.  
  10. # mod_wsgi needs the "application" variable to serve our small app
  11. application = wsgi_app
Programming Language: Python

Test

See if "http://localhost/index.wsgi" gives out "WSGI working!".