From 1715f31f4a9c2c4ffc8a134f12eb7370b11cfa63 Mon Sep 17 00:00:00 2001 From: hhf Date: Wed, 27 Nov 2024 14:00:59 +0530 Subject: [PATCH] Add maintenance/optimize-server.sh --- maintenance/optimize-server.sh | 226 +++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 maintenance/optimize-server.sh diff --git a/maintenance/optimize-server.sh b/maintenance/optimize-server.sh new file mode 100644 index 0000000..895f426 --- /dev/null +++ b/maintenance/optimize-server.sh @@ -0,0 +1,226 @@ +#!/bin/bash + +#-----------------------------------# +# VARIABLES # +#-----------------------------------# + +this_script_url="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/maintenance/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 + +# Initialize success flag +success=0 + +# Paths +NGINX_CONF="/etc/nginx/nginx.conf" +MARIADB_CONF="/etc/mysql/mariadb.conf.d/100-cloudpanel.cnf" + +# System memory in GB +TOTAL_MEM_GB=$(free -g | awk '/^Mem:/{print $2}') + +#-----------------------------------# +# 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() { + # Backup original config + cp "$NGINX_CONF" "${NGINX_CONF}.backup" + + # Calculate worker_processes based on CPU cores + local cpu_cores=$(nproc) + + # Update nginx.conf + sed -i "s/worker_processes.*/worker_processes $cpu_cores;/" "$NGINX_CONF" + sed -i "s/worker_connections.*/worker_connections 65535;/" "$NGINX_CONF" + + # Update buffer sizes + sed -i "s/client_body_buffer_size.*/client_body_buffer_size 16k;/" "$NGINX_CONF" + sed -i "s/client_max_body_size.*/client_max_body_size 100M;/" "$NGINX_CONF" + + # Update timeouts + sed -i "s/keepalive_timeout.*/keepalive_timeout 65;/" "$NGINX_CONF" + sed -i "s/client_body_timeout.*/client_body_timeout 15;/" "$NGINX_CONF" + + # Enable GZIP and Brotli + sed -i "s/gzip.*/gzip on;/" "$NGINX_CONF" + sed -i "s/brotli.*/brotli on;/" "$NGINX_CONF" + + # Test configuration + if ! nginx -t; then + echo "Error: Invalid Nginx configuration" + mv "${NGINX_CONF}.backup" "$NGINX_CONF" + return 1 + fi + + return 0 +} + +optimize_mariadb() { + # Backup original config + cp "$MARIADB_CONF" "${MARIADB_CONF}.backup" + + # Calculate buffer pool size (70% of total memory) + local innodb_buffer_pool_size=$((TOTAL_MEM_GB * 70 / 100)) + + # Update MariaDB configuration + cat > "$MARIADB_CONF" <