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.
A fresh Debian 10 VPS on the Atlantic.net Cloud Platform
A root password configured on your server
Step 1 – Create an Atlantic.Net Cloud Server
First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Debian 10 as the operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.
Once you are logged in to your Debian 10 server, run the following command to update your base system with the latest available packages.
apt-get update -y
Step 2 – 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 3 – 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:
Once all the packages are installed, restart the Apache service to apply the changes.
systemctl restart apache2
Step 4 – 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:
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:
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:
Next, restart the Apache service to apply the changes.
systemctl restart apache2
Step 5 – 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:
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.