How To - Mod_wsgi Module With Apache To Host Python In CentOs

This article will teach you how to use mod_wsgi module with Apache to host python web server in Centos 6.4.Mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python 2.3+ based web applications under Apache.It is written in C codes.So it have better performance and lower memory overhead than mod_python or others.

apache module

The following paragraph will show the steps to use mod_wsgi.

Step 1. Install mod_wsgi

Run command:

[root@localhost]yum install mod_wsgi

Step 2. Configure Apache with mod_wsgi

Modify the /etc/httpd/conf/httpd.conf file.Add below line in the LoadModule section:

AddModule wsgi_module modules/mod_wsgi.so

Restart httpd service:

[root@localhost]service httpd stop
[root@localhost]service httpd start

Maybe you don't need to modify httpd.conf file,if there is /etc/httpd/conf.d/wsgi.conf.

Step 3. Create web access directory and python file

For example,create /usr/local/wsgi/scripts directory and add wsgi-myapp.py file in it.
File content is as follows:

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]

step 4. Create Apache conf file

Create a file named myapp.conf and put it in the /etc/httpd/conf.d/ directory.The file content is:

Alias /myapp “/usr/local/wsgi/scripts/wsgi-myapp.py”

Options ExecCGI
SetHandler wsgi-script
Order allow,deny
Allow from all

Step 5. Check the result

Restart httpd service.Then open firefox and input address”localhost/myapp”,you will see
“Hello world” in the web page.

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.