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.
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.
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
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
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
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
Once everything is ready, you can now start working with Python and build your web-application.
Here are some great examples:
How To Deploy Flask Web Applications
How To Structure Large Flask Applications
How To Use the Pyramid Framework To Build Your Python Web App
How to Deploy Pyramid Based Python WSGI Web-Applications
How To Use the Web2py Framework to Quickly Build Your Python App
How To Use the Bottle Micro Framework to Develop Python Web Apps
How to Set Up and Install Django CMS How To Install Django CMS Version 3 Beta 3
How To Install And Get Started With Django-Based Mezzanine CMS
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:
How to Deploy Python WSGI Apps Using Gunicorn HTTP Server Behind Nginx
How to Deploy Python WSGI Applications Using uWSGI Web Server with Nginx
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.
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!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
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.
@javier.collado: Thanks, I’ve updated the tutorial.
The new URL for get-pip.py seems to be: https://bootstrap.pypa.io/get-pip.py