Configuring WSGI on Ubuntu/Centos
- What Is WSGI (Web Server Gateway Interface)
- Configuring WSGI on Ubuntu/Centos
- Getting Started With Flask
This article outlines the process of configuring a Dedicated server for Python 3 web applications with Apache 2.4 using mod_wsgi.
What is mod_wsgi?
Mod_wsgi is an Apache module that allows Python web applications to function on a server. This module provides a web framework for Flask, Django, and other Python based frameworks to operate within a clustered server environment on a group of servers.
Prerequisites
- A CentOS VPS or Ubuntu cloud dedicated server
- Apache 2.4, apache-dev/httpd-devel and python3-dev installed
- Python 3, pip3, and virtualenv installed
- Access to a terminal
If you do not have the above prerequisites installed, this build will not work as expected. The following commands can be used to install the prerequisites
apt install apache2 apache2-dev python3 python3-dev python3-pip
pip3 install virtualenv
Step 1
Begin by downloading the source files for mod_wsgi.i
root@host [~]# wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.5.tar.gz
Next, unpack the archive
root@host [~]# tar -xzvf 4.6.5.tar.gz
Enter the source files directory
root@host [~]# cd mod_wsgi-4.6.5
Next, determine the path to the Python interpreter.
root@host [~]# which python3
/usr/bin/python3
root@host [~]#
Configure the installation and define the Python interpreter for mod_wsgi.
root@host [~]# ./configure --with-python=/usr/bin/python3
checking for apxs2... no
checking for apxs... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/src/mod_wsgi-4.6.5':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
Now, run the following commands to compile the package.
root@host [~]# make && make install
The path to where the module was installed will be at the end of the output from the last command.
root@host [~]# /usr/lib/apache2/modules/mod_wsgi.so
Use the path noted above to load the module into Apache by adding the following snippet to the bottom of the apache.conf or httpd.conf
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
Now test the syntax and restart the Apache service on the cloud server.
Debian/Ubuntu based systems:
root@host [~]# apache2 -t
root@host [~]# service apache2 restart
RHEL/CentOS based systems:
root@host [~]# httpd -t
root@host [~]# service httpd restart
Step 2
Now that mod_wsgi has been installed, let’s take a look at the configuration. We will begin by creating a basic Python web application to detail the steps to configure mod_wsgi.
First, let's create a virtual environment using the Python version that mod_wsgi was configured for. For this article, the example application will be placed in /var/www/. Next, we will run this command to create the directory wsgi_test, which will contain the virtual environment.
root@host [~]# virtualenv --python=python3 wsgi_test
Next, enter this directory and activate the virtualenv.
root@host [~]# cd wsgi_test
root@host [~]# source bin/activate
root@host [~]# pip install flask
root@host [~]# touch wsgi_test.py
root@host [~]# vim wsgi_test.py
Place the following configuration inside of the wsgi_test.py file
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello, from WSGI"
Now, let's create the wsgi file to load the application.
root@host [~]# touch wsgi.py
root@host [~]# vim wsgi.py
Next, add the following information to the file. This data will activate the virtual environment when a request is received and execute the application.
activate_this = '/var/www/wsgi_test/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
import sys
sys.path.insert(0, '/var/www/wsgi_test/')
from wsgi_test import app as application
Now that the example application has been created, let’s configure Apache.
We will begin by creating a virtual host for this application. This file will be placed inside of either
/etc/httpd/conf.d/ for RHEL based systems, or
/etc/apache2/sites-enabled/for Debian based.
Redhat
For RHEL based systems, write the file to the following path replacing domain.com with your domain.
/etc/httpd/conf.d/domain.com.conf
Debian
For Debian based systems, write the file to the following path, replacing domain.com with your domain.
/etc/apache2/sites-enabled/domain.com.conf
The contents of the file will be as follows, replacing domain.com with your domain.
<VirtualHost *:80>
ServerName domain.com
WSGIDaemonProcess wsgi_test user=www-data group=www-data threads=2
WSGIScriptAlias / /var/www/wsgi_test/wsgi.py
<Directory /var/www/wsgi_test>
Require all granted
</Directory>
</VirtualHost>
For RHEL based systems, replace the user and group with apache as follows:
WSGIDaemonProcess wsgi_test user=apache group=apache threads=2
Now test and restart Apache.
Debian based systems:
root@host [~]# apache2 -t
root@host [~]# service apache2 restart
RHEL based systems:
root@host [~]# httpd -t
root@host [~]# Service httpd restart
You can also use the curl command via the CLI
root@host [~]# curl http://domain.com
or navigate directly to the domain in a browser to see your application. Any errors will be logged to Apache’s default error log file usually found in /var/log/apache2/error_log
Ready to get started?
Liquidweb offers multiple hosted private cloud server options for setting up and configuring this type of environment to run your apps. Reach out to one of our Solutions Team members today, and they can put together a hosting package that will definitely meet your specific needs.
Related Articles:
- How to Edit the PHP Memory for Your WordPress Site via WP Toolkit
- 4 Methods for How to Install Yarn on Windows Server
- How to Install Bpytop Resource Monitoring Tool on AlmaLinux
- How to Fix “This Site Can’t Provide a Secure Connection” Error
- How to Install MongoDB on AlmaLinux
- How to Install PostgreSQL on AlmaLinux

About the Author: John Long
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
How to Edit the PHP Memory for Your WordPress Site via WP Toolkit
Read ArticleWhat is CGI-Bin and What Does it Do?
Read ArticleTop 10 Password Security Standards
Read ArticleTop 10 Password Security Standards
Read ArticleHow to Use the WP Toolkit to Secure and Update WordPress
Read Article