@@ -497,23 +497,105 @@ obtain_ssl_certificate() {
497497
498498 print_status " info" " Requesting SSL certificate from Let's Encrypt..."
499499
500- # Use --webroot method instead of stopping NGINX
500+ # First try the webroot method
501501 if certbot certonly --webroot --non-interactive --agree-tos \
502502 --email " $SSL_EMAIL " -d " $DOMAIN_NAME " \
503503 --webroot-path=" /var/www/html" >> " $LOG_FILE " 2>&1 ; then
504- print_status " success" " SSL certificate obtained successfully"
504+ print_status " success" " SSL certificate obtained successfully using webroot method "
505505
506506 # Setup automatic renewal
507507 (crontab -l 2> /dev/null; echo " 0 12 * * * /usr/bin/certbot renew --quiet" ) | crontab -
508508 print_status " info" " Automatic certificate renewal has been configured"
509509
510510 # Now add HTTPS configuration
511511 add_https_configuration
512+ return 0
512513 else
513- print_status " error" " Failed to obtain SSL certificate"
514- print_status " warning" " Continuing without SSL. You can manually obtain a certificate later with:"
515- print_status " info" " sudo certbot --nginx -d $DOMAIN_NAME "
516- ENABLE_SSL=" no"
514+ print_status " warning" " Webroot method failed, trying nginx method..."
515+
516+ # If webroot method fails, try the nginx method
517+ # Create a backup of the config file first
518+ cp " /etc/nginx/sites-available/$DOMAIN_NAME " " /etc/nginx/sites-available/$DOMAIN_NAME .backup"
519+
520+ if certbot --nginx --non-interactive --agree-tos \
521+ --email " $SSL_EMAIL " -d " $DOMAIN_NAME " >> " $LOG_FILE " 2>&1 ; then
522+ print_status " success" " SSL certificate obtained successfully using nginx method"
523+
524+ # Setup automatic renewal
525+ (crontab -l 2> /dev/null; echo " 0 12 * * * /usr/bin/certbot renew --quiet" ) | crontab -
526+ print_status " info" " Automatic certificate renewal has been configured"
527+
528+ # The nginx method automatically configures HTTPS, so we need to restore our proxy settings
529+ restore_proxy_settings_after_certbot
530+ return 0
531+ else
532+ print_status " error" " Failed to obtain SSL certificate using both methods"
533+ print_status " warning" " Continuing without SSL. You can manually obtain a certificate later with:"
534+ print_status " info" " sudo certbot --nginx -d $DOMAIN_NAME "
535+ ENABLE_SSL=" no"
536+ return 1
537+ fi
538+ fi
539+ }
540+
541+ # Function to restore proxy settings after Certbot modifies the config
542+ restore_proxy_settings_after_certbot () {
543+ local config_file=" /etc/nginx/sites-available/$DOMAIN_NAME "
544+ local temp_file=" /tmp/nginx_temp_config"
545+
546+ print_status " info" " Restoring proxy settings after Certbot modification..."
547+
548+ # Create a temporary file with our original proxy settings
549+ cat > " $temp_file " << EOF
550+ # Proxy settings
551+ proxy_pass http://$BACKEND_IP :$BACKEND_PORT ;
552+ proxy_http_version 1.1;
553+ $( if [[ " $WEBSOCKET_SUPPORT " == " yes" ]]; then
554+ echo " proxy_set_header Upgrade \$ http_upgrade;"
555+ echo " proxy_set_header Connection \" upgrade\" ;"
556+ else
557+ echo " # proxy_set_header Upgrade \$ http_upgrade;"
558+ echo " # proxy_set_header Connection \" upgrade\" ;"
559+ fi)
560+ proxy_set_header Host \$ host;
561+ proxy_set_header X-Real-IP \$ remote_addr;
562+ proxy_set_header X-Forwarded-For \$ proxy_add_x_forwarded_for;
563+ proxy_set_header X-Forwarded-Proto \$ scheme;
564+
565+ # Timeout settings
566+ proxy_connect_timeout 60s;
567+ proxy_send_timeout 60s;
568+ proxy_read_timeout 60s;
569+
570+ # Buffer settings
571+ proxy_buffering on;
572+ proxy_buffer_size 4k;
573+ proxy_buffers 8 4k;
574+ EOF
575+
576+ # Replace the location block content with our proxy settings
577+ # This is a bit complex but necessary because Certbot modifies the config
578+ sed -i ' /location \/ {/,/}/ {
579+ /location \/ {/ {
580+ n
581+ r /tmp/nginx_temp_config
582+ d
583+ }
584+ /}/!d
585+ }' " $config_file "
586+
587+ # Clean up
588+ rm -f " $temp_file "
589+
590+ # Test and reload configuration
591+ if nginx -t >> " $LOG_FILE " 2>&1 ; then
592+ systemctl reload nginx >> " $LOG_FILE " 2>&1
593+ print_status " success" " Proxy settings restored and NGINX reloaded successfully"
594+ else
595+ print_status " error" " Configuration test failed after restoring proxy settings"
596+ print_status " warning" " Restoring from backup configuration..."
597+ cp " /etc/nginx/sites-available/$DOMAIN_NAME .backup" " $config_file "
598+ systemctl reload nginx >> " $LOG_FILE " 2>&1
517599 fi
518600}
519601
@@ -584,7 +666,9 @@ SSL_EOF
584666 # Add HTTPS redirect if enabled
585667 if [[ " $FORCE_HTTPS " == " yes" ]]; then
586668 # Add redirect to the HTTP server block
587- sed -i ' /server_name $DOMAIN_NAME;/a \ \n # Redirect HTTP to HTTPS\n return 301 https://$host$request_uri;' " $config_file "
669+ sed -i " /server_name $DOMAIN_NAME ;/a \\
670+ # Redirect HTTP to HTTPS\\
671+ return 301 https://\\\$ host\\\$ request_uri;" " $config_file "
588672 fi
589673
590674 print_status " success" " HTTPS configuration added successfully"
@@ -809,4 +893,4 @@ main() {
809893trap ' echo -e "\n${RED}Operation interrupted by user${NC}"; exit 1' INT
810894
811895# Run main function
812- main " $@ "
896+ main " $@ "
0 commit comments