Compiling and Tuning a LAMP Stack (Linux, Apache, MySQL, PHP)
While the LEMP stack is popular for its lightweight nature, a properly tuned LAMP stack remains a powerhouse for compatibility and flexibility. On Hovixa’s high-speed NVMe infrastructure, the goal of tuning LAMP is to move away from the resource-heavy prefork model and embrace modern, multi-threaded architectures that can handle thousands of concurrent requests with minimal overhead.
1. Apache: Moving to MPM Event
The biggest performance bottleneck in legacy LAMP setups is the mpm_prefork module, which spawns a full process for every connection. For modern high-concurrency environments, you must switch to MPM Event. This allows Apache to handle multiple connections per thread, significantly reducing RAM usage.
# 1. Disable the old module and enable Event
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
# 2. Configure Event limits in /etc/apache2/mods-enabled/mpm_event.conf
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
2. PHP: Integrating PHP-FPM with Apache
Using mod_php is incompatible with MPM Event. Instead, use PHP-FPM (FastCGI Process Manager). This separates the web server from the PHP processing engine, allowing for granular control over PHP resource pools.
# Install PHP-FPM and the Proxy FastCGI module
sudo apt install php-fpm libapache2-mod-fcgid
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.x-fpm
# Verify Apache is talking to the PHP socket
# In your VirtualHost config:
SetHandler "proxy:unix:/var/run/php/php8.x-fpm.sock|fcgi://localhost"
3. MySQL: InnoDB Buffer Pool Optimization
MySQL performance on a VPS is largely determined by its ability to keep data in memory. Since Hovixa uses NVMe storage, disk I/O is fast, but RAM is still orders of magnitude faster. Adjust your my.cnf to prioritize the InnoDB buffer pool.
| Setting | Target Value | Purpose |
|---|---|---|
| `innodb_buffer_pool_size` | ~60% of Total RAM | Caches tables and indexes in RAM. |
| `innodb_log_file_size` | 256M - 512M | Reduces checkpoint frequency. |
| `max_connections` | 100 - 300 | Prevents RAM exhaustion from too many threads. |
4. Enabling HTTP/2 for Apache
HTTP/2 allows for multiplexing (sending multiple files over a single connection) and header compression. This is essential for modern web performance.
# Enable the HTTP/2 module
sudo a2enmod http2
# Add to your VirtualHost (Port 443 block)
Protocols h2 http/1.1
5. Technical Implementation Details
- .htaccess Overhead: Apache checks for
.htaccessfiles in every directory it accesses. For maximum performance, move your rewrite rules into the mainVirtualHostconfiguration and setAllowOverride None. - KeepAlive Tuning: Set
KeepAliveTimeoutto2or3. Higher values keep worker threads occupied for idle connections, while lower values ensure threads are recycled quickly. - OPcache: Ensure the PHP OPcache is enabled. This stores precompiled script bytecode in shared memory, eliminating the need for PHP to load and parse scripts on each request.
Sysadmin Advice: Use apachectl -S to verify your VirtualHost configurations and top or htop to monitor the resident set size (RSS) of your Apache and PHP-FPM processes under load.