If your MySQL database or Web Server randomly crashes without warning, the culprit is usually running Out of Memory.

When your VPS runs out of RAM, the Linux kernel has to make a tough decision: it must "kill" a process to save the rest of the system. Usually, it kills the heaviest process—which is almost always your Database.

The solution is Swap Space. Swap acts as "emergency RAM" on your hard drive. If your real RAM gets full, Linux moves inactive data to the Swap file instead of crashing your server.


Prerequisites

  • A Joy Services VPS (especially if you have 1GB or 2GB of RAM).
  • Root or Sudo access.

Step 1: Check Current Swap Status

First, let's see if your system already has swap configured.

1. Run the check command:

Bash
root@joy:/home/joy# sudo swapon --show

Expected Output:

If nothing comes back (empty line), you have no swap. If you see a list like below, you are already using swap.

Terminal Output (Example)
NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2

Step 2: Create the Swap File

We will create a 2GB file on your hard disk to use as swap. 2GB is generally a good safety net for most small-to-medium servers.

1. Create the file:

Bash
root@joy:/home/joy# sudo fallocate -l 2G /swapfile
Possible Error: "fallocate failed: Operation not supported" If you see this error, your file system doesn't support fallocate. Don't panic! Use this command instead:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

2. Verify it was created:

Bash
root@joy:/home/joy# ls -lh /swapfile

Expected Output:

Terminal Output
-rw-r--r-- 1 root root 2.0G Dec 7 10:00 /swapfile

Step 3: Secure the Swap File

Swap space contains data from your RAM. We must ensure only the root user can read this file.

1. Lock down permissions:

Bash
root@joy:/home/joy# sudo chmod 600 /swapfile

2. Verify permissions:

Bash
root@joy:/home/joy# ls -lh /swapfile

Expected Output:

Notice the -rw------- permission. This means it is secure.

Terminal Output
-rw------- 1 root root 2.0G Dec 7 10:05 /swapfile

Step 4: Enable the Swap File

Now we tell the operating system to treat this standard file as Swap memory.

1. Mark the file as swap space:

Bash
root@joy:/home/joy# sudo mkswap /swapfile

Expected Output:

Terminal Output
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=06b36449-650a-4503-91c6-455243161725

2. Turn the swap on:

Bash
root@joy:/home/joy# sudo swapon /swapfile
Possible Error: "swapon: /swapfile: insecure permissions" If you see this warning, it means you skipped Step 3. Run this command immediately to fix it:
sudo chmod 600 /swapfile

3. Verify it is active:

Bash
root@joy:/home/joy# sudo swapon --show

Expected Output:

Terminal Output
NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2

Step 5: Make It Permanent

If you reboot your VPS right now, the swap will disappear. We need to add it to the fstab file so it loads on boot.

1. Back up the fstab file (Safety First):

Bash
root@joy:/home/joy# sudo cp /etc/fstab /etc/fstab.bak

2. Add the swap information to the end of the file:

Bash
root@joy:/home/joy# echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

3. Verify the file (Critical):

If the fstab file is corrupt, your server won't boot. Run this command to check for errors:

Bash
root@joy:/home/joy# sudo mount -a
Note: If the command above outputs nothing, you are safe! If it shows an error, check the file again before rebooting.

Step 6: Adjust "Swappiness" (Optimization)

By default, Ubuntu moves data to swap fairly often (value: 60). We want to lower this to 10 so it uses real RAM as much as possible.

1. Check current value:

Bash
root@joy:/home/joy# cat /proc/sys/vm/swappiness

Expected Output:

Terminal Output
60

2. Change it to 10:

Bash
root@joy:/home/joy# sudo sysctl vm.swappiness=10

3. Make this setting permanent:

Bash
root@joy:/home/joy# sudo nano /etc/sysctl.conf

Scroll to the bottom and add this line:

vm.swappiness=10

Save and exit (Ctrl+O, Enter, Ctrl+X).


Summary

Your Joy Services VPS is now significantly more stable. You have added a 2GB safety buffer that will prevent MySQL and Web Servers from crashing during traffic spikes.