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.

Getting Started With Flask

Reading Time: 1 minute

What is Flask?

Flask is a micro web framework for Python that allows unlimited possibilities to the structure and format for building powerful web applications. This article demonstrates how to get started with Flask using Python 3.7 inside of a virtual environment.

Flask_logo
Continue reading “Getting Started With Flask”

How To Install VIM (Vi IMproved) on Fedora 23

Reading Time: 1 minute

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

Pre-Flight Check

  • These instructions are intended specifically for installing VIM (Visual editor IMproved or Vi IMproved), an open-source text editor, on Fedora 23.
  • We’ll be working as root on a Liquid Web Self Managed Fedora 23 server. If you need to install vim on Fedora 22, visit this tutorial.
  • If you’ve received the error “-bash: vim: command not found” while following any of our other tutorials, following the instructions below will resolve it.

Step #1: The Installation

As a matter of best practice we’ll update our packages:

dnf -y update

Then we’ll install VIM (Visual editor IMproved) and any required packages:

dnf -y install vim-enhanced

Step #2: Verify the Application is Available

Now verify that the application is available by using the following command:

vim

Running that code should result in output similar to the following:

~ VIM - Vi IMproved
VIM - Vi IMproved
~
~ version 7.4.827
~ by Bram Moolenaar et al.
~ Modified by <bugzilla@redhat.com>
~ Vim is open source and freely distributable
~
~ Help poor children in Uganda!
~ type :help iccf for information
~
~ type :q to exit
~ type :help or for on-line help
~ type :help version7 for version info

Step #3: Learn VIM!

For a refresher on editing files with vim, see New User Tutorial: Overview of the Vim Text Editor.

How to Install VIM (Visual editor IMproved) on Fedora 21

Reading Time: 1 minute

Note:
Please note that this article is considered legacy documentation because Fedora 21 has reached its end-of-life support.
Pre-Flight Check
  • These instructions are intended specifically for installing VIM (Visual editor IMproved or Vi IMproved), an open-source text editor, on Fedora 21.
  • I’ll be working from a Liquid Web Self Managed Fedora 21 server, and I’ll be logged in as root.

Continue reading “How to Install VIM (Visual editor IMproved) on Fedora 21”

How to Install VIM (Visual editor IMproved) on Fedora 22

Reading Time: 1 minute

Pre-Flight Check
  • These instructions are intended specifically for installing VIM (Visual editor IMproved or Vi IMproved), an open-source text editor, on Fedora 22.
  • I’ll be working from a Liquid Web Self Managed Fedora 22 server, and I’ll be logged in as root.

Continue reading “How to Install VIM (Visual editor IMproved) on Fedora 22”

Recommended Production Settings for Cassandra on CentOS 6

Reading Time: 1 minuteIf you’ve already followed the tutorial on How To Install Cassandra on CentOS 6, then you’re ready for another step!

Pre-Flight Check
  • These instructions are intended for Cassandra 2 on a single CentOS 6 server.
  • I’ll be working from a Liquid Web Core Managed CentOS 6.4 server, and I’ll be logged in as root.

Continue reading “Recommended Production Settings for Cassandra on CentOS 6”

New User Tutorial: Overview of the Vim Text Editor

Reading Time: 2 minutesMany articles in this knowledge base advise editing configuration files. We usually recommend using your preferred text editor. At Liquid Web, we prefer vim. Vim, or its older sibling vi, are installed by default on nearly every server that we come across. If you do not yet have a preferred text editor, read on to see if vim might work for you.

Continue reading “New User Tutorial: Overview of the Vim Text Editor”