How to Install mod_wsgi on CentOS


install mod_wsgi on centos

mod_wsgi is a useful Apache module that allows you to host Python applications in Apache web server. It supports python applications built using all popular Python frameworks such as Django, Pylon, Flask, etc. Here’s how to install mod_wsgi on CentOS.

 

 

How to Install mod_wsgi on CentOS

Here are the steps to install mod_wsgi on CentOS.

 

1. Update system & packages

Open terminal and run the following command to update system packages


$ sudo yum update

 

2. Install mod_wsgi

Install mod_wsgi with the command


$ sudo yum install mod_wsgi

 

3. Restart Apache Server

Restart Apache web server to apply changes


$ sudo systemctl restart httpd

 

4. Verify Installation


$ sudo httpd -M | grep wsgi

 

You’ll see the following output


$ sudo httpd -M | grep wsgi
wsgi_module (shared)

 

5. Configure mod_wsgi

Let us create a sample python script to configure mod_wsgi. We’ll save it in a separate folder.


$ sudo mkdir /var/www/python

Change its ownership so that apache can access this folder


$ sudo chown apache:apache /var/www/python

 

6. Create Test Script

Create a test python script using a text editor


$ sudo nano /var/www/python/helloworld.py

 

Add the following code to it. Please ensure they are properly indented as shown. Otherwise, python will throw an error


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

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

  return [output]

 

Save and close the file

 

Change the owner of this file so Apache can access it


$ sudo chown apache:apache /var/www/python/helloworld.py

 

7. Configure Apache

We use an WSGIScriptAlias to configure the Apache to run the script.

Create an Apache config file


$ sudo nano /etc/httpd/conf.d/helloworld.conf

 

Add the following lines to it


WSGIScriptAlias /helloworld /var/www/python/helloworld.py

<Directory /var/www/python/>
Order allow,deny
Allow from all
</Directory>

 

Save and close the file


$ sudo systemctl restart httpd

 

8. Test mod_wsgi

Open your web browser and visit http://example.com/helloworld, replace example.com with your domain name or server IP address.

You will see “Hello World!” message.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!