Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

calimdor's avatar

Forge - Setting Max Children Count

Hi there,

Currently I'm using Elasticbeanstalk in AWS, And wanted to continue with Forge. But couldnt arrange my PHP-FPM settings.

Currently I'm using x_optimize_php.sh file like below;

#!/bin/bash

# This file will make sure that will set the max processes and spare processes
# according to the details provided by this machine instance.

DEFAULT_PROCESS_MEMORY="120"
MAX_REQUESTS="500"

PROCESS_MAX_MB=$(ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+= } END { printf ("%d\n", sum/NR/1024) }') || $DEFAULT_PROCESS_MEMORY

VCPU_CORES=$(($(lscpu | awk '/^CPU\(s\)/{ print  }')))

TOTAL_MEMORY_IN_KB=$(free | awk '/^Mem:/{print }')
USED_MEMORY_IN_KB=$(free | awk '/^Mem:/{print }')
FREE_MEMORY_IN_KB=$(free | awk '/^Mem:/{print }')

TOTAL_MEMORY_IN_MB=$(($TOTAL_MEMORY_IN_KB / 1024))
USED_MEMORY_IN_MB=$(($USED_MEMORY_IN_KB / 1024))
FREE_MEMORY_IN_MB=$(($FREE_MEMORY_IN_KB / 1024))

MAX_CHILDREN=$(($FREE_MEMORY_IN_MB / $PROCESS_MAX_MB))

# Optimal would be to have at least 1/4th of the children filled with children waiting to serve requests.
START_SERVERS=$(($MAX_CHILDREN / 4))
MIN_SPARE_SERVERS=$(($MAX_CHILDREN / 4))

# Optimal would be to have at most 3/4ths of the children filled with children waiting to serve requests.
MAX_SPARE_SERVERS=$(((3 * $MAX_CHILDREN) / 4))

sudo sed -i "s|pm.max_children.*|pm.max_children = $MAX_CHILDREN|g" /etc/php-fpm.d/www.conf
sudo sed -i "s|pm.start_servers.*|pm.start_servers = $START_SERVERS|g" /etc/php-fpm.d/www.conf
sudo sed -i "s|pm.min_spare_servers.*|pm.min_spare_servers = $MIN_SPARE_SERVERS|g" /etc/php-fpm.d/www.conf
sudo sed -i "s|pm.max_spare_servers.*|pm.max_spare_servers = $MAX_SPARE_SERVERS|g" /etc/php-fpm.d/www.conf

printf "\npm.max_requests = $MAX_REQUESTS" | sudo tee -a /etc/php-fpm.d/www.conf

# Restarting the services afterwards.
sudo systemctl restart php-fpm.service
sudo systemctl restart nginx.service

As you can see, it arrange max requests & max children regarding its currently active RAM slots.

And how can I arrange the same build on my Forge account?

0 likes
2 replies
LaryAI's avatar
Level 58

To set the max children count in Laravel Forge, you can follow these steps:

  1. Log in to your Laravel Forge account.
  2. Select the server you want to configure.
  3. Go to the "PHP" tab.
  4. Scroll down to the "PHP-FPM Configuration" section.
  5. In the "Additional PHP-FPM Configuration" field, you can add the necessary configuration directives to set the max children count.

Here's an example of how you can modify the configuration to match the script you provided:

pm.max_children = 10
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 8
pm.max_requests = 500

Adjust the values according to your requirements.

  1. Click on the "Save" button to apply the changes.

After saving the configuration, Forge will automatically update the PHP-FPM settings on your server.

calimdor's avatar

@LaryAI This is not enough for me. I want to make it dynamically.

For example; If I have 16gb ram, then how I should do it?

Please or to participate in this conversation.