AI development environment on Ubuntu
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:
Expected Output:
If nothing comes back (empty line), you have no swap. If you see a list like below, you are already using swap.
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:
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:
Expected 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:
2. Verify permissions:
Expected Output:
Notice the -rw------- permission. This means it is secure.
-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:
Expected Output:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=06b36449-650a-4503-91c6-455243161725
2. Turn the swap on:
sudo chmod 600 /swapfile
3. Verify it is active:
Expected 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):
2. Add the swap information to the end of the file:
3. Verify the file (Critical):
If the fstab file is corrupt, your server won't boot. Run this command to check for errors:
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:
Expected Output:
60
2. Change it to 10:
3. Make this setting permanent:
Scroll to the bottom and add this line:
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.