Skip to content

Instantly share code, notes, and snippets.

@PraneethKarnena
Last active June 4, 2024 06:48
Show Gist options
  • Save PraneethKarnena/aa351fb0aa5a0a5c6c1311c008b754c5 to your computer and use it in GitHub Desktop.
Save PraneethKarnena/aa351fb0aa5a0a5c6c1311c008b754c5 to your computer and use it in GitHub Desktop.
List of commands to setup a fresh Debian server
# OS - Debian 10
# AWS EC2 - t3.xlarge
# Swap - 16GB
# Login into server
ssh admin@server-ip-address -i key.pem
# Create new user & grant admin privileges
sudo adduser praneeth
sudo usermod -aG sudo praneeth
# Add SSH key to the newly created user
sudo su - praneeth
mkdir ~/.ssh && chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
sudo cat /home/admin/.ssh/authorized_keys > ~/.ssh/authorized_keys
# Login with the newly created user
ssh praneeth@server-ip-address -i key.pem
# Update the system
sudo apt update && sudo apt dist-upgrade -y
# Increase Swap space
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Add the following line to /etc/fstab
/swapfile swap swap defaults 0 0
# Install Pyenv for Python
# Dependencies
sudo apt install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl git libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev libedit-dev libncurses5-dev -y
# Install Pyenv
curl https://pyenv.run | bash
# Add the following lines to the end of ~/.bashrc file
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
# Restart shell
exec $SHELL
# Install Python 3.7.4
CONFIGURE_OPTS=--enable-shared pyenv install 3.7.4
pyenv global 3.7.4
# Install Postgres
sudo apt install postgresql postgresql-contrib -y
sudo systemctl enable postgresql
sudo service postgresql restart
# Set password for postgres user
sudo -u postgres psql
ALTER USER postgres PASSWORD 'postgres';
# Create database & user
CREATE DATABASE d8adriven;
CREATE USER d8adriven WITH PASSWORD 'd8adriven';
GRANT ALL PRIVILEGES ON DATABASE d8adriven TO d8adriven;
\q
# Fix Postgres access
sudo nano /etc/postgresql/11/main/pg_hba.conf
# Replace the below line:
local all all peer
# With
local all all md5
# Restart Postgres
sudo service postgresql restart
# Install Rust - dependency for AWS ARM processors
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh && exec $SHELL
# Create project directory
mkdir -p d8adriven/d8adriven-backend
# Create virtual environment
cd d8adriven/d8adriven-backend
pyenv local 3.7.4
python -m venv venv
source venv/bin/activate
# Ugrade pip
pip install --upgrade pip
# Optional - Install GitHub cli
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo gpg --dearmor -o /usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install gh -y
# Clone the repo - using GitHub cli
gh auth login
gh repo clone d8adriven-team/backend
cd backend
git checkout feat-ams-v2
# Install project requirements
pip install -r requirements.txt
# Create .env file and add variables
# Set DEBUG=False & set ALLOWED_HOSTS
nano .env
# Run migrate command
python manage.py migrate
# Add static files
python manage.py collectstatic --no-input
# Install nginx
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring -y
# Add key
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
# Verify key
gpg --dry-run --quiet --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
# Set up stable repository
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
# Pin package
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
# Install
sudo apt update && sudo apt install nginx -y && sudo systemctl enable nginx && sudo service nginx restart
# Allow large file uploads
sudo nano /etc/nginx/nginx.conf
Add "client_max_body_size 20000M;" in the http block
# Create gunicorn service
# https://gist.github.com/PraneethKarnena/e41737f92c9d15023ef4e41f58f83758
sudo nano /etc/systemd/system/gunicorn.service
sudo systemctl enable gunicorn
sudo systemctl daemon-reload
sudo service gunicorn restart
# Create Nginx configuration
# https://gist.github.com/PraneethKarnena/c8a63f4ce231707d7eca1ee4be0b7e46
# https://gist.github.com/PraneethKarnena/16730d2e4a347137e8b3ada90ae83960
sudo nano /etc/nginx/conf.d/d8adriven.conf
sudo nginx -t
sudo service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment