How To Set Up Ubuntu Cloud Servers For Python Web-Applications
How To Set Up Ubuntu Cloud Servers For Python Web-Applications
We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Set Up Ubuntu Cloud Servers For Python Web-Applications

Posted Feb 25, 2014 28k views Getting Started Python Ubuntu


Cloud computing is shaping the entire information technology (IT) industry and Ubuntu is right on it -- literally! With provided support for a very large array of various tools, libraries, and applications, coupled with ease of use and the way it makes hard tasks become simple, this trusted Linux distribution has become the go-to choice for many on its way of taking over the public cloud space.

In this DigitalOcean article, we are going to learn how to prepare an Ubuntu cloud server from scratch to host Python web-applications. By following this tutorial, you will have a solid Ubuntu installation, equipped with almost all necessary tools to deploy your Python project.

Ubuntu 13.10 (Release date: October 2013)

We will begin with updating the system, downloading and installing necessary tools and libraries we are going to need [for Python], then creating the perfect deployment environment for your web-application.

Updating The System

The first thing to do is to put everything that is already shipped with Ubuntu up to date.

Let's update the software sources list and then upgrade the outdated applications:

# Update the applications sources list:
aptitude    update

# Upgrade the outdated applications on the system:
aptitude -y upgrade

Getting The Necessary System Tools And Libraries

Next, let's download and install necessary tools and libraries which will be either used directly or come handy in the future.

Run the following command to install build-essential package required for compiling applications' source code:

aptitude install -y build-essential 

Next, whichever version control tool you need for development:

aptitude install -y cvs subversion git-core mercurial

In order to get some Python applications work, we need certain Python packages as well:

aptitude install python-setuptools python-dev \
                 python2.7-dev python-software-properties \

And finally, some third-party libraries required for various other things, such as image processing:

aptitude install libtiff4-dev libjpeg8-dev \
                 zlib1g-dev libfreetype6-dev liblcms2-dev \
                 libwebp-dev tcl8.5-dev tk8.5-dev

Getting The Common Python Tools

On Ubuntu and Debian, a recent version of Python interpreter - which you can use - comes by default. It leaves us with only a limited number of additional packages to install:

  • pip (to manage packages);

  • virtualenv (to create isolated, virtual environments).

Run the following commands to install pip:

# Get pip's dependency:
curl | python -

# And then pip:
curl | python -

# Finally, let's add it the PATH variable:
export PATH="/usr/local/bin:$PATH"

It is best to contain a Python application within its own environment together with all of its dependencies. An environment can be best described (in simple terms) as an isolated location (a directory) where everything resides. For this purpose, a tool called virtualenv is used.

Run the following to install virtualenv using pip:

pip install virtualenv  

Creating An Environment For Your Python Web-Application

Note: Instructions given here are kept brief. To learn more, check out our how-to article on pip and virtualenv: Common Python Tools: Using virtualenv, Installing with Pip, and Managing Packages.

Run the following commands to create a virtual environment for your python web-application:

# Create a virtual environment:
# Usage: virtualenv [main app. directory name]
# Example:
virtualenv django_app

# Enter the directory to start working with
# the Python interpreter:
# Example:
cd django_app

Afterwards, in order to work with the isolated Python interpreter and pip the package manager, either activate the environment, e.g.:

# Activate the *virtualenv*:
source bin/activate

Or call the Python interpreter directly whenever you need to work with the application located inside this environment, e.g.:

# Call the Python interpreter directly:
# Usage: bin/python [command]
# Example:
bin/python runserver

Installing Your Application

Once everything is ready, you can now start working with Python and build your web-application.

Here are some great examples:

  • Flask:

How To Deploy Flask Web Applications

How To Structure Large Flask Applications

  • Pyramid:

How To Use the Pyramid Framework To Build Your Python Web App

How to Deploy Pyramid Based Python WSGI Web-Applications

  • Web2py:

How To Use the Web2py Framework to Quickly Build Your Python App

  • Bottle:

How To Use the Bottle Micro Framework to Develop Python Web Apps

  • Django CMS:

How to Set Up and Install Django CMS
How To Install Django CMS Version 3 Beta 3

  • Mezzanine:

How To Install And Get Started With Django-Based Mezzanine CMS

Getting Ready For Production

The test [application] server which comes with Django is not suitable for actual production scenarios. Before putting your application online, you should download and install a suitable Python application server and configure it. However, do not be afraid as it is truly a remarkably easy process.

In order to decide which application server might suit your needs the best, check out our article: A Comparison of Web Servers for Python Based Web Applications.

Once you decide, go through any one of the following tutorials to get your Mezzanine application deployed online in a solid and reliable way:

  • Gunicorn:

How to Deploy Python WSGI Apps Using Gunicorn HTTP Server Behind Nginx

  • uWSGI:

How to Deploy Python WSGI Applications Using uWSGI Web Server with Nginx

  • CherryPy:

How to Deploy Python WSGI Applications Using CherryPy Web Server Behind Nginx

If you are more used to working with Apache or would like to keep it for some reason, you can opt for replacing Nginx with it. In order to see how to use your current Apache installation as a reverse-proxy to any one of the above application servers, check out our article: How To Use Apache HTTP Server As Reverse-Proxy Using mod_proxy Extension.

Submitted by: O.S. Tezer


Creative Commons License