Atlantic.Net Blog

How to Install Apache with Python Mod_wsgi on Debian 10

WSGI stands for “Web Server Gateway Interface,” which provides a standard and efficient method for dynamic web applications to communicate with web servers.  It is used for deploying applications written in Django, Web.py, Werkzug, and Flask. Mod_wsgi simplifies the WSGI application deployment on an Apache webserver. Mod_wsgi is an Apache webserver module that can be used to serve Python applications using the HTTP protocol.

In this tutorial, we will show you how to install Apache with Python Mod_wsgi on Debian 10.

Step 1 – Install Python

First, you will need to install Python in your system. You can install it by just running the following command:

apt-get install python3 libexpat1  -y

Once Python is installed, you can proceed to the next step.

Step 2 – Install Apache with mod_wsgi Module

Next, you will need to install the Apache webserver package and mod_wsgi Python module in your system. Run the following command to install them:

apt-get install apache2 apache2-utils ssl-cert libapache2-mod-wsgi -y

Once all the packages are installed, restart the Apache service to apply the changes.

systemctl restart apache2

Step 3 – Configure Apache with mod_wsgi Module

First, you will need to create a Python script in your Apache web root directory. You can create it with the following command:

nano /var/www/html/wsgi.py

Add the following lines:

def application(environ,start_response):
    status = '200 OK'
    html = '<html>\n' \
           '<body>\n' \
           '<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \
           'Welcome to Apache with mod_wsgi \n' \
           '</div>\n' \
           '</body>\n' \
           '</html>\n'
    response_header = [('Content-type','text/html')]
    start_response(status,response_header)
    return [html]

Save and close the file when you are finished.

Next, you will need to create an Apache virtual host configuration file to serve the Python script with HTTP protocol.

You can create it with the following command:

nano /etc/apache2/conf-available/mod-wsgi.conf

Add the following line:

WSGIScriptAlias /wsgi /var/www/html/wsgi.py

Save and close the file. Then, activate the virtual host configuration with the following command:

a2enconf mod-wsgi

Next, restart the Apache service to apply the changes.

systemctl restart apache2

Step 4 – Verify mod_wsgi Application

Now, open your web server and visit the URL http://your-server-ip/wsgi. You will be redirected to the following screen:

Conclusion

Congratulations! You have successfully deployed Python application with mod_wsgi and Apache on Debian 10. Easily deploy a Python application in the production environment on your VPS hosting account from Atlantic.Net.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year