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 |     # Download new configuration | ||||||
|     local cpu_cores=$(nproc) |     if ! wget --no-check-certificate "$NGINX_CONF_URL" -O "$NGINX_CONF.new"; then | ||||||
|      |         echo "Error: Failed to download Nginx configuration" | ||||||
|     # 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 |         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/ |  | ||||||
|      |      | ||||||
| character-set-server = utf8mb4 |     # Replace the configuration | ||||||
| collation-server = utf8mb4_general_ci |     mv "$MARIADB_CONF.new" "$MARIADB_CONF" | ||||||
| init-connect = 'SET NAMES utf8mb4' |  | ||||||
|      |      | ||||||
| # === InnoDB Settings === |     return 0 | ||||||
| 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 | #         SERVICE FUNCTIONS         # | ||||||
| thread_cache_size = 200 | #-----------------------------------# | ||||||
| interactive_timeout = 300 |  | ||||||
| wait_timeout = 300 |  | ||||||
| 
 | 
 | ||||||
| # === Buffer Settings === | restart_nginx() { | ||||||
| join_buffer_size = 8M |     # Final verification before restart | ||||||
| read_buffer_size = 4M |     if ! nginx -t; then | ||||||
| sort_buffer_size = 8M |         echo "Error: Invalid Nginx configuration detected before restart" | ||||||
|  |         return 1 | ||||||
|  |     } | ||||||
|      |      | ||||||
| # === Query Cache Settings === |     if ! systemctl restart nginx; then | ||||||
| query_cache_size = 0 |         echo "Error: Failed to restart Nginx" | ||||||
| query_cache_type = 0 |         return 1 | ||||||
|  |     } | ||||||
|      |      | ||||||
| # === Logging Settings === |     # Verify service is running | ||||||
| slow_query_log = 1 |     if ! systemctl is-active --quiet nginx; then | ||||||
| slow_query_log_file = /var/lib/mysql/mysql_slow.log |         echo "Error: Nginx failed to start" | ||||||
| long_query_time = 2 |         return 1 | ||||||
|  |     } | ||||||
|      |      | ||||||
| # === Binary Logging === |     return 0 | ||||||
| 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 |     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
	
	 hhftechnologies
						hhftechnologies