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 par­tic­u­lar­ly good choice for web de­vel­op­ers who are ac­cus­tomed to the way Apache handles PHP. If you want the power and flex­i­bil­i­ty of Python, but you want it to work like PHP on the web, mod_wsgi is a simple answer.

Re­quire­ments

  • A Cloud Server running Linux (CentOS 7).
  • Apache installed and running.
  • A basic fa­mil­iar­i­ty with Python.
VPS Hosting
VPS hosting at un­beat­able prices on Dell En­ter­prise Servers
  • 1 Gbit/s bandwidth & unlimited traffic
  • Minimum 99.99% uptime & ISO-certified data centers
  • 24/7 premium support with a personal con­sul­tant

mod_wsgi vs mod_python

Many users are confused about the dif­fer­ence 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 de­vel­op­ment. 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 WS­GIS­crip­tAl­ias 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 con­fig­u­ra­tion 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 pro­tec­tion
  • Automatic backup & easy recovery
  • Intuitive sched­ul­ing and man­age­ment
  • AI-based threat pro­tec­tion

Create a test script

We will use the official rec­om­mend­ed 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/hel­loworld. You will see the message "Hello World!"

Go to Main Menu