# 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](https://www.ionos.co.uk/digitalguide/websites/web-development/python-tutorial/ "Python tutorial"), 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](https://www.ionos.co.uk/digitalguide/websites/website-creation/learn-php-our-all-encompassing-php-tutorial-for-beginners/ "Learn PHP: our all-encompassing PHP tutorial for beginners"). 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.

## `mod_wsgi` vs `mod_python`

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:

```none
sudo yum update
```

Install mod\_wsgi with the command:

```none
sudo yum install mod_wsgi
```

Restart Apache:

```none
sudo systemctl restart httpd
```

Verify that the module is loaded:

```none
sudo httpd -M | grep wsgi
```

The server will respond with:

```none
[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:

```none
sudo mkdir /var/www/python
```

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

```none
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:

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

Put the following content into this file:

```none
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:

```none
sudo systemctl restart httpd
```

## Create a test script

We will use the official recommended [mod\_wsgi Hello World test script](https://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html "mod_wsgi Hello World test script") for this example.

Create the file and open it for editing:

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

Put the following content into this file:

```none
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:

```none
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!”


This is a markdown version of: [https://www.ionos.co.uk/digitalguide/websites/web-development/use-mod-wsgi-to-run-python-as-a-web-application-on-centos-7/](https://www.ionos.co.uk/digitalguide/websites/web-development/use-mod-wsgi-to-run-python-as-a-web-application-on-centos-7/) for AI/LLM consumption.