Files
CKB/setup-ssl.sh

191 lines
5.4 KiB
Bash
Executable File

#!/bin/bash
# SSL Certificate Setup Script for CKB Application
# This script sets up SSL certificate using Let's Encrypt
set -e
echo "=== SSL Certificate Setup for CKB Application ==="
echo "Domain: bengkel.digitaloasis.xyz"
echo ""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if running as root
if [[ $EUID -ne 0 ]]; then
print_error "This script must be run as root (use sudo)"
exit 1
fi
# Check if certbot is installed
if ! command -v certbot &> /dev/null; then
print_status "Installing certbot..."
apt update
apt install -y certbot
fi
# Check if nginx is installed
if ! command -v nginx &> /dev/null; then
print_error "Nginx is not installed. Please install Nginx first."
exit 1
fi
# Step 1: Create temporary Nginx configuration for Let's Encrypt challenge
print_status "Creating temporary Nginx configuration for Let's Encrypt challenge..."
cat > /etc/nginx/sites-available/bengkel.digitaloasis.xyz << 'EOF'
server {
listen 80;
server_name bengkel.digitaloasis.xyz www.bengkel.digitaloasis.xyz;
# Root directory untuk Let's Encrypt challenge
root /var/www/html;
# Let's Encrypt challenge location
location /.well-known/acme-challenge/ {
root /var/www/html;
}
# Redirect semua traffic HTTP ke HTTPS (akan diaktifkan setelah SSL)
location / {
return 301 https://$server_name$request_uri;
}
}
EOF
# Step 2: Enable the site
print_status "Enabling Nginx site..."
ln -sf /etc/nginx/sites-available/bengkel.digitaloasis.xyz /etc/nginx/sites-enabled/
# Step 3: Test and reload Nginx
print_status "Testing Nginx configuration..."
nginx -t
print_status "Reloading Nginx..."
systemctl reload nginx
# Step 4: Generate SSL certificate
print_status "Generating SSL certificate with Let's Encrypt..."
certbot certonly --webroot \
--webroot-path=/var/www/html \
--email admin@digitaloasis.xyz \
--agree-tos \
--no-eff-email \
-d bengkel.digitaloasis.xyz \
-d www.bengkel.digitaloasis.xyz
# Step 5: Check if certificate was generated successfully
if [ -f "/etc/letsencrypt/live/bengkel.digitaloasis.xyz/fullchain.pem" ]; then
print_status "SSL certificate generated successfully!"
else
print_error "SSL certificate generation failed!"
exit 1
fi
# Step 6: Update Nginx configuration with SSL
print_status "Updating Nginx configuration with SSL..."
cat > /etc/nginx/sites-available/bengkel.digitaloasis.xyz << 'EOF'
# HTTP server (redirect to HTTPS)
server {
listen 80;
server_name bengkel.digitaloasis.xyz www.bengkel.digitaloasis.xyz;
# Let's Encrypt challenge
location /.well-known/acme-challenge/ {
root /var/www/html;
}
# Redirect to HTTPS
location / {
return 301 https://$server_name$request_uri;
}
}
# HTTPS server
server {
listen 443 ssl http2;
server_name bengkel.digitaloasis.xyz www.bengkel.digitaloasis.xyz;
# SSL configuration
ssl_certificate /etc/letsencrypt/live/bengkel.digitaloasis.xyz/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/bengkel.digitaloasis.xyz/privkey.pem;
# SSL security settings
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# Security headers
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options DENY always;
add_header X-Content-Type-Options nosniff always;
add_header X-XSS-Protection "1; mode=block" always;
# Proxy to Docker application on port 8082
location / {
proxy_pass http://localhost:8082;
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;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-Port $server_port;
# Proxy timeouts
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
# Buffer settings
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
}
}
EOF
# Step 7: Test and reload Nginx
print_status "Testing Nginx configuration with SSL..."
nginx -t
print_status "Reloading Nginx with SSL configuration..."
systemctl reload nginx
# Step 8: Setup auto-renewal
print_status "Setting up SSL certificate auto-renewal..."
(crontab -l 2>/dev/null; echo "0 12 * * * /usr/bin/certbot renew --quiet") | crontab -
# Step 9: Test SSL certificate
print_status "Testing SSL certificate..."
if curl -s -o /dev/null -w "%{http_code}" https://bengkel.digitaloasis.xyz | grep -q "200\|301\|302"; then
print_status "SSL certificate is working correctly!"
else
print_warning "SSL certificate might not be working yet. Please check manually."
fi
print_status "SSL certificate setup completed successfully!"
echo ""
print_status "Certificate information:"
certbot certificates
echo ""
print_status "Your application should now be accessible at: https://bengkel.digitaloasis.xyz"