Skip to content

Instantly share code, notes, and snippets.

@theptrk
Last active December 27, 2023 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theptrk/ddd12460aaf5a2896e2d1a501f03ac4f to your computer and use it in GitHub Desktop.
Save theptrk/ddd12460aaf5a2896e2d1a501f03ac4f to your computer and use it in GitHub Desktop.
Excerpt From
Deployment from Scratch
Josef Strzibny
This material may be protected by copyright.
#!/bin/bash
USER=root
SERVER=$SERVER
PORT=22
SSH_KEY=$SSH_KEY
SSH_OPTIONS="-i $SSH_KEY -o StrictHostKeyChecking=no"
SSH_ARGS="$USER@$SERVER -p $PORT $SSH_OPTIONS"
fail () {
echo $1
exit 1
}
# Check if SERVER is set
if [ -z "$SERVER" ]; then
fail "SERVER key is not set. Aborting script."
fi
# Check if SSH_KEY is set
if [ -z "$SSH_KEY" ]; then
fail "SSH key is not set. Aborting script."
fi
ssh $SSH_ARGS 'bash -s' <<-STDIN || fail "installation failed"
set -euo pipefail
apt-get install -y nginx
STDIN
DIR=`dirname "$(readlink -f "$0")"`
if [ -e $DIR/index.html ]; then
scp $SCP_ARGS $DIR/index.html \
$USER@$SERVER:/var/www/html/index.html || \
fail "scp failed"
else
echo "index.html does not exist!"
exit 1
fi
ssh $SSH_ARGS 'bash -s' <<-STDIN || fail "restart nginx failed"
set -euo pipefail
systemctl restart nginx.service
STDIN
USER=root
SERVER=$SERVER
PORT=22
SSH_KEY=$SSH_KEY
SSH_OPTIONS="-i $SSH_KEY -o StrictHostKeyChecking=no"
SSH_ARGS="$USER@$SERVER -p $PORT $SSH_OPTIONS"
ssh $SSH_ARGS 'bash -s' <<-STDIN
apt-get install -y nginx
cat /etc/nginx/sites-available/default | grep root
> /var/www/html/index.html
cat <<-TEXT | tee /var/www/html/index.html
<h1>Tiger</h1>
<p>
Hey hey, my name is Tiger.
</p>
TEXT
systemctl restart nginx.service
STDIN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment