Running Drupal 8 on LAMP with 512MB RAM

If you're wondering how much RAM you need for a Drupal 8 installation, you might look to the minimum requirements at drupal.org. However neither of the requirements pages of Drupal 7 nor Drupal 8 list an amount. (Both however require a minimum PHP limit of 32MB.)

The amount of memory required is dependent on a number of factors such as the server and database software you choose, the modules you use, and the amount of visitors to your site.

This is a very basic blog which I setup using a VPS with 512MB RAM. Despite being a vanilla Drupal 8 installation with few extra contrib modules, I began periodically receiving a white screen with this text on it:

Drupal 8 error

I began reading through the logs to determine what could be causing the issue, but it seemed to be occurring after various different operations.

I began examining the VPS and noticed that it had been setup by the provider without a swap file (though with Ubuntu pre-installed).

After enabling swap, Drupal was stable and there are no more issues to report.

Here are the steps to replicate this process:

Check whether swap is enabled on your system:

sudo swapon -s

To create a new partition, dd is usually used. However, you can create a partition faster using fallocate if you're not concerned about zeroing out the space beforehand.

For systems with a low amount of RAM,  it is generally suggested to use a swap file that is double the RAM. Because I have alot of extra hard drive space, I'll use 2GB.

sudo fallocate -l 2G /swapfile

For security reasons, access to swap should be limited to root:

sudo chmod 600 /swapfile

If you're not familiar with Linux permissions, the digits represent the owner, the group, and all other users, respectively. Each number represents the sum of the following permissions:

0 = No Permission

1 = Execute

2 = Write

4 = Read

So 600, means that the owner has read+write permissions (2 + 4 = 6), while the group and other users have 0 permissions.

Then setup the swap space:

sudo mkswap /swapfile

Enable swap:

sudo swapon /swapfile

To ensure that swap persists across reboots, open up /etc/fstab with your favorite editor, and append the following line:

/swapfile none swap sw 0 0

 

For best results, you might also want to adjust the swappiness which determines how aggressively the operating system swaps data between RAM and swap. Ubuntu comes with a default of 60 which means that when the RAM reaches 40% capacity, Linux will begin moving data to swap.  But for servers, you may want to lower the amount.

To view it:

cat /proc/sys/vm/swappiness

If you want to change it, e.g. to 20, you can run this command, which will change it until poweroff/reboot.

sudo sysctl vm.swappiness=20

To modify the value permanently, append the swappiness command (vm.swappiness=value) to the end of the /etc/sysctl.conf file.