Create a swapfile for your Linux server

:bulb: For servers with <= 4GB of RAM, running ./discourse-setup will prompt for and automatically create a 2GB swapfile.

Most cloud virtual machine providers do not set up swapfiles as part of their server provisioning.

In particular, upgrading Discourse produces a lot of memory pressure. With a swap file, rather than randomly terminating processes with an out of memory error, things will slow down instead. Having a swap file is a cheap insurance policy that protects you from many other load related failures.

This can be done at any time from the command line on your server.

Set up a 2GB swap file

We recommend a 2GB swap file for Discourse, unless your server has more than 4GB of memory.

In the shell you have opened to your droplet, do the following:

:warning: You will need to be root for much of this, so either be root or sudo -s before running these commands! Don’t forget to exit root afterwards.

  1. Create an empty swapfile

     install -o root -g root -m 0600 /dev/null /swapfile
    
  2. Allocate a 2GB file named ‘swapfile’

     fallocate -l 2G /swapfile
    
  3. tell linux this is the swap file:

     mkswap /swapfile
    
  4. Activate it

     swapon /swapfile
    
  5. Add it to the file system table so its there after reboot:

     echo "/swapfile       swap    swap    auto      0       0" | tee -a /etc/fstab
    
  6. Set the swappiness to 10 so its only uses as an emergency buffer

     sysctl -w vm.swappiness=10
     echo 'vm.swappiness = 10' > /etc/sysctl.d/30-discourse-swap.conf
    

The whole thing as a single copy and pastable script:

install -o root -g root -m 0600 /dev/null /swapfile
fallocate -l 2G /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile       swap    swap    auto      0       0" | tee -a /etc/fstab
sysctl -w vm.swappiness=10
echo 'vm.swappiness = 10' > /etc/sysctl.d/30-discourse-swap.conf

Last edited by @JammyDodger 2022-05-23T18:45:59Z

Check documentPerform check on document:
96 лайков

I’ve been trying to set up a larger swapfile (4GB instead of the default 2GB), but have run into this when running the script above:

rm: cannot remove `swapfile’: Operation not permitted

To get around this, I’ve had to do this first:

  1. Reboot the server (it wouldn’t let me swapoff, presumably due to heavy swapfile use)
  2. swapoff -a -v
3 лайка

For similar reasons, I run with two swapfiles:

# swapon
NAME                       TYPE  SIZE  USED PRIO
/var/local/swap/swapfile.0 file 1024M 44.9M   -3
/var/local/swap/swapfile.1 file 1024M 1024M   -2

A second swap file could be a clever way to avoid the reboot. Just create a second swap file, enable it, and then remove the first.

4 лайка

Значит, нам не нужно следовать этой инструкции для создания файла подкачки размером 2 ГБ, поскольку установка делает это автоматически, если у нас меньше 2 ГБ памяти?

Да, но если вы измените конфигурацию сервера или, возможно, выполните миграцию, вам, возможно, придется позаботиться об этом самостоятельно. (В то же время есть две важные конфигурации ядра, которые необходимо изменить, и установка не занимается ими — см. Мнение MKJ о конфигурации развёртывания Discourse)

10 сообщений были перенесены в новую тему: Высокие требования к памяти при перестройке: апрель 2025 года