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개의 좋아요

기본값인 2GB 대신 4GB의 더 큰 스왑파일을 설정하려고 시도해 왔지만, 위 스크립트를 실행할 때 다음과 같은 오류가 발생했습니다:

rm: cannot remove `swapfile’: Operation not permitted

이를 우회하기 위해 먼저 다음 작업을 수행해야 했습니다:

  1. 서버 재시작 (스왑파일 사용량이 많아 swapoff 명령을 실행할 수 없었음)
  2. swapoff -a -v 실행
3개의 좋아요

비슷한 이유로 저는 두 개의 스왑파일을 사용하고 있습니다:

# 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

두 번째 스왑 파일을 만드는 것이 재부팅을 피하는 현명한 방법이 될 수 있습니다. 두 번째 스왑 파일을 만들고 활성화한 후, 첫 번째 파일을 삭제하면 됩니다.

4개의 좋아요

즉, 메모리가 2GB 미만인 경우 설치 과정에서 이미 2GB 스왑 파일이 생성되므로, 이 가이드를 따라 2GB 스왑 파일을 만들 필요가 없다는 말씀이신가요?

맞습니다. 하지만 서버 설정을 변경하거나 마이그레이션을 수행하는 경우, 이 부분을 직접 관리해야 할 수 있습니다. (동시에 설치 과정에서는 관리되지 않는 중요한 커널 설정 두 가지가 있으며, 변경이 필요합니다. MKJ의 의견이 담긴 Discourse 배포 구성을 참조하세요.)

10개의 게시물이 새로운 주제로 분리되었습니다: 높은 재빌드 메모리 요구사항: 2025년 4월 에디션