LAMP & Laravel stack optimization.

Sep. 20, 2024

(Post in progress)

LAMP & Laravel Stack optimization.

Hi and welcome to another post of mine about tech stuff. This time I want to talk about something very related to my current work: LAMP, Laravel and optimization for high traffic servers.

Personal word of advice.

Word of advice from personal experience before diving a lot into the trap of obsession related to optimize everything to make it work with less hardware and bandwith possible. In where I live, Argentina, I have a phrase for that called “ser un pajero”. Don’t be a “pajero”, think before optimizing a hammer when what you need it’s a screwdriver.

All that being said, let’s go to the tricks I learned about optimization for the LAMPstack.

Linux optimization tips for web apps.

I only have experience using Debian-based distros, but you could apply the same ideas to any other distros probably. This part will be short since I am not expert in linux at all but I learned a few tricks of optimization for networking and high traffic servers.

All the modifications will be done at /etc/sysctl.conf

vm.swappiness

This setting controls how intensively the system swaps out pages from RAM to disk swap space. The values go from 0 to 100. For servers with limited memory, you should set this value high to avoid the server being out of ram and crashing.

But for servers with high available memory, you must consider putting this value really low, like between 0 and 25 for example.

Recycle time-wait connections.

Many times you will have like ton of time-wait connections in your network, so reusing them can be beneficial in some cases.

net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_tw_reuse = 1 

Apache optimization for high traffic.

First of all, yes, I know that nginx usually is faster and if possible may you should consider to use it instead of apache. But I believe that, after you read all above, you should consider that the problem is not “if apache or nginx is faster” but “How much time and how messy would be to port my app to nginx, is it worth it?, will it really make so much difference?”

Chosing the right Apache’s MPM.

Usually for high traffic servers, the default prefork mpm will not be ideal. Usually you want to use the worker mpm or event mpm. I personally use the mpm_worker.

To enable and configure mpm worker you do the following:

  1. a2dismod mpm_prefork
  2. a2enmod mpm_worker
  3. open the /etc/apache2/mods-enabled/mpm_worker.conf file to start configuring you worker.

Most recommend to configure the worker this way [1]:

ServerLimit           (Total RAM - Memory used for Linux, DB, etc.) / process size
StartServers          (Number of Cores)
MinSpareThreads       25
MaxSpareThreads       75
ThreadLimit           64
ThreadsPerChild       25
MaxRequestWorkers     (Total RAM - Memory used for Linux, DB, etc.) / process size
MaxConnectionsPerChild   1000

Tweaking Apache configuration.

You usually configure this at /etc/apache2/apache2.conf

#
# Timeout: The number of seconds before receives and sends time out.
#
# Personal comment:
# You want to keep this value as low as possible, but make sure not to make the value so long that your web app does not have time to execute the required scripts to generate the view / do what it has to do. Usually point is, you want this value to be a bit higher than your slower script in your server during high traffic conditions.
#
Timeout 100

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
# Personal comment:
# You usually want this ALWAYS ON
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
# Personal comment:
# Nothing more to add here, Apache description makes it as clear as possible. FOr high traffic servers with enough resources, put this as high as possible but do not go too far from what you need.
#
MaxKeepAliveRequests 500

PHP-FPM Optimization for high traffic.

I personally always use PHP-FPM, but some of the things I will share here will also work for regular PHP or other variants.

Choosing the right process managment type.

To select a process managment (pm) we must modify our /etc/php/8.2/fpm/php-fpm.conf I actually recommend to create a totally new conf folder, for example, mkdir /etc/php8.2/fpm/myconfigs.d/ and there just create a new .conf file, for example /etc/php8.2/fpm/myconfigs.d/performance.conf Then, to load all .conf files inside that folder, you add this at the bottom for your php-fpm.conf file:

include=/etc/php/8.2/fpm/myconfigs.d/*.conf

Now we are ready to select our process managment for php-fpm. We have three types of PMs: ondemand, static and dynamic. Let’s explain a bit the differences between them and which one we should choose depending in our server resources and traffic.

comments powered by Disqus