Create a swapfile for your Linux server

:bulb: For servers with <= 2GB 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 4GB or more 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. write out a 2GB file named ‘swapfile’

     dd if=/dev/zero of=/swapfile bs=1k count=2048k
    
  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 | tee -a /etc/sysctl.conf
    

The whole thing as a single copy and pastable script:

install -o root -g root -m 0600 /dev/null /swapfile
dd if=/dev/zero of=/swapfile bs=1k count=2048k
mkswap /swapfile
swapon /swapfile
echo "/swapfile       swap    swap    auto      0       0" | tee -a /etc/fstab
sysctl -w vm.swappiness=10
echo vm.swappiness = 10 | tee -a /etc/sysctl.conf

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

Check documentPerform check on document:
「いいね!」 95

デフォルトの2GBではなく4GBの大きなスワップファイルを設定しようとしていますが、上記のスクリプトを実行したときにこれが発生しました。

\u003e rm: `swapfile’ を削除できません: 操作が許可されていません

これを回避するために、まずこれを行う必要がありました。

  1. サーバーを再起動します(重いスワップファイルの使用により、swapoff が許可されなかったようです)。
  2. swapoff -a -v
「いいね!」 3

同様の理由で、2つのスワップファイルで実行しています。

# 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

2つ目のスワップファイルは、再起動を回避するための巧妙な方法になる可能性があります。2つ目のスワップファイルを作成し、有効にしてから、最初のファイルを削除します。

「いいね!」 4

そのため、インストール時にメモリが2GB未満の場合に自動的に行われるため、このガイドに従って2GBのswapファイルを作成する必要はないということですね?

はい。ただし、サーバー構成を変更した場合や、移行を実行した場合などは、ご自身で対応する必要があるかもしれません。(同時に、インストールでは対応しない重要なカーネル設定が2つあります。詳細はMKJ’s Opinionated Discourse Deployment Configurationを参照してください。)

10件の投稿が新しいトピックに分割されました: High rebuild memory requirements: April 2025 edition