Créer un fichier d'échange pour votre serveur Linux

: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 « J'aime »

J’ai essayé de configurer un fichier d’échange plus grand (4 Go au lieu des 2 Go par défaut), mais j’ai rencontré ceci en exécutant le script ci-dessus :

rm : impossible de supprimer `swapfile’ : Opération non permise

Pour contourner ce problème, j’ai dû faire ceci d’abord :

  1. Redémarrer le serveur (il ne me permettait pas de swapoff, probablement en raison d’une utilisation intensive du fichier d’échange)
  2. swapoff -a -v
3 « J'aime »

Pour des raisons similaires, j’utilise deux 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

Un deuxième fichier d’échange pourrait être un moyen astucieux d’éviter le redémarrage. Créez simplement un deuxième fichier d’échange, activez-le, puis supprimez le premier.

4 « J'aime »

Nous n’aurons donc pas besoin de suivre ce guide pour créer le fichier swap de 2 Go puisque l’installation le fait déjà si nous avons moins de 2 Go de mémoire ?

Oui - mais si vous modifiez la configuration de votre serveur, ou peut-être si vous effectuez une migration, vous devrez peut-être vous en occuper vous-même. (Dans le même temps, il y a deux configurations importantes du noyau à modifier que l’installation ne gère pas - voir MKJ’s Opinionated Discourse Deployment Configuration

10 messages ont été déplacés vers un nouveau sujet : Exigences élevées en matière de mémoire de reconstruction : édition d’avril 2025