update
This commit is contained in:
parent
ffa63acade
commit
cba3bf6482
1 changed files with 84 additions and 83 deletions
|
@ -4,20 +4,21 @@
|
|||
# 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 Nginx MariaDB"
|
||||
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
|
||||
|
||||
# Initialize success flag
|
||||
success=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"
|
||||
|
||||
# Paths
|
||||
# Configuration 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}')
|
||||
# Initialize success flag
|
||||
success=0
|
||||
|
||||
#-----------------------------------#
|
||||
# FORMATTER #
|
||||
|
@ -95,92 +96,87 @@ check_mariadb() {
|
|||
#-----------------------------------#
|
||||
|
||||
optimize_nginx() {
|
||||
# Backup original config
|
||||
cp "$NGINX_CONF" "${NGINX_CONF}.backup"
|
||||
|
||||
# Calculate worker_processes based on CPU cores
|
||||
local cpu_cores=$(nproc)
|
||||
# Create backup
|
||||
cp "$NGINX_CONF" "${NGINX_CONF}.backup.$(date +%Y%m%d_%H%M%S)"
|
||||
|
||||
# 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"
|
||||
# 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() {
|
||||
# Backup original config
|
||||
cp "$MARIADB_CONF" "${MARIADB_CONF}.backup"
|
||||
# Create backup
|
||||
cp "$MARIADB_CONF" "${MARIADB_CONF}.backup.$(date +%Y%m%d_%H%M%S)"
|
||||
|
||||
# Calculate buffer pool size (70% of total memory)
|
||||
local innodb_buffer_pool_size=$((TOTAL_MEM_GB * 70 / 100))
|
||||
# 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
|
||||
}
|
||||
|
||||
# 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/
|
||||
# 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
|
||||
}
|
||||
|
||||
character-set-server = utf8mb4
|
||||
collation-server = utf8mb4_general_ci
|
||||
init-connect = 'SET NAMES utf8mb4'
|
||||
#-----------------------------------#
|
||||
# SERVICE FUNCTIONS #
|
||||
#-----------------------------------#
|
||||
|
||||
# === 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
|
||||
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
|
||||
}
|
||||
|
||||
# === 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
|
||||
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
|
||||
}
|
||||
|
@ -214,9 +210,14 @@ main() {
|
|||
return 1
|
||||
fi
|
||||
|
||||
# Restart services
|
||||
format_output "systemctl restart nginx" "Restarting Nginx"
|
||||
format_output "systemctl restart mariadb" "Restarting MariaDB"
|
||||
# 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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue