Add maintenance/optimize-server.sh
This commit is contained in:
parent
6625da4402
commit
1715f31f4a
1 changed files with 226 additions and 0 deletions
226
maintenance/optimize-server.sh
Normal file
226
maintenance/optimize-server.sh
Normal file
|
@ -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" <<EOF
|
||||
[mysqld]
|
||||
# === Required Settings ===
|
||||
pid-file = /var/run/mysqld/mysqld.pid
|
||||
socket = /var/run/mysqld/mysqld.sock
|
||||
log-error = /var/log/mysql/error.log
|
||||
datadir = /home/mysql/
|
||||
|
||||
character-set-server = utf8mb4
|
||||
collation-server = utf8mb4_general_ci
|
||||
init-connect = 'SET NAMES utf8mb4'
|
||||
|
||||
# === InnoDB Settings ===
|
||||
innodb_file_per_table = 1
|
||||
innodb_buffer_pool_size = ${innodb_buffer_pool_size}G
|
||||
innodb_buffer_pool_instances = 8
|
||||
innodb_flush_log_at_trx_commit = 2
|
||||
innodb_log_file_size = 2G
|
||||
innodb_io_capacity = 3000
|
||||
innodb_io_capacity_max = 6000
|
||||
innodb_read_io_threads = 16
|
||||
innodb_write_io_threads = 16
|
||||
|
||||
# === Connection Settings ===
|
||||
max_connections = 2000
|
||||
thread_cache_size = 200
|
||||
interactive_timeout = 300
|
||||
wait_timeout = 300
|
||||
|
||||
# === Buffer Settings ===
|
||||
join_buffer_size = 8M
|
||||
read_buffer_size = 4M
|
||||
sort_buffer_size = 8M
|
||||
|
||||
# === Query Cache Settings ===
|
||||
query_cache_size = 0
|
||||
query_cache_type = 0
|
||||
|
||||
# === Logging Settings ===
|
||||
slow_query_log = 1
|
||||
slow_query_log_file = /var/lib/mysql/mysql_slow.log
|
||||
long_query_time = 2
|
||||
|
||||
# === Binary Logging ===
|
||||
disable_log_bin
|
||||
EOF
|
||||
|
||||
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
|
||||
format_output "systemctl restart nginx" "Restarting Nginx"
|
||||
format_output "systemctl restart mariadb" "Restarting MariaDB"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main
|
||||
exit $?
|
Loading…
Reference in a new issue