Web3CV/update-nginx-config.sh

92 lines
2.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"
# Create main website configuration file
echo "Creating main website configuration file..."
cat > boilerhaus.org.conf.new << 'EOL'
server {
listen 80;
listen [::]:80;
server_name 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
}
EOL
# Upload the new configuration
echo "Uploading main website 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, reload Nginx
if [ $NGINX_TEST_EXIT_CODE -eq 0 ]; then
echo "Reloading Nginx..."
eval "$SSH_CMD \"systemctl reload nginx\""
echo "Configuration updated successfully!"
else
echo "Nginx configuration test failed:"
echo "$NGINX_TEST"
echo "Please check the configuration and try again."
exit 1
fi
# Run Certbot to ensure SSL certificates are set up for main domain
echo "Running Certbot to ensure SSL certificates are set up..."
eval "$SSH_CMD \"certbot --nginx -d boilerhaus.org -d cloud.boilerhaus.org --non-interactive --agree-tos --email admin@boilerhaus.org\""
echo "Done!"