by Carl James | BLOOMINGTON, IN | June 27, 2021
I had a lot of options when it came to hosting the currently two Django websites that I currently maintain (including the one that this blog is posted on). There were several recommendations, but being an IT guy in support and operations I wanted to do the administration myself. I drew the line at configuring hardware, and chose Digital Ocean as a cloud provider to host my virtual servers.
The instructions that are available to assist someone like me in setting up these servers is pretty great. Digital Ocean has some great tutorials like How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 20.04. This provided a lot of step-by-step guidance that allowed me to publish my first Django site quickly. I am also very concerned about web security so I also implemented How To Secure Nginx with Let's Encrypt on Ubuntu 20.04 to make both of my sites SSL compliant.
That was great to start, but then I had to learn how to maintain these. Keeping the Ubuntu server itself and its core software is easy enough. Even keeping Django and its dependencies up-to-date is a simple process. I ran into an issue, however when it came to updating the version of Python that was running.
In order to prevent the server itself from developing a lot of crud and vulnerabilities with Python dependency libraries, the best practice is to run Python within what are called virtual environments. Those environments hold the dependencies for those apps and no others. I can update Django, it's dependencies, and other libraries for the app easily enough from within the environement. What I can't do is update Python itself.
What my internet research showed was the only good way to do that was to create a new virtual environment. I could not find specific instructions for how to do that in my situation, so I developed that myself. It turns out that the process is actually quite simple. Essentially I install the newer version of python on the server. I then use that to create a new virtual environment which I name "env" followed by numbers that indicate the new Python version. I then install all of the dependices, libraries, and including "gunicorn" which is the program that serves up the web app from the virtual environment into nginx.
Once I bind the gunicorn with gunicorn --bind IP.IP.IP.IP:8000 django_project.wsgi I then edited gunicorn.service file to point to the new environment and reboot the server. Once back online if it is working, I then delete the old virtual enviroment and my web app is running on the latest most secure version of Python available.