Jul 4, 2024
0
Level 1
Laravel Forge - Nginx Configurations - Optimize PHP
Currently im using elasticbeanstalk in AWS. I want to use forge in my project now. But i couldnt handle optimizing nginx in it.
In my AWS, i got x_optimize_php_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
So automatically optimize php loads with my memory.
When I switch my service to Forge. It creates a load problem. (Even EBS doesnt do it with same memory)
For example im picking 32gb memory bla bla bla. But it starts overload issues on the system. Users stucks etc.
How can I handle these things on forge?
Please or to participate in this conversation.