Skip to content

Instantly share code, notes, and snippets.

@saturngod
Created December 18, 2022 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saturngod/6502929a6e5f91a5eece769124d8f79a to your computer and use it in GitHub Desktop.
Save saturngod/6502929a6e5f91a5eece769124d8f79a to your computer and use it in GitHub Desktop.
update worker base on system
#!/bin/bash
# Get the total system memory in MB
total_memory=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
# Calculate the amount of memory to use for PHP-FPM workers
memory_per_worker=$((total_memory / 8 / 1024))
# Get the number of CPU cores
cpu_cores=$(grep -c ^processor /proc/cpuinfo)
# Set the number of PHP-FPM workers to the number of CPU cores
worker_processes=$cpu_cores
# Update the www.conf file with the calculated values
sed -i "s/^pm.max_children.*/pm.max_children = $worker_processes/" /etc/php/7.4/fpm/pool.d/www.conf
sed -i "s/^pm.start_servers.*/pm.start_servers = $((worker_processes / 2))/" /etc/php/7.4/fpm/pool.d/www.conf
sed -i "s/^pm.min_spare_servers.*/pm.min_spare_servers = $((worker_processes / 4))/" /etc/php/7.4/fpm/pool.d/www.conf
sed -i "s/^pm.max_spare_servers.*/pm.max_spare_servers = $((worker_processes / 2))/" /etc/php/7.4/fpm/pool.d/www.conf
sed -i "s/^pm.max_requests.*/pm.max_requests = 500/" /etc/php/7.4/fpm/pool.d/www.conf
sed -i "s/^php_admin_value\[memory_limit\].*/php_admin_value[memory_limit] = ${memory_per_worker}M/" /etc/php/7.4/fpm/pool.d/www.conf
# Restart the PHP-FPM service to apply the changes
systemctl restart php7.4-fpm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment