Use mod_wsgi to run Python as a web application on CentOS 7

Learn how to install and use Apache's mod_wsgi module to run Python scripts in a web page. This Apache module can be used to serve web pages written in Python, or to render web pages with embedded Python scripts.

mod_wsgi is a particularly good choice for web developers who are accustomed to the way Apache handles PHP. If you want the power and flexibility of Python, but you want it to work like PHP on the web, mod_wsgi is a simple answer.

Requirements

  • A Cloud Server running Linux (CentOS 7).
  • Apache installed and running.
  • A basic familiarity with Python.
VPS Hosting
Fully virtualized servers with root access
  • Unlimited traffic
  • Fast SSD NVMe storage
  • Free Plesk Web Host Edition

<code>mod_wsgi</code> vs <code>mod_python</code>

Many users are confused about the difference between mod_wsgi and mod_python. Both Apache modules have roughly the same effect: They let you run Python modules in a web page.

Although mod_python has a more robust set of features, mod_wsgi is under far more active support and development. Therefore, we recommend mod_wsgi for most users.

Install mod_wsgi

Update your system:

sudo yum update

Install mod_wsgi with the command:

sudo yum install mod_wsgi

Restart Apache:

sudo systemctl restart httpd

Verify that the module is loaded:

sudo httpd -M | grep wsgi

The server will respond with:

[user@localhost ~]# sudo httpd -M | grep wsgi
wsgi_module (shared)

Configure Apache

For security reasons, the Python scripts should be stored in a directory which is not available on the web. Create this directory:

sudo mkdir /var/www/python

Set Apache as the owner of this directory, so that it can access the files:

sudo chown apache:apache /var/www/python

We will use WSGIScriptAlias to configure an alias to the script. Access rights will also need to be granted to the directory where the script is located.

Create an Apache configuration file for an example "Hello World" script, and open it for editing:

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

Put the following content into this file:

WSGIScriptAlias /helloworld /var/www/python/helloworld.py
Directory /var/www/python/
Order allow,deny
Allow from all
Directory

Save and exit the file. Then restart Apache:

sudo systemctl restart httpd
Cloud Backup powered by Acronis
Mitigate downtime with total workload protection
  • Automatic backup & easy recovery
  • Intuitive scheduling and management
  • AI-based threat protection

Create a test script

We will use the official recommended mod_wsgi Hello World test script for this example.

Create the file and open it for editing:

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

Put the following content into this file:

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 exit the file. Then set Apache as the owner of this file, so that it can be accessed:

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

View this file in a browser at the URL http://example.com/helloworld. You will see the message "Hello World!"

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top