How To Set Up Ubuntu Cloud Servers For Python Web-Applications
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 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:
- Django 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:
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.