EC2 WordPress Behind CloudFront Part 3

There’s 2 more parts that’s really important to using a t3.micro instance with 20GB of disk space, and that’s to use swap memory, and to reduce the number of php processes.

Since t3.micro only has 1GB of RAM, we want to give it 4GB of swap memory. The swap memory will be much slower since it’s using the disk, but it will prevent the server from seizing once it runs out of the 1GB of RAM. This will leave 16GB of disk space for actual data, which should be enough unless you’re loading a lot of media.

To do this, run:
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

This will create the swap file, set proper permissions, set up the swap space, enable the swap file, and make it permanent by persisting after reboot.

Next we limit the number of php-fpm processes that gets generated. Each process uses up a lot of memory, and can quickly overwhelm the server if not limited. My WordPress server is running on AL2023, so the config file www.conf is at /etc/php-fpm.d/ and might be different for you if you’re running a different server.

Set the following parameters:
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 100

Save the file then restart php:
sudo systemctl restart php-fpm

With all that, your WordPress server will be using a lot less memory, and can run on AWS EC2’s t3.micro with no issues!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *