This Guide provides a comprehensive set of instructions for setting up, developing, and deploying a web application using a combination of technologies including Docker, Django, and Next.js. This guide is tailored for deployment on a DigitalOcean Droplet, ensuring a smooth transition from local development to a live production environment. It covers initial setup, development configurations.
- Initial Setup
- Development Setup
- Production Setup on any Ubuntu Server
- Production Setup on Digitalocean Droplet
- Deploy Script Commands
- Bonus - Install OhMyZsh
- Docker and Docker Compose: Installation Guide
- Git: Installation Guide
-
Clone the Repository
git clone https://github.com/tarikkavaz/citrustrading.ch.git cd citrustrading.ch
-
Setup Frontend Environment Variables
cp frontend/.env-sample-local frontend/.env
-
Install Frontend Dependencies and Build
(cd frontend && yarn) (cd frontend && yarn build)
-
Build and Run Docker Containers
docker-compose up --build -d
-
Initialize Django Database
docker-compose exec backend python manage.py migrate
-
Load Sample Data (Optional)
docker-compose exec backend python manage.py loaddata datadump.json
-
Create Django Superuser
docker-compose exec backend python manage.py createsuperuser
-
Stop the Docker
docker-compose down
-
Build and Run Docker Containers
docker-compose up --build
-
Access the Application
- Open http://0.0.0.0:3000 for the site.
- Open http://0.0.0.0:8000/api for the API.
- Open http://0.0.0.0:8000/admin for the Django Admin Panel.
Ensure you have a clean installation of Ubuntu Server without Nginx, Docker Compose, or other related software pre-installed.
Connect to the server via SSH and run the following commands:
-
Install Docker Engine
sudo apt update sudo apt install apt-transport-https ca-certificates curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt update sudo apt install docker-ce
-
Manage Docker as a Non-root User
sudo groupadd docker sudo usermod -aG docker $USER newgrp docker
-
Install Docker Compose
sudo apt install docker-compose
-
Install nvm and Set Node Version to 18.12.0
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.zshrc echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.zshrc echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.zshrc source ~/.zshrc nvm install 18.12.0
-
Install Yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt update sudo apt install yarn
-
Install Git
sudo apt install git
-
Configure Firewalll (Optional) Configure the firewall to allow traffic on necessary ports (80, 443, 8000, 3000):
sudo ufw allow 80 sudo ufw allow 443 sudo ufw allow 8000 sudo ufw allow 3000
-
SSL Certificates Setup (Optional)
If using HTTPS, set up your SSL certificates and reference them in your Nginx configuration.
-
Running the Application Navigate to your project directory and start your Docker containers:
docker-compose -f docker-compose.prod.yml up
Notes
- Adjust commands as necessary for your specific Ubuntu version and server configuration.
- Always refer to the official Docker documentation for the most up-to-date installation procedures.
- Docker Droplet installed from Digitalocean Marketplace (Ubuntu 20.04)
- Select a minimum Plan of 2 GB / 1 CPU 50 GB SSD Disk 2 TB transfer
- Domain name pointing to the server
Connect to the server via SSH and run the following commands:
-
Install Docker Compose
apt install docker-compose
-
Install nvm and set Node version to 18.12.0
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash && echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.zshrc && echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.zshrc && echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.zshrc && source ~/.zshrc && nvm install 18.12.0
-
Install Yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - && echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list && sudo apt update && sudo apt install yarn
-
Clone the Repository
git clone https://github.com/tarikkavaz/citrustrading.ch.git citrustrading.ch cd citrustrading.ch
-
Setup Frontend Environment Variables
cp frontend/.env-sample-server frontend/.env
Ensure that the values are correctly set.
-
Setup Nginx file
cp nginx.conf-sample nginx.conf
Ensure that the values
YOURDOMAINNAME
are correctly set. -
Run the Docker
docker-compose -f docker-compose.prod.yml up --build -d && docker-compose exec backend python manage.py migrate && docker-compose exec backend python manage.py loaddata datadump.json && docker-compose exec backend python manage.py collectstatic && docker-compose exec backend python manage.py createsuperuser
( u:
admin
p:boilerplate123
)
On local
docker run --rm -v citrustradingch_media:/data -v $(pwd):/backup ubuntu bash -c 'cd /data && tar cvf /backup/images.tar *' &&
scp images.tar citrustrading:/var/lib/docker/volumes/citrustradingch_media/_data/images
On Server
tar -xvf images.tar -C /var/lib/docker/volumes/citrustradingch_media/_data/images
Before using the deploy script, set up your frontend/.env
file and SSH configurations:
Edit .env
File:
Make sure your frontend/.env
file has these lines:
SERVER_PATH=/root/yourdomainname.com
SSH_ALIAS=yoursshalias
IMAGEVOLUMEPREFIX=yourprojectname
Replace yourdomainname.com
, yoursshalias
, and yourprojectname
with your actual server path, SSH alias, and project name.
Find Your Image Volume Name:
If unsure about IMAGEVOLUMEPREFIX
, run docker volume ls
in the terminal.
Use the name before _images
from the volume list.
Ensure that your script is executable by running the following command:
chmod +x deploy.sh
To Restart Docker on server, use the following command:
./deploy.sh restart
To Pull Changes perform a git pull on the server, use the following command:
./deploy.sh pull
To update the frontend of your application, use the following command:
./deploy.sh frontend
To update the backend, use this command:
./deploy.sh backend
If you need to update both frontend and backend, use:
./deploy.sh all
To prune unused Docker objects, you can use:
./deploy.sh prune
For data management tasks, the script supports the following commands:
To dump data:
./deploy.sh dumpdata
To load data (e.g., from a dump):
./deploy.sh loaddata
To manage images stored in a Docker volume, use the following commands.
To create a tar file of all files from the images
volume on the remote server:
./deploy.sh create-tar
To download the tar file to the local root of the project:
./deploy.sh download-tar
To extract files from the tar file to the local
images volume:
./deploy.sh extract-tar
Install zsh
sudo apt install zsh -y
Set zsh as default
chsh -s $(which zsh)
Install Oh my Zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Install p10k
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/powerlevel10k
echo 'source ~/powerlevel10k/powerlevel10k.zsh-theme' >>~/.zshrc
Source zshrc
source .zshrc
If not autoloaded, setup p10k
p10k configure
hushlogin
touch ~/.hushlogin