This article provides a step-by-step guide on how to update a live Django website deployed through DigitalOcean.
Abstract
The article discusses the challenges of updating a live Django website deployed through DigitalOcean, as it is a cloud hosting provider and not a PaaS like AWS Elastic Beanstalk. The author provides a detailed guide on how to update an existing website/app, including configuring Django settings for local debugging, making updates in the project's code, making migrations, reconfiguring settings for live deployment, committing changes to Git, pulling updated code from the remote repository, running migration and collect static, and restarting Gunicorn and NGINX. The article also provides links to resources for updating websites deployed on AWS Elastic Beanstalk, Heroku, and Microsoft Azure.
Opinions
The author believes that updating a live Django website deployed through DigitalOcean is a challenging task, as it is a cloud hosting provider and not a PaaS like AWS Elastic Beanstalk.
The author recommends using a custom variable in the Django settings file to switch between 'live' and 'local' modes, especially if the database changes too.
The author suggests using Fabric and Ansible to automate updates, but notes that this may not be necessary for beginners who prefer to keep things as simple as possible.
The author provides links to resources for updating websites deployed on AWS Elastic Beanstalk, Heroku, and Microsoft Azure, indicating that updating is much easier on these platforms.
How to Update Your Live Django Website
Step-by-step guide for DigitalOcean and links for AWS EB, Heroku and Azure
My Django-DigitalOcean web app. Image by author.
I’ve recently built a web app (see it here) using Django and deployed it through DigitalOcean.
There are many wonderful reasons to use DigitalOcean, but updating is not one of them. Because it is a cloud hosting provider and not a PaaS like AWS Elastic Beanstalk, the updating process is 100% your problem.
Plus, if you’ve also deployed with DigitalOcean, you might have realized that there are very few resources on how to run updates. That’s where I come in.
I’ve written about this process in my detailed walkthrough of how I built my website (which you can read below if you’d like to), but I wanted to elaborate on this to help fill in the knowledge gap for beginners like myself.
Step 1: If Django: Configure your Django settings for local debugging. You need to do this to run your work-in-progress on your localhost.
# in settings.pyDEBUG = TrueALLOWED_HOSTS = []
You could make this slightly easier if you use a custom variable in your settings file to switch between ‘live’ and ‘local’ modes, especially if your database changes too (e.g. if you use PostgreSQL).
In this example, all I do is toggle the live_deploy variable when I want to edit or deploy.
Step 2: Make updates in your project’s code.
Step 3: If Django: Make migrations. Make them locally but do the migration on the server because migration files are part of the source code and shouldn’t be tampered with on the server. Read this StackOverflow post for more info.
# in terminalpython3 manage.py makemigrations
Step 4: If Django: Reconfigure your settings for deploying live.
# in settings.pyDEBUG = FalseALLOWED_HOSTS = ['00.000.0.000','www.yourdomain.com'] # insert your IP or domain
Step 5: Stage your changes, commit them to Git and pushthem to your remote repository of choice. Extremely important, because the remote is where you’ll get the code from while in DigitalOcean (Step 8).
Step 6: Login to your DigitalOcean virtual machine with SSH.
# in terminalssh user@00.000.0.000
Step 7: Activate the virtual environment you’re using for your project, because you need to run Django commands.
# in terminalsource venv/bin/activate #replace with your venv
Step 8: From your project directory, pullyour updated code from your remote repository.
# in terminalcd /your/project/dir
git pull origin
Step 9: If Django: Run migration and collect static. Do not make migrations — refer to Step 3.
# in terminal
python3 manage.py migrate
python3 manage.py collectstatic
Step 10: Restart Gunicorn (your WSGI server) to apply the changes. Typically you won’t need to restart NGINX since no changes are made to the web server — the only time I do is when I renew my SSL certificate.
# in terminalsudo service gunicorn restart
sudo service nginx restart #only if you need to
Seems a little drawn-out, but it’s important to stick to all the steps to ensure that your new code is correctly configured for live deployment, makes it onto the virtual machine, and shows up in your website or app.