Learn how to create a production-ready Django web platform using cookiecutter-django, deployed to AWS Ubuntu 18.04 with Nginx, Gunicorn, Redis and MySQL. I’ve refined the following “best practice” comprehensive setup procedure to ensure that I get each project setup consistently, the right way, the first time.
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. As humbly claimed by its authors, it is “Ridiculously fast, Reassuringly secure, and Exceedingly scalable”. I’ve consistently found all three claims to be true.
This article will take you step by step thru creating a Continuous Integration loop for a commercial-grade Django site from which you can begin to iterate and develop your web project. Continuous Integration (CI) is the practice of merging all developers’ working copies to a shared mainline several times a day. To facilitate this, we will set up an efficient development environment, push our code to Github, and then create a way to automatically deploy code updates to our production environment.
For this article I am using a Macbook Pro running OS X 11, and I’ll be deploying to an AWS Ubuntu 18.04 EC2 instance configured with Nginx, Gunicorn, Redis and MySQL. Keep in mind however that none of these decisions has any material impact on the procedure that I’m presenting in this article.
1. Install Python
As of this writing there are two completely different and co-existing versions of Python: Python 2.7 and Python 3.x. The former will reach “end of life” in April-2020 and thus, we’ll be using Python 3.x for this article. It can be tricky to get your project setup with the correct version of Python depending on whether your development environment is on Windows, OS X or Linux, and also depending on whether or not a version of Python is already installed globally on your computer. I’ll point out how we check and double-check this, each stop along the way.
I use a Mac, and as it happens, recent versions of OS X ship with Python 2.7 pre-installed; not the version we want to use. Nonetheless, you can safely install multiple versions of Python on the same machine. I have both Python 2.7.16 and Python 3.7.1 on my Macbook Pro.
If you do not yet have Python version 3.x installed on your computer then you should take care of that now by referring to these instructions from The Hitchhiker’s Guide to Python.
2. Install PIP (Python Install Package)
Our journey begins with Pip (Python Install Package), the standard package manager for Python. Pip allows you to install and manage additional packages that are not part of the Python standard library. Pip provides a simple, clean means of adding (or removing) high-quality third party code libraries to your Django project. Django itself is installed with pip, which is why we have to begin by installing pip.
Your first step is to check to see if pip is already installed, and if so, what version are your running? In my case this is what I see:
$ pip --version pip 19.3.1 from /Users/mcdaniel/.local/share/virtualenvs/openstax-integrator-WBtBbSHL/lib/python2.7/site-packages/pip (python 2.7)
Here you can see once again that pip is running on Python 2.7, but this is perfectly ok because we’re only going to use the older version of Python to install a new virtual environment for our new Django project, within which we will install a newer version, Python 3.7. Sounds weird, I know!
If pip is not installed on your computer then you’ll need to install it now. If you’re using a Mac then you can easily install pip from the command line in a terminal window as follows:
sudo easy_install pip
3. Create a Python Virtual Environment
It may not be readily obvious, but it’s really important to manage each of your Django projects from within a virtual environment. A virtual environment is a lightweight, safe means of isolating all of the Python code packages associated with your project. Even the versions of Python and Django that you choose for your project are locally installed into a virtual environment; or at least, they should be. W