47 lines
1.8 KiB
Bash
47 lines
1.8 KiB
Bash
#!/bin/sh
|
|
set -eu
|
|
test "$(id -u)" = 0 || { echo 'Run this setup script with sudo.' >&2; exit 1; }
|
|
script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd)
|
|
snippet="$script_dir/../deploy/nginx-locations.conf"
|
|
if [ ! -f "$snippet" ]; then snippet="$script_dir/nginx-locations.conf"; fi
|
|
test -s "$snippet"
|
|
site=$(readlink -f /etc/nginx/sites-enabled/ManiCandles.midastix.com.conf)
|
|
test -f "$site"
|
|
if grep -Eq 'listen[[:space:]].*443|ssl_certificate' "$site"; then
|
|
echo 'This site already contains TLS settings. Review its configuration before running setup.' >&2
|
|
exit 1
|
|
fi
|
|
nginx -t
|
|
command -v curl
|
|
command -v flock
|
|
command -v certbot
|
|
root=/var/www/manicandles
|
|
install -d -o mihir -g "$(id -gn mihir)" -m 0755 "$root" "$root/releases" "$root/incoming"
|
|
if [ ! -e "$root/current" ] && [ ! -L "$root/current" ] && [ -s /var/www/ManiCandles.midastix.com/index.html ]; then
|
|
ln -s /var/www/ManiCandles.midastix.com "$root/current"
|
|
chown -h mihir:"$(id -gn mihir)" "$root/current"
|
|
fi
|
|
install -d -m 0755 /etc/nginx/snippets /var/backups
|
|
backup="/var/backups/manicandles-nginx-$(date +%Y%m%d%H%M%S)-$$.conf"
|
|
cp -p "$site" "$backup"
|
|
if [ -e /etc/nginx/snippets/manicandles.conf ] && ! cmp -s "$snippet" /etc/nginx/snippets/manicandles.conf; then
|
|
echo 'A different deployment snippet exists. Review the existing setup before continuing.' >&2
|
|
exit 1
|
|
fi
|
|
install -m 0644 "$snippet" /etc/nginx/snippets/manicandles.conf
|
|
cat > "$site" <<'NGINX'
|
|
server {
|
|
listen 80;
|
|
server_name manicandles.midastix.com;
|
|
include /etc/nginx/snippets/manicandles.conf;
|
|
}
|
|
NGINX
|
|
if ! nginx -t || ! systemctl reload nginx; then
|
|
cp -p "$backup" "$site"
|
|
nginx -t && systemctl reload nginx
|
|
echo "Setup failed. Restored $backup" >&2
|
|
exit 1
|
|
fi
|
|
echo "Site prepared. Original configuration saved at $backup"
|
|
echo 'Next run: sudo certbot --nginx -d manicandles.midastix.com --redirect'
|