Misc

High Performance PHP-FPM

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.

Root required!

This guide requires root access for system settings, and the ability to edit nginx.conf for webserver settings.

Tweaking sysctl.conf

/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

The above are general network tweaks in the majority. They are designed to improve the network performance and increase the resources available and are all safe to use as defaults.

Tweaking php-fpm

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

The above configuration sets the php-fpm process scaling to dynamic, with a maximum of 50 children and allows it to scale in between. The limit of 50 is very high, but you could increase that if needed.

Increasing system file limits

In order to allow PHP to have a large number of open network connections and files you must raise the system limits. Errors with wording like "too many open files" are caused by this.

Add this to the bottom of /etc/security/limits.conf and replace 'www-data' with your php-cgi user, to allow a higher number of open files (and specifically sockets):

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 to allow the increased limits:

session required pam_limits.so

Add this to bottom of /etc/systemd/system.conf to set the max open files (and sockets as above):

DefaultLimitNOFILE=65536

Apply changes

Now reboot the system to apply your changes. It's possible to apply some of these changes without a reboot but by far the most effective will be a restart.

Testing

With LoadForge you can test the response time of all your website pages, and how they perform under stress by simulating many virtual users. Sign up today to test your systems.

We'd love to help you, contact us at hello@loadforge.com for more info.

Previous
High performance Nginx