update
This commit is contained in:
parent
ffa63acade
commit
cba3bf6482
1 changed files with 84 additions and 83 deletions
|
@ -4,20 +4,21 @@
|
||||||
# VARIABLES #
|
# VARIABLES #
|
||||||
#-----------------------------------#
|
#-----------------------------------#
|
||||||
|
|
||||||
this_script_url="https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/maintenance/optimize-server.sh"
|
this_script_url="https://example.com/optimize-server.sh"
|
||||||
this_script_name="Server Optimization Script Nginx MariaDB"
|
this_script_name="Server Optimization Script"
|
||||||
formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/task_formatter.sh"
|
formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/task_formatter.sh"
|
||||||
scriptname=$0
|
scriptname=$0
|
||||||
|
|
||||||
# Initialize success flag
|
# Configuration URLs
|
||||||
success=0
|
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"
|
||||||
|
|
||||||
# Paths
|
# Configuration paths
|
||||||
NGINX_CONF="/etc/nginx/nginx.conf"
|
NGINX_CONF="/etc/nginx/nginx.conf"
|
||||||
MARIADB_CONF="/etc/mysql/mariadb.conf.d/100-cloudpanel.cnf"
|
MARIADB_CONF="/etc/mysql/mariadb.conf.d/100-cloudpanel.cnf"
|
||||||
|
|
||||||
# System memory in GB
|
# Initialize success flag
|
||||||
TOTAL_MEM_GB=$(free -g | awk '/^Mem:/{print $2}')
|
success=0
|
||||||
|
|
||||||
#-----------------------------------#
|
#-----------------------------------#
|
||||||
# FORMATTER #
|
# FORMATTER #
|
||||||
|
@ -95,92 +96,87 @@ check_mariadb() {
|
||||||
#-----------------------------------#
|
#-----------------------------------#
|
||||||
|
|
||||||
optimize_nginx() {
|
optimize_nginx() {
|
||||||
# Backup original config
|
# Create backup
|
||||||
cp "$NGINX_CONF" "${NGINX_CONF}.backup"
|
cp "$NGINX_CONF" "${NGINX_CONF}.backup.$(date +%Y%m%d_%H%M%S)"
|
||||||
|
|
||||||
# Calculate worker_processes based on CPU cores
|
|
||||||
local cpu_cores=$(nproc)
|
|
||||||
|
|
||||||
# Update nginx.conf
|
# Download new configuration
|
||||||
sed -i "s/worker_processes.*/worker_processes $cpu_cores;/" "$NGINX_CONF"
|
if ! wget --no-check-certificate "$NGINX_CONF_URL" -O "$NGINX_CONF.new"; then
|
||||||
sed -i "s/worker_connections.*/worker_connections 65535;/" "$NGINX_CONF"
|
echo "Error: Failed to download Nginx configuration"
|
||||||
|
|
||||||
# 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
|
return 1
|
||||||
fi
|
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
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
optimize_mariadb() {
|
optimize_mariadb() {
|
||||||
# Backup original config
|
# Create backup
|
||||||
cp "$MARIADB_CONF" "${MARIADB_CONF}.backup"
|
cp "$MARIADB_CONF" "${MARIADB_CONF}.backup.$(date +%Y%m%d_%H%M%S)"
|
||||||
|
|
||||||
# Calculate buffer pool size (70% of total memory)
|
# Download new configuration
|
||||||
local innodb_buffer_pool_size=$((TOTAL_MEM_GB * 70 / 100))
|
if ! wget --no-check-certificate "$MARIADB_CONF_URL" -O "$MARIADB_CONF.new"; then
|
||||||
|
echo "Error: Failed to download MariaDB configuration"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
# Update MariaDB configuration
|
# Verify the downloaded configuration
|
||||||
cat > "$MARIADB_CONF" <<EOF
|
if ! mysqld --validate-config --defaults-file="$MARIADB_CONF.new"; then
|
||||||
[mysqld]
|
echo "Error: Invalid MariaDB configuration"
|
||||||
# === Required Settings ===
|
rm "$MARIADB_CONF.new"
|
||||||
pid-file = /var/run/mysqld/mysqld.pid
|
return 1
|
||||||
socket = /var/run/mysqld/mysqld.sock
|
}
|
||||||
log-error = /var/log/mysql/error.log
|
|
||||||
datadir = /home/mysql/
|
# Replace the configuration
|
||||||
|
mv "$MARIADB_CONF.new" "$MARIADB_CONF"
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
character-set-server = utf8mb4
|
#-----------------------------------#
|
||||||
collation-server = utf8mb4_general_ci
|
# SERVICE FUNCTIONS #
|
||||||
init-connect = 'SET NAMES utf8mb4'
|
#-----------------------------------#
|
||||||
|
|
||||||
# === InnoDB Settings ===
|
restart_nginx() {
|
||||||
innodb_file_per_table = 1
|
# Final verification before restart
|
||||||
innodb_buffer_pool_size = ${innodb_buffer_pool_size}G
|
if ! nginx -t; then
|
||||||
innodb_buffer_pool_instances = 8
|
echo "Error: Invalid Nginx configuration detected before restart"
|
||||||
innodb_flush_log_at_trx_commit = 2
|
return 1
|
||||||
innodb_log_file_size = 2G
|
}
|
||||||
innodb_io_capacity = 3000
|
|
||||||
innodb_io_capacity_max = 6000
|
if ! systemctl restart nginx; then
|
||||||
innodb_read_io_threads = 16
|
echo "Error: Failed to restart Nginx"
|
||||||
innodb_write_io_threads = 16
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Verify service is running
|
||||||
|
if ! systemctl is-active --quiet nginx; then
|
||||||
|
echo "Error: Nginx failed to start"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
# === Connection Settings ===
|
restart_mariadb() {
|
||||||
max_connections = 2000
|
if ! systemctl restart mariadb; then
|
||||||
thread_cache_size = 200
|
echo "Error: Failed to restart MariaDB"
|
||||||
interactive_timeout = 300
|
return 1
|
||||||
wait_timeout = 300
|
}
|
||||||
|
|
||||||
# === Buffer Settings ===
|
# Verify service is running
|
||||||
join_buffer_size = 8M
|
if ! systemctl is-active --quiet mariadb; then
|
||||||
read_buffer_size = 4M
|
echo "Error: MariaDB failed to start"
|
||||||
sort_buffer_size = 8M
|
return 1
|
||||||
|
}
|
||||||
# === 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
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -214,9 +210,14 @@ main() {
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Restart services
|
# Restart services with verification
|
||||||
format_output "systemctl restart nginx" "Restarting Nginx"
|
if ! format_output restart_nginx "Restarting Nginx service"; then
|
||||||
format_output "systemctl restart mariadb" "Restarting MariaDB"
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! format_output restart_mariadb "Restarting MariaDB service"; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue