How to get the highest RPS out of your PHP-FPM installation through tweaks, sysctls, and config options.
A common installation method for PHP is using php-fpm, typically used by Nginx and often the best method for scaling php processes for your web server. There are performance tweaks needed to the default setup for high requests per second.
/etc/sysctl.conf
holds the various OS tweaks for your server. It allows you to tune the
performance of your system in general, and particularly what PHP-FPM can achieve. Add these lines
to the bottom of your sysctl.conf file:
net.ipv4.tcp_syncookies=1
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.all.secure_redirects=1
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.all.accept_source_route=0
net.ipv6.conf.all.accept_source_route=0
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.tcp_timestamps=0
net.ipv4.tcp_mem=786432 1697152 1945728
net.ipv4.tcp_rmem=4096 4096 16777216
net.ipv4.tcp_wmem=4096 4096 16777216
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ipv4.netfilter.ip_conntrack_max=999999
net.netfilter.nf_conntrack_max=999999
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_max_orphans=262144
net.ipv4.ip_local_port_range=1000 65535
net.ipv4.tcp_fin_timeout=30
net.core.netdev_max_backlog=10000
net.core.somaxconn=60000
net.ipv4.tcp_synack_retries=3
fs.file-max=640000
Customize your PHP-FPM installations performance by editing the /etc/php/VERSION/fpm/pool.d/www.conf file (replacing version with your version, e.g. 7.4). In particular replace these values below:
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 5000
In order to allow PHP to have a large number of open network connections and files you must raise the system limits.
Add this to the bottom of /etc/security/limits.conf
and replace 'www-data' with your php-cgi user:
root soft nofile 65536
root hard nofile 65536
www-data soft nofile 65536
www-data hard nofile 65536
Add this to bottom of /etc/pam.d/common-session
:
session required pam_limits.so
Add this to bottom of /etc/systemd/system.conf
:
DefaultLimitNOFILE=65536
Now reboot the system to apply your changes.