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.

    vServer (VPS) from IONOS

    Low-cost, powerful VPS hosting for running your custom applications, with a personal assistant and 24/7 support.

    100 % SSD storage
    Ready in 55 sec.
    SSL certificate

    <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
    
    &lt;Directory /var/www/python/&gt;
    Order allow,deny
    Allow from all
    &lt;/Directory&gt;

    Save and exit the file. Then restart Apache:

    sudo systemctl restart httpd

    Cloud backup from IONOS

    Make costly downtime a thing of the past and back up your business the easy way!

    Simple
    Secure
    Integrated

    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!"


    Own your online success
    IONOS takes care of your online challenges
    Create your website with the perfect domain and the UK's fastest hosting!