Web3CV/update-nginx-config.sh

142 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
# This script updates the Nginx configuration for boilerhaus.org and its subdomains
# Load environment variables
if [ -f .env ]; then
source .env
else
echo "Error: .env file not found. Please create it based on .env.example."
exit 1
fi
# Check if sshpass is installed
if ! command -v sshpass &> /dev/null; then
echo "Error: sshpass is not installed. Please run ./install-sshpass.sh first."
exit 1
fi
# Set SSH and SCP commands with password
SSH_CMD="sshpass -p \"$SERVER_PASSWORD\" ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP"
SCP_CMD="sshpass -p \"$SERVER_PASSWORD\" scp -o StrictHostKeyChecking=no"
# Clean up existing configuration files
echo "Cleaning up existing configuration files..."
eval "$SSH_CMD \"rm -f /etc/nginx/sites-enabled/boilerhaus.org /etc/nginx/sites-enabled/boilerhaus.org.conf\""
eval "$SSH_CMD \"rm -f /etc/nginx/sites-available/boilerhaus.org /etc/nginx/sites-available/boilerhaus.org.conf\""
# Create backup of existing configuration
echo "Creating backup of existing configuration..."
eval "$SSH_CMD \"mkdir -p /etc/nginx/sites-available && cp -f /etc/nginx/sites-available/boilerhaus.org.conf /etc/nginx/sites-available/boilerhaus.org.conf.bak 2>/dev/null || true\""
# Create updated configuration file
cat > boilerhaus.org.conf.new << 'EOL'
# Main website configuration
server {
listen 80;
listen [::]:80;
server_name boilerhaus.org www.boilerhaus.org;
root /var/www/boilerhaus.org;
index index.html;
location / {
try_files $uri $uri/ =404;
}
# Managed by Certbot
# This section will be updated by Certbot automatically
}
# Nextcloud configuration
server {
listen 80;
listen [::]:80;
server_name cloud.boilerhaus.org;
# Proxy to Nextcloud
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Managed by Certbot
# This section will be updated by Certbot automatically
}
# Gitea configuration
server {
listen 80;
listen [::]:80;
server_name git.boilerhaus.org;
# Proxy to Gitea
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Managed by Certbot
# This section will be updated by Certbot automatically
}
# Vaultwarden configuration
server {
listen 80;
listen [::]:80;
server_name bw.boilerhaus.org;
# Proxy to Vaultwarden
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Managed by Certbot
# This section will be updated by Certbot automatically
}
# SSL configurations will be added by Certbot automatically
EOL
# Upload the new configuration
echo "Uploading new configuration..."
eval "$SCP_CMD boilerhaus.org.conf.new $SERVER_USER@$SERVER_IP:/tmp/boilerhaus.org.conf.new"
eval "$SSH_CMD \"mv /tmp/boilerhaus.org.conf.new /etc/nginx/sites-available/boilerhaus.org.conf\""
# Make sure the site is enabled
echo "Ensuring site is enabled..."
eval "$SSH_CMD \"ln -sf /etc/nginx/sites-available/boilerhaus.org.conf /etc/nginx/sites-enabled/boilerhaus.org.conf\""
# Test Nginx configuration
echo "Testing Nginx configuration..."
NGINX_TEST=$(eval "$SSH_CMD \"nginx -t 2>&1\"")
NGINX_TEST_EXIT_CODE=$?
# If the test is successful, start or reload Nginx
if [ $NGINX_TEST_EXIT_CODE -eq 0 ]; then
echo "Starting or reloading Nginx..."
eval "$SSH_CMD \"systemctl is-active nginx || systemctl start nginx\""
eval "$SSH_CMD \"systemctl reload nginx || systemctl restart nginx\""
echo "Configuration updated successfully!"
else
echo "Nginx configuration test failed. Restoring backup..."
echo "$NGINX_TEST"
eval "$SSH_CMD \"cp -f /etc/nginx/sites-available/boilerhaus.org.conf.bak /etc/nginx/sites-available/boilerhaus.org.conf 2>/dev/null || true\""
echo "Backup restored. Please check the configuration and try again."
fi
# Run Certbot to ensure SSL certificates are set up for all domains
echo "Running Certbot to ensure SSL certificates are set up..."
eval "$SSH_CMD \"certbot --nginx --expand -d boilerhaus.org -d www.boilerhaus.org -d cloud.boilerhaus.org -d git.boilerhaus.org -d bw.boilerhaus.org --non-interactive --agree-tos --email admin@boilerhaus.org\""
echo "Done!"