#!/bin/bash #-----------------------------------# # VARIABLES # #-----------------------------------# this_script_url="https://example.com/optimize-server.sh" this_script_name="Server Optimization Script" formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/task_formatter.sh" scriptname=$0 # Configuration URLs NGINX_CONF_URL="https://git.hhf.technology/hhf/script-management-cloudpanel/src/branch/main/optimize/nginx.conf" MARIADB_CONF_URL="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/optimize/100-cloudpanel.cnf" # Configuration paths NGINX_CONF="/etc/nginx/nginx.conf" MARIADB_CONF="/etc/mysql/mariadb.conf.d/100-cloudpanel.cnf" # Initialize success flag success=0 #-----------------------------------# # FORMATTER # #-----------------------------------# # Download and source the formatter with error handling download_formatter() { if [ ! -f "task_formatter.sh" ]; then if ! wget "$formatter_url" --no-check-certificate -O task_formatter.sh > /dev/null 2>&1; then echo "Error: Failed to download task_formatter.sh" exit 1 fi fi chmod +x task_formatter.sh source ./task_formatter.sh || { echo "Error: Failed to source task_formatter.sh"; exit 1; } } download_formatter #-----------------------------------# # CHECK FUNCTIONS # #-----------------------------------# check_os_compatibility() { local os_version=$(lsb_release -rs) local os_name=$(lsb_release -is) local is_compatible=0 if [ "$os_name" = "Ubuntu" ] && [ "$os_version" = "24.04" ]; then is_compatible=1 elif [ "$os_name" = "Debian" ] && [ "$os_version" = "12" ]; then is_compatible=1 fi if [ $is_compatible -eq 0 ]; then echo "Error: This script only supports Ubuntu 24.04 and Debian 12" return 1 fi return 0 } check_nginx_version() { if ! command -v nginx >/dev/null 2>&1; then echo "Error: Nginx is not installed" return 1 fi local version=$(nginx -v 2>&1 | grep -oP 'nginx/\K[0-9]+\.[0-9]+\.[0-9]+') if [[ ! $version =~ ^1\.26\. ]]; then echo "Error: This script requires Nginx version 1.26.X" return 1 fi return 0 } check_mariadb() { if ! command -v mysql >/dev/null 2>&1; then echo "Error: MySQL/MariaDB is not installed" return 1 fi if ! mysql --version | grep -q MariaDB; then echo "Error: This script only supports MariaDB" return 1 fi return 0 } #-----------------------------------# # OPTIMIZATION FUNCTIONS # #-----------------------------------# optimize_nginx() { # Create backup cp "$NGINX_CONF" "${NGINX_CONF}.backup.$(date +%Y%m%d_%H%M%S)" # Download new configuration if ! wget --no-check-certificate "$NGINX_CONF_URL" -O "$NGINX_CONF.new"; then echo "Error: Failed to download Nginx configuration" return 1 fi # Verify the downloaded configuration if ! nginx -t -c "$NGINX_CONF.new"; then echo "Error: Invalid Nginx configuration" rm "$NGINX_CONF.new" return 1 fi # Replace the configuration mv "$NGINX_CONF.new" "$NGINX_CONF" return 0 } optimize_mariadb() { # Create backup cp "$MARIADB_CONF" "${MARIADB_CONF}.backup.$(date +%Y%m%d_%H%M%S)" # Download new configuration if ! wget --no-check-certificate "$MARIADB_CONF_URL" -O "$MARIADB_CONF.new"; then echo "Error: Failed to download MariaDB configuration" return 1 } # Verify the downloaded configuration if ! mysqld --validate-config --defaults-file="$MARIADB_CONF.new"; then echo "Error: Invalid MariaDB configuration" rm "$MARIADB_CONF.new" return 1 } # Replace the configuration mv "$MARIADB_CONF.new" "$MARIADB_CONF" return 0 } #-----------------------------------# # SERVICE FUNCTIONS # #-----------------------------------# restart_nginx() { # Final verification before restart if ! nginx -t; then echo "Error: Invalid Nginx configuration detected before restart" return 1 } if ! systemctl restart nginx; then echo "Error: Failed to restart Nginx" return 1 } # Verify service is running if ! systemctl is-active --quiet nginx; then echo "Error: Nginx failed to start" return 1 } return 0 } restart_mariadb() { if ! systemctl restart mariadb; then echo "Error: Failed to restart MariaDB" return 1 } # Verify service is running if ! systemctl is-active --quiet mariadb; then echo "Error: MariaDB failed to start" return 1 } return 0 } #-----------------------------------# # MAIN LOGIC # #-----------------------------------# main() { print_header "$this_script_name" "$this_script_url" # Check compatibility if ! format_output check_os_compatibility "Checking OS compatibility"; then return 1 fi if ! format_output check_nginx_version "Checking Nginx version"; then return 1 fi if ! format_output check_mariadb "Checking MariaDB installation"; then return 1 fi # Perform optimizations if ! format_output optimize_nginx "Optimizing Nginx configuration"; then return 1 fi if ! format_output optimize_mariadb "Optimizing MariaDB configuration"; then return 1 fi # Restart services with verification if ! format_output restart_nginx "Restarting Nginx service"; then return 1 fi if ! format_output restart_mariadb "Restarting MariaDB service"; then return 1 fi return 0 } # Run main function main exit $?