Apache Optimization for a Low-Memory VPS on Ubuntu 24.04
Make Apache fit a small VPS on Ubuntu 24.04: event MPM with PHP-FPM, worker limits sized from measured memory, and fewer modules.
On a VPS with 1 GB of memory or less, a default Apache setup with PHP can use up all the RAM under a small burst of traffic, push the server into swap and stop answering. This guide shows you how to make Apache 2.4 on Ubuntu 24.04 LTS fit into a small server: switch to the event MPM with PHP-FPM, size the worker limits from measured memory use, and trim modules and settings you do not need. The steps are the same on Ubuntu 22.04 (with PHP 8.1 instead of 8.3) and on Debian 12 (PHP 8.2).
Ubuntu 14.04 is end-of-life. Two things from guides of that era no longer apply: the apache2-mpm-worker package does not exist any more (the MPMs are modules inside the apache2 package and you switch them with a2dismod and a2enmod), and the prefork MPM with mod_php is no longer the setup to tune first.
Requirements
- Ubuntu 24.04 or 22.04 with Apache installed from the distribution packages.
- A user with
sudorights. - A backup of
/etc/apache2before you change it:sudo cp -a /etc/apache2 /etc/apache2.bak
Step 1: Measure before you change anything
See how much memory is really available. The available column is what matters:
free -m
Find out which MPM Apache is using and which modules are loaded:
a2query -M
sudo apache2ctl -M
Show the memory use of each Apache process and the average, in megabytes:
ps -ylC apache2 --sort=rss | awk 'NR>1 {sum+=$8; n++; print $8/1024 " MB"} END {if (n) print "average: " sum/n/1024 " MB"}'
If a2query -M prints prefork and apache2ctl -M lists a php module, PHP is running inside every Apache process. Each process then carries the PHP interpreter even when it serves an image or a CSS file. Fixing that is the biggest single saving.
Step 2: Switch to the event MPM and PHP-FPM
With the event MPM, a few Apache processes each run many threads, and idle keep-alive connections are parked on a listener thread instead of occupying a worker. PHP moves into a separate PHP-FPM pool that you can size on its own. If your site does not use PHP, do only the MPM part.
Install PHP-FPM:
sudo apt update
sudo apt install php-fpm
Disable mod_php and prefork, then enable event and the FastCGI proxy. On Ubuntu 24.04 the PHP version is 8.3; adjust the two version numbers on other releases:
sudo a2dismod php8.3 mpm_prefork
sudo a2enmod mpm_event proxy_fcgi setenvif
sudo a2enconf php8.3-fpm
Test the configuration and restart:
sudo apache2ctl configtest
sudo systemctl restart apache2
Confirm the result. The first command should print event, and a PHP page on your site should still work:
a2query -M
systemctl status php8.3-fpm --no-pager
PHP settings you changed in /etc/php/8.3/apache2/php.ini are not read by PHP-FPM. Copy them to /etc/php/8.3/fpm/php.ini. php_value lines in .htaccess files also stop working with PHP-FPM and must be removed or moved to a .user.ini file.
Step 3: Limit Apache's workers
Edit the event MPM settings:
sudo nano /etc/apache2/mods-available/mpm_event.conf
A conservative starting point for a 1 GB server:
<IfModule mpm_event_module>
StartServers 1
ServerLimit 2
ThreadsPerChild 25
MinSpareThreads 25
MaxSpareThreads 50
MaxRequestWorkers 50
MaxConnectionsPerChild 1000
</IfModule>
MaxRequestWorkersis the number of requests processed at the same time. With event it cannot exceedServerLimit×ThreadsPerChild. Apache's default is 400, which is far more than a small server can back with memory once PHP is involved.ServerLimitcaps the number of child processes.MaxConnectionsPerChildrecycles a process after that many connections, which contains slow memory leaks. The default0means never.
Requests above the limit wait in the listen queue instead of starting more processes. That is the point: a short wait is better than a server that swaps.
Step 4: Size the PHP-FPM pool
With PHP-FPM the memory goes to the PHP workers, so this is where the arithmetic belongs. Let the site run for a while, then measure the average PHP worker:
ps -ylC php-fpm8.3 --sort=rss | awk 'NR>1 {sum+=$8; n++} END {if (n) print "average: " sum/n/1024 " MB"}'
Work out the limit: take the total RAM, subtract what the database, Apache and the system need, and divide the rest by the average worker size. For example, if 400 MB is left for PHP and a worker averages 50 MB, the limit is 8.
sudo nano /etc/php/8.3/fpm/pool.d/www.conf
Set these directives (they already exist in the file; change the values):
pm = ondemand
pm.max_children = 8
pm.process_idle_timeout = 10s
pm.max_requests = 500
ondemand starts workers only when requests arrive and stops them after the idle timeout, which suits a low-traffic site on a small server. pm.max_children is the hard limit on simultaneous PHP requests. pm.max_requests restarts a worker after that many requests. Apply the change:
sudo systemctl restart php8.3-fpm
If you must stay on prefork and mod_php
Some old applications depend on mod_php. In that case, apply the same arithmetic to Apache itself, as the Apache performance documentation describes: divide the memory you can give Apache by the average Apache process size from step 1 and use the result as MaxRequestWorkers in /etc/apache2/mods-available/mpm_prefork.conf:
<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 2
MaxSpareServers 4
MaxRequestWorkers 10
MaxConnectionsPerChild 500
</IfModule>
Step 5: Disable modules you do not use
List the enabled modules:
ls /etc/apache2/mods-enabled/
Disable one, test and reload. For example, if you do not want directory listings or the server status page:
sudo a2dismod autoindex status
sudo apache2ctl configtest
sudo systemctl restart apache2
a2dismod warns you before disabling modules that the base configuration relies on; do not force those. If configtest reports an unknown directive, a site still needs the module, so re-enable it with sudo a2enmod followed by the module name. Keep rewrite and ssl if your sites use pretty URLs or HTTPS, which is nearly always the case. Expect a modest gain from this step compared with steps 2 to 4.
Step 6: Smaller savings
Shorten keep-alive so idle browsers release their connection sooner. In /etc/apache2/apache2.conf:
KeepAlive On
KeepAliveTimeout 2
If your application does not need .htaccess files, set AllowOverride None for the document root in your virtual host. Apache then stops looking for a .htaccess file in every directory of every request.
Lowering LogLevel from warn to error saves very little and hides useful warnings; leave it. Reload after any of these changes:
sudo apache2ctl configtest
sudo systemctl reload apache2
Verify under load
Watch memory while the site is busy. The available figure should stay well above zero and swap use should not grow:
free -m
Check whether Apache or PHP-FPM hit their limits:
sudo grep MaxRequestWorkers /var/log/apache2/error.log
sudo grep max_children /var/log/php8.3-fpm.log
An occasional hit during a spike is fine. If the limits are reached all the time and free -m still shows spare memory, raise them a little and measure again. If there is no spare memory, the server is too small for the workload, or the application needs caching.
Frequently asked questions
Should I switch to Nginx instead?
Not for memory reasons alone. With the event MPM and PHP-FPM, Apache's own footprint is small and most of the memory goes to PHP either way. Switching web servers means rewriting your .htaccess rules; tune the PHP pool first.
Does adding swap solve the problem?
A small swap file is a useful safety net that prevents the kernel from killing processes during a brief spike. It is not a substitute for limits: the Apache documentation is explicit that a web server should never have to swap, because every request then becomes slow.
What is the difference between the worker and event MPMs?
Both use threads. Event is based on worker but hands idle keep-alive connections to a dedicated listener thread, so worker threads are only busy while they process a request. On current Ubuntu releases event is the one to use.
What happens to visitors when the limits are reached?
New connections wait in a queue until a worker is free, so pages load slower for a moment. If the queue fills, connections are refused. Both are better than an out-of-memory server that answers nobody.