Troubleshooting AWS Lightsail server performance issues

How to Fix AWS Lightsail Instance Crashes: Increase Swap, Optimize Apache & OPcache

AWS Lightsail is Amazon's entry-point cloud hosting, aimed at customers looking for a simple way to host virtual private servers. Featuring flat-rate, all-in-one pricing, one-click app stacks, SSD storage included, static IPs, and built-in DNS management, Lightsail enables you to spin up websites, blogs, and small applications in minutes without having to worry about complex setups. But this ease of use is traded off for flexibility: Lightsail has a pre-defined set of instance types, restricted networking and storage configurations, and less out-of-the-box integration with more specialized AWS services. For users who want fine-grained control over scaling policies, specialized instance families, or tighter integration into the enormous AWS ecosystem, EC2 offers a level of power that you'll eventually outgrow with Lightsail. But for a lot of small-scale projects, Lightsail's simplicity and predictable billing are precisely what you're looking for.

However, another drawback of using Lightsail is that it does not take much for it to become unresponsive, crash and require a reboot. This is especially true when using an instance with minimal specs, e.g. 1 GB RAM, 2 vCPUs. With the ever presence of A.I. scrapers, hosting something like a phpBB forum on such an instance can result in constant headaches.

Despite this, there are ways to improve the situation.

Finding The Cause

The most important first step is to discover what is causing the crashes. There are several steps to this, keeping in mind you will need to use sudo for many of these steps:

System Resource Usage

  • Use top, htop, or glances to monitor CPU, memory, and disk I/O.
  • Check if swap usage is excessive (see KiB Swap when running top, use free -h or swapon --show). If you haven't create any swap space (you should) then see below to create it.

System Logs

  • Look at /var/log/syslog, /var/log/kern.log, and /var/log/apache2/error.log (or Nginx if used).
  • Search for any messages near the time of the crash for out-of-memory (OOM) errors, kernel panics, or service failures.

OOM Logging

  • Verify that the kernel is logging out-of-memory events. You might see messages like oom-killer in dmesg or logs.
  • To find these messages: dmesg | grep -i kill. If you get an Operation not permitted error then you forgot to use sudo.

phpBB Logs

If you're using phpBB then it's also important to check its logs. That can be done within phpBB itself (via the ACP) or by looking at the logs in store/logs/.

Swap Space

If you are not using swap space, then consider using it, especially if you are only using 1GB of RAM. For Ubuntu, the following steps create 2GB of swap space, which is how much you'll want if you only have 1GB of RAM. If you have 4GB then 2-4GB of swap space is sufficient. Do not use more than twice as much RAM.

  • If you already have swap space, and want to increase it, switch it off first: sudo swapoff /swapfile
  • To create, or increase, your swap space use sudo fallocate -l 2G /swapfile or sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
  • Set the permissions using sudo chmod 600 /swapfile
  • Initialize it via sudo mkswap /swapfile
  • Enable it using sudo swapon /swapfile
  • Verify it is enabled with swapon --show

To make sure the swap space is used when you reboot, you need to add it to /etc/fstab (if you haven't already): echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Apache Tweaking (MPM)

mpm_prefork.conf is only read and applied when Apache is running with the prefork Multi-Processing Module (MPM) enabled. To check if it is: apache2ctl -M | grep mpm_

It is possible that the Apache MPM module is not optimized, and an easy way to do this is to change the MaxRequestWorkers value in mpm_prefork.conf. Each Apache child process (especially with PHP) can consume 20–50 MB or more of memory. Therefore, if this value is 150, for example, then it means it could exceed 3GB. If you only have 1GB of RAM (with 2GB of swap space) then you're already over your limit.

If you're using 1GB of RAM, consider setting this value to a much lower value, e.g. 15. To make the change edit your MPM configuration file (yours may be in a different place):

sudo nano /etc/apache2/mods-enabled/mpm_prefork.conf

With 1GB of RAM, change it to use the following (the MaxRequestWorkers value below may be too low if you have moderate traffic, so consider a higher value, e.g. 30):

StartServers             2
MinSpareServers          2
MaxSpareServers          5
MaxRequestWorkers        15
MaxConnectionsPerChild   300

With 4GB of RAM, change it to use the following:

StartServers             5
MinSpareServers          5
MaxSpareServers          10
MaxRequestWorkers        75
MaxConnectionsPerChild   1000

After changing these values, you need to restart Apache (sudo systemctl restart apache2).

OPcache

If you're using phpBB, or you are using PHP extensively, then check if you are using Zend OPcache. The first step is to locate your php.ini file:

php -i | grep 'Loaded Configuration File'
php -i | grep 'Configuration File (php.ini) Path'

Depending on your PHP version, it will output something like Loaded Configuration File => /etc/php/7.4/cli/php.ini

Alternatively, you could create a temporary PHP script that contains phpinfo(). Open that in a web browser to see the paths. You should delete the script immediately after using it as it can be a security risk.

It's common to have multiple php.ini files (one for CLI, one for Apache, one for FPM, etc.). You must edit the one that corresponds to how your phpBB forum is running, for example. For web applications, this usually means the apache2 or fpm version. PHP also loads additional configuration files from a conf.d directory (e.g. /etc/php/7.4/apache2/conf.d/). These files can override settings in the main php.ini. Be aware of them if your changes aren't taking effect.

To enable OPcache, edit your appropriate php.ini file and search for the line containing opcache.enable. If it's set to 0, or commented out with a semicolon (;), change it to 1 and remove the semicolon.

The default values are a good start, but if you're using phpBB then you may want to change them to the following:

; Enables the opcode cache
opcache.enable=1

; The amount of memory to be consumed by OPcache
opcache.memory_consumption=128 ; Start with 128MB, adjust based on RAM and usage

; The maximum number of files that can be stored in the cache
opcache.max_accelerated_files=10000 ; Good starting point, increase if many files

; How often (in seconds) to check script timestamps for updates
; 0 means check every request, which is bad for performance.
; Set to a higher value for production (e.g., 60 or more)
opcache.revalidate_freq=60

; If enabled, OPcache will check for updated scripts every opcache.revalidate_freq seconds.
; When disabled, you must manually clear the cache (e.g., by restarting PHP-FPM/Apache)
; after code changes. For production, setting this to 0 is common for max performance.
opcache.validate_timestamps=1 ; Set to 0 if you prefer manual cache clearing on deployment

; If disabled, all documentation comments will be discarded from the opcode cache
; to reduce the size of the optimised code. Some frameworks rely on comments (e.g., for annotations).
; For phpBB, it's generally safe to set this to 0 for a small memory saving.
opcache.save_comments=0

; Enables the opcode cache for the CLI version of PHP
opcache.enable_cli=0 ; Keep 0 for web server, only enable if you run phpBB CLI tools often

After updating the php.ini file you must restart Apache, e.g. sudo systemctl restart apache2

Content Delivery Network (CDN)

One way to reduce the load on your server is to use a Content Delivery Network (CDN) like Cloudflare. Although a CDN does not help with dynamic pages, e.g. PHP, and indeed you should be very careful when using it with something like phpBB, it can help with static assets like images, JS and CSS.

If you're using a CDN then configure it to cache those static assets. For example, with phpBB set it to cache /styles/*, /download/file.php* and /images/avatars/*. For styles, you can probably using a TTL of 1 month. Make sure you do not cache /ucp.php*, /mcp.php*, /adm/*, /posting.php*, any URL's with 'sid=' or the login/logout pages.

There are other Cloudflare settings that may help:

  • Auto Minify: Enable for CSS, JavaScript, and HTML
  • Rocket Loader: To help JavaScript performance
  • Polish: Optimize images automatically (this may only help with large images)
  • Brotli Compression: Enable for better text compression

More RAM, More vCPUs

Ultimately you may need to simply increase the resources your instance has. If you're only using 1GB of RAM then you may need to upgrade to 4GB, for example. There is only so much optimization can achieve. As the saying goes: you get what you pay for.

It's easy to upgrade in Lightsail. Simply create a new instance using a snapshot. However, downgrading is not so simple.

Conclusion

You may have created a Lightsail instance to host an SFTP server, or make use of SyncBack Touch, so you can backup to or from the server with SyncBackPro. SyncBackPro can also download files from a web server using HTTP.

Whatever the reason, we hope this article helped you resolve any issues that you may have with your Lightsail instance. By correctly diagnosing the cause, creating a swap file, and carefully tuning your Apache and OPcache settings, you can dramatically improve the stability of even low-specification instances.

Noted Customers

© 2003-2025 2BrightSparks Pte. Ltd.  | Home | Support | Privacy | Terms | Affiliate Program

Home | Support | Privacy | Terms
© 2003-2025 2BrightSparks Pte. Ltd.

Back to top