Overview

Optimizing a LAMP stack starts by understanding your current performance bottlenecks — both at the application layer(your website) and at the server level. This guide walks you through a structured process to assess your current environment, interpret performance results, and begin fine-tuning your infrastructure.

In this example, we'll use a Red Hat-based server (AlmaLinux, RockyLinux, or CentOS) with 4 GB of RAM running a WordPress site. Paths to configuration files may vary depending on your control panel (e.g., cPanel, CyberPanel, Webmin), but we’ll reference standard locations.


Step 1: Run a Website Speed Assessment

Before diving into the server internals, analyze how your website performs from the outside. This gives clues about bloated themes, unoptimized images, unused scripts, and other front-end issues that affect perceived and actual performance.

Tools You Can Use:

Common Issues Found (especially on WordPress):

  • Render-blocking JavaScript and CSS – Often caused by heavy themes or multiple plugins

  • Unoptimized images – PNGs where JPEG or WebP is better; oversized banners

  • Missing cache headers – Static assets not cached by browsers

  • Too many HTTP requests – Third-party plugins, external fonts, tracking tools

  • Slow TTFB – Can indicate poor server configuration or plugin overload

Recommended Fixes:

  • Use a caching plugin like WP Rocket, W3 Total Cache, or LiteSpeed Cache (depending on your server stack)

  • Use Autoptimize to minify JS/CSS and defer loading

  • Resize and compress images using Smush or ShortPixel

  • Offload non-critical scripts

Important: These changes are made within the WordPress dashboard and theme/plugin structure — not on the server. However, you may need to enable or configure modules on the server to support them (e.g., mod_expires, mod_deflate, mod_rewrite).

Once optimizations are done, run another speed test and compare results (load time, page size, number of requests, TTFB).


Step 2: Evaluate Server Resource Usage

Once the application layer is optimized, shift your focus to the server itself. This ensures you're not overloading or starving services.

Key Metrics to Check:

Run these as root or sudo:

top
free -m
vmstat 1 5
swapon --show
ps aux --sort=-%mem | head

Interpretation (4 GB RAM Server Example):

Let’s say:

  • Apache is using ~800MB

  • MySQL ~1.2GB

  • PHP-FPM ~400MB

  • OS & background processes ~500MB

  • Free: ~400MB

  • Swap: 1GB enabled, 600MB used

This indicates possible memory pressure. If swap usage is high, and the system is slow under load, you're likely exceeding the physical memory limits — causing processes to be swapped or killed by OOM (Out of Memory) killer.

Check the OOM log:

dmesg | grep -i kill

You may need to:

  • Reduce max workers in Apache

  • Limit MySQL memory footprint

  • Tweak PHP-FPM settings


Step 3: Analyze Your Database with MySQLTuner

Install and run MySQLTuner for in-depth analysis:

wget http://mysqltuner.pl/ -O mysqltuner.pl
perl mysqltuner.pl

Focus on:

  • innodb_buffer_pool_size (should be ~50–70% of total RAM dedicated to MySQL)

  • Temporary tables on disk

  • Slow queries

  • Query cache (disabled on newer versions — fine)

  • Max connections

Example config file locations:

  • /etc/my.cnf

  • /etc/mysql/my.cnf

  • In cPanel: /usr/my.cnf

Adjust memory allocation only after calculating total RAM usage. For example:

  • 1.5 GB to MySQL

  • 1 GB to Apache

  • 512 MB to PHP

  • 512 MB buffer for OS and idle

If MySQLTuner warns about potential OOM, scale back.


Step 4: Tune Apache and PHP for Resource Control

Apache Tuning (for 4 GB RAM):

Use the event MPM module if possible (default in newer versions). Check with:

httpd -V | grep MPM

Config file locations:

  • /etc/httpd/conf/httpd.conf

  • /etc/httpd/conf.modules.d/

  • In cPanel: /usr/local/apache/conf/

Adjust:

KeepAlive On
KeepAliveTimeout 2
MaxKeepAliveRequests 100

<IfModule mpm_event_module>
  StartServers          2
  MinSpareThreads      25
  MaxSpareThreads      75
  ThreadsPerChild      25
  MaxRequestWorkers   150
  MaxConnectionsPerChild 1000
</IfModule>

Reduce MaxRequestWorkers if Apache is using too much memory.

PHP Tuning (with PHP-FPM):

Config locations:

  • /etc/php.ini

  • /etc/php-fpm.d/www.conf

  • In cPanel: /opt/cpanel/ea-php*/root/etc/php-fpm.d/

Recommended Edits:

memory_limit = 256M
max_execution_time = 60
realpath_cache_size = 4096k
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 5

Adjust based on actual usage.


Step 5: Plan for Caching

Caching is essential to offload repeated requests and avoid processing the same queries or scripts multiple times.

Options:

  • Full-page cache via WordPress plugins (WP Rocket, LSCache)

  • Object cache with Redis or Memcached

  • Enable mod_expires and mod_deflate in Apache

  • Consider using a reverse proxy like Varnish (if you have enough memory)

Apache config example:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType text/css "access plus 1 week"
</IfModule>

Final Thoughts

This guide gives you a sysadmin-friendly, hands-on starting point to optimize your LAMP stack for better performance and stability.

Start by testing your WordPress site performance, then analyze how server resources are being used, tune services accordingly, and retest.

Need expert help reviewing your stack?

Was this answer helpful? 0 Users Found This Useful (0 Votes)