Steps and shell script to install required packages, install a database and setup a Django project on a Digital Ocean Droplet.
Goals - Host a Django website and database for under $7 per month
- Setup a Django project on a Digital Ocean Droplet
- Install required packages using a shell script
- Install and configure database on the droplet itself to not have to pay for any other database.
Steps
These are the steps I followed to setup a Django project on a Digital Ocean Droplet. I come back to this page every time I need to setup a new project for now. The postgres setup part is not very elaborate, I had searched online for answers and I got it.
Ok so here we go...
- Create a droplet on digitalocean
- Run below seed.sh script https://prasannakulkarni.com/seed.html
#!/bin/bash
echo Hello World! I am a shell script. Run on your server. Google about it.
cmd="sudo apt-get update && sudo apt-get upgrade -y"
get_ready()
{
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install \
apache2 \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
gnupg \
lsb-release \
python3-pip \
python3-venv \
software-properties-common -y
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo chmod a+r /etc/apt/keyrings/docker.gpg
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
sudo apt install postgresql postgresql-contrib -y
sudo systemctl start postgresql.service
sudo apt install net-tools -y
sudo apt-get
update-rc.d postgresql enable
service postgresql start
service postgresql status
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
}
open_ports()
{
sudo ufw allow http
sudo ufw allow https
sudo ufw allow OpenSSH
sudo ufw allow 5432/tcp
sudo ufw enable -y
}
open_ports
get_ready
- Install gh https://github.com/cli/cli#installation
- login to gh `gh auth login`
- `gh repo clone etherator-xyz/pop_djangocrawler`
- postgres setup
find postgresql.conf, and change listen_addresses to '*' Now restart postgresql server. In order to fix it, open pg_hba.conf and add following entry at the very end to fix it.
- Use below dockerfile for your django project with modifications as per your use case
FROM python:3.7.4-alpine3.10
ADD mysitedjango/requirements.txt /app/requirements.txt
RUN set -ex \
&& apk add --no-cache --virtual .build-deps postgresql-dev build-base python3-dev libffi-dev\
&& python3 -m venv /env \
&& /env/bin/pip3 install --upgrade pip \
&& /env/bin/pip3 install --no-cache-dir -r /app/requirements.txt \
&& /env/bin/pip3 install algoliasearch-django \
&& /env/bin/pip3 install typesense \
&& runDeps="$(scanelf --needed --nobanner --recursive /env \
| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
| sort -u \
| xargs -r apk info --installed \
| sort -u)" \
&& apk add --virtual rundeps $runDeps \
&& apk del .build-deps
ADD mysitedjango /app
WORKDIR /app
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
EXPOSE 8000
CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "mysiteDjango.wsgi:application"]
- build image & run the image