Configuring WSGI on Ubuntu/Centos

Reading Time: 3 minutes

This article outlines the process of configuring a 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 production environment on a server.

Prerequisites

  • A CentOS or Ubuntu 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 Apache.

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 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.

You can also give us a call 24 hours a day 1.800.580.4985 or open a chat or ticket with us.

How to Install Python on Windows

Reading Time: 2 minutesPython is a popular programming language for developing applications. The Python design philosophy emphasizes code readability and focuses on clear programming for both small and large-scale projects. Python allows you to run modules and full applications from a large library of resources (or even applications you write yourself) on your server. Python works on a number of popular operating systems, including Windows Server OS.

Continue reading “How to Install Python on Windows”

How to Install PIP on Windows

Reading Time: 2 minutesOne of the best tools to install and manage Python packages is called Pip. Pip has earned its fame by the number of applications using this tool. Used for its capabilities in handling binary packages over the easy_installed packaged manager, Pip enables 3rd party package installations. Though the newest versions of Python come with pip installed as a default, this tutorial will show how to install Pip, check its version, and show some basic commands for its use. Watch the video or see the rest of the article for written instructions.

Continue reading “How to Install PIP on Windows”

How to Install Cassandra on Ubuntu 16.04 LTS

Reading Time: 3 minutesApache Cassandra is a free open-source database system that is NoSQL based. Meaning Cassandra does not use the table model seen in MySQL, MSSQL or PostgreSQL, but instead uses a cluster model. It’s designed to handle large amounts of data and is highly scalable. We will be installing Cassandra and its pre-requisites, Oracle Java, and if necessary the Cassandra drivers. Continue reading “How to Install Cassandra on Ubuntu 16.04 LTS”

How to Install Pip on Ubuntu 16.04 LTS

Reading Time: 2 minutesArguably one of the easiest tools to use for installing and managing Python packages, Pip has earned its notoriety by the number of applications utilizing this tool. Fancied for its capabilities in handling binary packages over the easy_installed packaged manager, pip enables 3rd party package installations. Though Python does sometimes come with pip as a default, this tutorial will show how to install, check its version as well as some basic commands for using pip on Ubuntu 16.04.

Continue reading “How to Install Pip on Ubuntu 16.04 LTS”

How To Install Pip on Fedora 23

Reading Time: < 1 minute

Note:
Please note that this article is considered legacy documentation because this OS has reached its end-of-life support.

Pip is a tool for installing and managing packages for Python. The popular Python programming language is considered one of the easiest to learn because of its emphasis on code readability.

Continue reading “How To Install Pip on Fedora 23”

How to Install Pip on Fedora 21

Reading Time: < 1 minute

Note:
Please note that this article is considered legacy documentation because this OS has reached its end-of-life support.

Pip is a tool for installing and managing Python packages.

Python is a programming language. It is quite popular and has a design philosophy that emphasizes code readability. It is widely considered to be a very easy programming language to learn and master because of its focus on readability. Python is open source, and will run a multitude of platforms including, but not limited to: Various Linux/UNIX distributions (CentOS, Ubuntu, Fedora, Debian, etc.), Microsoft Windows, and Mac OS X.

How to Install Pip on Fedora 20

Reading Time: < 1 minute

Note:
Please note that this article is considered legacy documentation because this OS has reached its end-of-life support.

Pip is a tool for installing and managing Python packages.

Python is a programming language. It is quite popular and has a design philosophy that emphasizes code readability. It is widely considered to be a very easy programming language to learn and master because of its focus on readability. Python is open source, and will run a a multitude of platforms including, but not limited to: Various Linux/UNIX distributions (CentOS, Ubuntu, Fedora, Debian, etc.), Microsoft Windows, and Mac OS X.

Continue reading “How to Install Pip on Fedora 20”