With WordPress now fully functional behind CloudFront, the next challenge was optimizing our t3.micro EC2 instance. With only 1GB of RAM and 20GB of disk space, we needed to be smart about resource management. Two critical configurations made all the difference.

1. Swap Memory

The t3.micro's 1GB of RAM isn't always enough for WordPress, especially when PHP processes spike. Adding swap memory gives the system a safety net when physical memory runs out. Here's how we set up a 4GB swap file:

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

The last line ensures the swap file persists across reboots by adding it to /etc/fstab.

2. PHP-FPM Process Limiting

By default, PHP-FPM can spawn more processes than a t3.micro can handle, leading to memory exhaustion. We edited /etc/php-fpm.d/www.conf to limit the number of PHP processes:

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 100

These settings keep PHP-FPM from consuming all available memory. The pm.max_children = 5 setting caps the total number of worker processes, while pm.max_requests = 100 ensures individual processes are recycled before they can accumulate too much memory.

After making these changes, restart PHP-FPM:

sudo systemctl restart php-fpm

With both of these configurations in place, our t3.micro instance runs WordPress reliably without running into the memory issues that plagued us before.