Deploying a django app on Ubuntu instance: Part 1
March 22, 2023, 4:50 p.m. | (346 words)
I'm going to walk through all the steps to deploy a Django application on a
Ubuntu virtual instance. I use Digital Ocean droplets. But Amazon EC2 or other
Linux instances with Ubuntu should be similar. Digital Ocean has some great
guides on there for getting set up on various things, but I want to fill in some
gaps and point out some minor troubles that I have had. It is not the most
intuitive thing and there are a lot of little errors that can happen along the
way and I need a reference.
1 - Set up a non root user on the droplet, with sudo privileges
sudo adduser new_username
sudo usermod -aG sudo new_username
After following the prompts to set a password and set up the user, a new
user named 'new_username' should be available. Now switch to the new user.
sudo su new_username
2 - Set up ssh for the django repo on github, and pull the repo.
ssh-keygen
The above command will generate an ssh key that can be used to push/pull
from the github repo.
Go to github Profile->Settings->Access->SSH and GPG keys. From there copy
and paste the ssh key that was generated from the above command into the
appropriate textbox. The key will start with ssh-rsa.
git clone git@github.com:github_username/reponame.git
Now the repo should be on the instance. I keep my requirements.txt file on
the repo to install all the dependencies from.
3 - Create a new virtualenv and install the project dependencies from a
requirements.txt
cd into the project directory created from cloning the repo.
sudo apt-get install python3-pip
pip3 install virtualenv
python3 -m virtualenv venv
source /venv/bin/activate
Now we have a new virtual environment, activated and the name of it should
be in parenthesis to the left of the shell.
pip3 install -r requirements.txt
All the requirements from the file should be ready to go.
Next post I'll go over how to set up the apache webserver with gunicorn to serve
the django application, and how to point the domain name to the IP that is
serving the app.
Switch to gunicorn
March 3, 2023, 1:32 a.m. | (143 words)
mod_wsgi was giving me problems. Everything would work find while loading
different parts of the website, until parts of the site that needed to access
the database were visited, and then apache would get a wsgi:error. The thing
is, it only happened once in a while.
It seemed like after about 2-3 queries to the database a wsgi:error would be
thrown. I'm still using apache, but I switched to gunicorn, and everything
works great now. I had to change some of the settings.py so that I wouldn't get
403 errors at times in the admin area, since gunicorn runs behind a proxy. But
after that, things are working great.
I had the domain name working with the IP, until I changed to apache settings to
allow for SSL. So that is next on the agenda. Getting the domain name working,
and also SSL.
Up and running
March 1, 2023, 9:42 p.m. | (91 words)
First post! Hopefully the first of many to come. Took me some time making the
database readable by Apache, and some final tweaking with the settings.py file,
but I am glad that everything works now (pretty well). I am getting some slow
down when accessing the site, but there is only a basic 'wsgi' error showing up
in the apache error logs. Basically, the site loads for a bit and then hangs.
Now to get SSL working with certbot, and the domain pointed at the IP and things
are looking good.

