Tutorial

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

Published on February 25, 2014
author

O.S. Tezer

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

Introduction


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 \
                 libpq-dev

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 https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py | python -

# And then pip:
curl https://bootstrap.pypa.io/get-pip.py | 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 manage.py runserver 0.0.0.0:8000

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.

<div class=“author”>Submitted by: <a href=“https://twitter.com/ostezer”>O.S. Tezer</a></div>

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
O.S. Tezer

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
5 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Thank you for this tutorial! While installing on Ubuntu 14.04 the following package was not found: zliblg-dev Hopefully it won’t break anything :)

questions:

1 - I just installed pip with aptitude install python-pip Is this a problem?

2 - should I be creating a non-root user to install Mezzanine? Everything I’ve read says that’s best practice, but this tutorial makes no mention of it.

I don’t know whether that’s because this tutorial follows a different security philosophy, or because the writer doesn’t understand security. I also do not know whether the tutorial relies on these commands being executed as root. As I said, this runs counter to what most tutorials recommend. Please advise.

I’m a Web Designer who’s been trying to learn how to use Mezzanine lately. I’m completely new to Python and to Web Development in general, and I’m facing difficulty with the simplest things, unfortunately. Things such as where to type in the [aptitude update] commands, and what directory should I be in (yeah, Linux noob) knowing that I don’t have permission for some reason to enter the root folder on my sharehosting. Do you recommend any article for beginners to help me out? Thanks for your great contributions.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 23, 2014

@javier.collado: Thanks, I’ve updated the tutorial.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more