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”

Python Web Applications In cPanel On CloudLinux

Reading Time: 2 minutesFunctionality for Python web applications can lead to increased profitability as a result of a broader offering of technologies to any current or future customers.

The Python Selector in cPanel on CloudLinux allows multiple versions of Python to run on a per-application basis. The versions of Python available to the selector is dependant on the installed Python versions. The steps below will outline installing all of the currently available versions of Python.

Continue reading “Python Web Applications In cPanel On CloudLinux”

How Do I Set System Variable Path for Python on Windows?

Reading Time: 2 minutesPython is a powerful programming language that is scalable and uses code that is readable and clear for all types of projects. Python is also available across a number of operating systems, making it a popular choice for developers. If you are using Python on your Windows operating system, you may need to adjust your System Environment Variables to simplify utilizing Python on your server.

The latest installers for Python for Windows can set the System Environment Variable Path automatically if you choose that option during the installation. To verify if this setting is correct, open an administrative command prompt (right-click on the command prompt and choose “run as administrator”) and type the word python, then press Enter. If the System Variable Path is correctly set, you should receive output similar to what is shown below.

python output screen

Note:
Python software installation is considered Beyond Scope Support. This means it is not covered under our managed support, but we will do what we reasonably can to assist. It may take longer for us to assist as the SLA for Beyond Scope Support is different than our managed services. Find out more in our article What Is Beyond Scope Support?

If you receive an error indicating that the command is unknown, but you’ve confirmed that Python is installed and can be launched from its directory, you will probably need to add the Python variables to the System Environment Variables. By following these quick steps, you’ll be able to access Python from any command prompt.

  1. From the server desktop, click the Windows icon and search for “Environment Variables”. Press Enter to launch the System Properties dialog.
    system variables launch link
  2. This will open the System Properties dialog. Click Environment Variables to make the necessary changes.
    system properties screenshot
  3. Create a new User Variable named: Path and Variable value: C:\Users\*yourusername*\AppData\Local\Programs\Python\Python37 (change the variable value to match your actual installation path).
    new path link highlighted
  4. Next, find the System variable called Path and click Edit.

    system variables screen, edit button highlighted

  5. Create a New entry that matches the Path variable that you created. Add \Scripts to the end of the entry (the new entry should look like C:\Users\*yourusername*\AppData\Local\Programs\Python\Python37\Scripts\
    edit system variable path screenshot
  6. You can verify that you have completed this successfully by opening a new administrative command prompt from any location and typing “python” (without the quotes). You should receive a response similar to what is shown above.

Creating a Virtual Environment for Python on Ubuntu 16.04

Reading Time: 2 minutesVirtualenv is a tool that creates an isolated environment separate from other projects. In this instance we will be installing different Python versions, including their dependencies.  Creating a virtual environment allows us to work on a Python project without affecting other projects that also use Python. It will utilize Python’s core files on the global environment to run, thus saving you disk space while providing the freedom to use different Python version for separate apps.

Continue reading “Creating a Virtual Environment for Python on Ubuntu 16.04”

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 Use Ansible

Reading Time: 8 minutesAnsible symbolAnsible is an easy to use automation software that can update a server, configure tasks, manage daily server functions and deploys jobs as needed on a schedule of your choosing. It is usually administered from a single location or control server and uses SSH to connect to the remote servers. Because it employs SSH to connect, it is very secure and, there is no software to install on the servers being managed. It can be run from your desktop, laptop or other platforms to assist with automating the tedious tasks which every server owner faces.

Continue reading “How to Use Ansible”

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

Using Conda for Alternate Python Installations

Reading Time: 3 minutesLet’s be honest: Most of the time, the Python version included with a Linux distro isn’t the newest. For example, at the time of this writing, the version of Python 2 included with our CentOS 7 Storm images is 2.7.5, and Python 3 isn’t even available out of the box. For reference (again, at the time of this writing), the latest versions are 2.7.14 and 3.6.4 respectively.

Continue reading “Using Conda for Alternate Python Installations”