Docker Deployment for DevLeague
{username} = your username
{ip} = your DO droplet ip address
{repo} = your repo address
{email} = your email
Generate an SSH key for your laptop (if does not exists yet)
Add your SSH key to your agent
ssh-add -K
// for mac
ssh-add {path to your id_rsa.pub file}
Go to https://aws.amazon.com/
Sign up for an account
Services -> EC2 -> (sidebar) Network & Security -> (sidebar) Key Pairs
Choose 'Import Key Pair' from the top navigation
Give your key pair a descriptive name
Either use the file input to upload your public key (.pub)
OR copy and paste the contents of your public key into the text area
Services -> EC2 -> Launch Instance
On Step 1, Choose (Ubuntu Server 18.04 LTS HVM, SSD)
On Step 2, t2.micro (Free tier)
On Step 3, leave all defaults
On Step 4, set size to 20 GB
On Step 5, don't add any tags
On Step 6, name the security group something descriptive
On Step 6, add HTTP and HTTPS ports
Notice that this EC2 is open to the world
Click Launch
Choose your SSH key from (Uploading your SSH Key)
Wait till the instance is fully launched
Name your EC2 instance something descriptive
Get public IP of new EC2 instance
ssh ubuntu@{ip}
Enter yes to prompt
sudo adduser {username}
and follow prompts
Make sure to set a password
sudo mkdir /home/{username}/.ssh
cd /home/{username}/.ssh
sudo touch authorized_keys
sudo vi authorized_keys
Paste your .ssh/id_rsa.pub key from your laptop into this file
:wq
to leave vim
cd ..
to leave .ssh folder
sudo chown -R {username}:{username} .ssh
exit
ssh {username}@{ip}
exit
ssh ubuntu@{ip}
// Get back in as root
sudo usermod -aG sudo {username}
su - {username}
// switch to new account
sudo ls -lah /root
// smoke test sudo capabilities
exit
On your laptop, go to ~/.ssh
touch config
vi config
Host {ssh-name}
Hostname {ip}
Port 22
User {username}
ForwardAgent yes
IdentityFile {.pub-file}
:wq
ssh {ssh-name}
cd /home/{username}
mkdir projects
cd projects
git clone {repo}
Follow these instructions to install Docker: https://docs.docker.com/install/linux/docker-ce/ubuntu/
Follow these instructions to install Docker Compose: https://docs.docker.com/compose/install/
Copy your .env file from your project into your project directory on the EC2 Server.
Update PORT to 80.
cd /home/{username}/projects/{repo}
docker-compose up -d
Edit your .env file on the EC2 server to 8080.
docker-compose down
docker-compose up -d
sudo apt install nginx
nginx -v
// smoke test nginx
cd /etc/nginx/sites-available
sudo touch {name}
sudo vi {name}
Paste in this:
server {
listen 80;
server_name {ip};
client_max_body_size 100m;
client_body_timeout 120s; # Default is 60, May need to be increased for very large uploads
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
sudo cp {name} ../sites-enabled/
sudo nginx -t
// smoke test the new config file we made
sudo service nginx reload
Smoke test by going to your {ip}
Purchase a domain from Google Domains or NameCheap
Point your DNS A records to point to your public IP
- A record - @ - {ip}
- A record - www - {ip}
Update your nginx config to handle that new domain (servername)
Test your nginx file
Smoke test the new domain
// Installing Let's Encrypt PAA repository, Please ENTER to accept adding this repo
sudo add-apt-repository ppa:certbot/certbot
// Update repositories
sudo apt-get update
// Installing Let's Encrypt
sudo apt-get install python-certbot-nginx
// Running certbot
sudo certbot --nginx -d ${domain} -d www.${domain}
// Ensure that option 2 is selected for Redirecting traffic .
// This will automatically update your Nginx files, and create a cronjob in /etc/cron.d/certbot
sudo ufw enable
sudo ufw default deny incoming
sudo ufw allow 80
sudo ufw allow 443
Port 2222
PermitRootLogin no
PasswordAuthentication no
sudo ufw allow 2222
sudo ufw deny 22
sudo service sshd restart
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades