The ambition to run a fully functional WordPress site on the AWS Free Tier EC2 instance is admirable, but it comes with a glaring weakness: memory.
While using a service like Lightsail might offer a smoother experience, it usually pushes you into a paid plan immediately. If you are determined to maximize the true zero-cost benefits of the EC2 Free Tier (typically a t2.micro or t3.micro specification), you must proactively optimize.
The Problem: Why Your Site Keeps Crashing
The standard Free Tier EC2 instance offers only 1 GiB of RAM. WordPress, combined with a PHP server (like PHP-FPM) and a database, can easily devour this memory.
The result of this memory inadequacy is sudden, catastrophic failure. You’ll notice this most acutely when performing resource-intensive tasks, such as uploading images or running updates. The server utility spikes, memory is exhausted, and the entire site (and often your SSH session) locks up, forcing an instance reboot.
To avoid this, we must allocate SWAP space. SWAP is virtual memory, a dedicated space on the hard drive (EBS volume) that the kernel can use when physical RAM is full. While slower than true RAM, this buffer prevents your server from crashing and dramatically improves stability under load.
The goal is to get maximum stability and resource utilization out of the box. Let’s start by allocating the crucial SWAP space.
Step 1: Checking Current Memory and Swap Status
Before making changes, check your current memory situation.
# Check physical memory and current swap
free -h
# Check swap status (virtual memory)
swapon --show
# Optional: Check top memory consumers (useful if debugging a crash)
top -bn1 | head -20
If the swapon --show command returns no output, or free -h shows “Swap: 0B,” you have confirmed that no swap is currently configured.
Step 2: Allocating 2GB of Persistent SWAP Memory
Given the 1GiB of physical RAM, allocating 2GiB of swap space is a safe and generous amount that will prevent most crashes related to burst memory use.
Run the following commands sequentially:
# 1. Create a 2GB file to be used for swap using fallocate (faster)
sudo fallocate -l 2G /swapfile
# 2. Secure the file by limiting access to the root user
sudo chmod 600 /swapfile
# 3. Mark the file as a swap area
sudo mkswap /swapfile
# 4. Activate the swap space for the current session
sudo swapon /swapfile
# 5. Make the swap configuration permanent so it survives a reboot
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Step 3: Verify the New Swap Space
After running the commands, verify that the swap space is active and correct:
# Check memory again. You should now see 2.0G in the Swap row.
free -h
With 2GiB of swap allocated, your WordPress site now has a much larger safety buffer, making it significantly more resilient to the typical Free Tier memory limitations.
Other recommendation for WordPress:
Web Server Stack Optimization
To truly reduce resource usage, consider a more lightweight stack.
- Switch from Apache to Nginx: If you are using Apache, moving to Nginx can significantly reduce memory overhead. Nginx is excellent for serving static content and acting as a reverse proxy.
- Use PHP-FPM: Ensure you are running PHP with PHP-FPM instead of as an Apache module (mod_php). Crucially, adjust the PHP-FPM pool settings (Eg.
pm.max_children,pm.start_servers) to match your 1GB RAM limit. Over-allocating here is a common mistake that defeats the purpose of adding swap. - Use a Lightweight Database: While MySQL is common, consider MariaDB, which is generally considered more resource efficient.
WordPress Specific Optimizations
The largest performance gains will come from within WordPress itself.
- Caching Plugin: Install and configure a robust caching plugin like WP Rocket, W3 Total Cache, or LiteSpeed Cache. This reduces the number of PHP and database calls, dramatically lowering the load on the server.
- Object Caching: If your cache plugin supports it, use an in-memory object cache like Redis or Memcached. This stores database query results in RAM (or now, a part of your swap/RAM combination), making them instant.
- Image Optimization: Use a plugin to serve WebP images and compress all existing media.
- Minimal Plugins: Ruthlessly audit and remove unnecessary or bloated plugins.
