Optimizing Slow Apache 2.2 Response Times on Gentoo LAMP Stack: MySQL and PHP Caching Solutions


5 views

When analyzing the Firebug Net panel timeline, we observe unusually long initial response times (300ms-9s) despite low network latency (30ms ping). The delays are particularly noticeable on database-intensive pages like concrete5's Dashboard.

# Apache 2.2 prefork settings
StartServers         10
MinSpareServers      10
MaxSpareServers      20
MaxClients           250
MaxRequestsPerChild  4000

The MySQLTuner report reveals several urgent issues:

[!!] Query cache is disabled
[!!] Temporary tables created on disk: 47%
[!!] Thread cache is disabled
[!!] Table cache hit rate: 6%

Add these to your my.cnf and restart MySQL:

query_cache_size = 32M
query_cache_limit = 2M
tmp_table_size = 64M
max_heap_table_size = 64M
thread_cache_size = 8
table_open_cache = 512
table_definition_cache = 512

For APC (Alternative PHP Cache), ensure proper installation:

# emerge dev-php/pecl-apc
# Add to php.ini:
extension=apc.so
apc.shm_size=128M
apc.num_files_hint=10000
apc.user_entries_hint=10000
apc.max_file_size=5M
apc.stat=0  # Disable stat checking in production

Consider switching from prefork to worker MPM for better performance:

# /etc/portage/package.use
www-servers/apache worker mpm

Then rebuild Apache with:

# emerge -av apache

Add these to concrete5's config/site.php:

define('ENABLE_CACHE', true);
define('ENABLE_BLOCK_CACHE', true);
define('ENABLE_OVERRIDE_CACHE', true);
define('CACHE_SITEMAP', true);

Install and run apachetop for real-time monitoring:

# emerge app-admin/apachetop
# apachetop -f /var/log/apache2/access_log

When migrating a concrete5 CMS installation to a Gentoo-based VPS running Apache 2.2, PHP5, and MySQL 5, I encountered consistently high response times ranging from 300ms to 8-9 seconds. The issue wasn't network-related (ping times ~30ms) but appeared to stem from server configuration.

The Apache prefork settings were:


StartServers         10
MinSpareServers      10
MaxSpareServers      20
MaxClients           250
MaxRequestsPerChild  4000

MySQLTuner revealed several critical issues:

[!!] Total fragmented tables: 15
[!!] Maximum possible memory usage: 219.7M (93% of installed RAM)
[!!] Query cache is disabled
[!!] Temporary tables created on disk: 47%
[!!] Thread cache is disabled
[!!] Table cache hit rate: 6%

First, I implemented these MySQL configuration changes in my.cnf:

query_cache_size = 32M
tmp_table_size = 64M
max_heap_table_size = 64M
thread_cache_size = 8
table_open_cache = 512
innodb_buffer_pool_size = 128M  # If using InnoDB
key_buffer_size = 32M           # For MyISAM

Then ran table optimizations:

mysqlcheck -o -A -u root -p

For Apache, I switched from mod_php to PHP-FPM with these changes:

# In httpd.conf
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

    SetHandler "proxy:fcgi://127.0.0.1:9000"


# In php-fpm.conf
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15

For APC (Alternative PHP Cache), I configured:

apc.enabled=1
apc.shm_size=128M
apc.num_files_hint=10000
apc.user_entries_hint=10000
apc.ttl=7200
apc.user_ttl=7200
apc.gc_ttl=3600
apc.stat=0

Added these to concrete5's configuration:

define('ENABLE_CACHE', true);
define('ENABLE_BLOCK_CACHE', true);
define('ENABLE_OVERRIDE_CACHE', true);
define('CACHE_LIFETIME', 86400);

Set up this monitoring script to track performance:

#!/bin/bash
while true; do
    mysql -e "SHOW STATUS LIKE 'Threads_connected';"
    mysql -e "SHOW STATUS LIKE 'Queries';"
    ps aux | grep httpd | wc -l
    netstat -an | grep :80 | wc -l
    sleep 5
done
1. MySQL memory parameters optimized
2. Tables defragmented
3. Query cache enabled
4. PHP-FPM implemented
5. APC properly configured
6. concrete5 caching enabled
7. Monitoring in place