Run Ghost on 1GB RAM without getting stuck - Solution 🥳

All of us know that Ghost runs on Node.js. When we choose a 1 GB VPS from Digitalocean, the VPS will get stuck because of its RAM usage. But here is the solution:

We have to create a swap partition on the server.

What is a swap?

Swap space is the area of a hard disk. It is a part of your machine’s virtual memory, which is a combination of accessible physical memory (RAM) and the swap space. Swap holds memory pages that are temporarily inactive. Swap space is used when your operating system needs physical memory for active processes but there isn’t enough physical memory available (that is, not being used). When this happens, inactive pages from the physical memory are then moved into the swap space, freeing up that physical memory for other uses. Note that the access time for a swap is slower, depending on the speed of the hard drive. It is not a complete replacement for physical memory. Swap space can be a dedicated swap partition, a swap file, or a combination of swap partitions and swap file(s).

Let’s create the swap.

Step 1: Swap Information Check-up

The size of swap space depends on the system’s RAM. So, it’s important to make sure your system has enough swap space before making one. Type the below command to check if the system has any swap configured:

$ sudo swapon --show

An output displayed means there is space available in your system to create swap space. However, no output is a signal indicating there is not enough available space to add swap space.

Use the free utility to verify that there is no active swap:

$ free -h

You’ll see the output as a null swap row:

Step 2: Hard Drive Partition Availability Check-up

Similar to checking the system availability, now let’s check our current disk usage to ensure that we have enough space. Use the df command to check the hard disk partition availability:

df -h

The device with / in the Mounted on column is our disk which is 11% use. You’ll see the output:

After checking the system’s requirements, let’s move forward and create a swap file on our filesystem.

Step 3: Create a Swap File
To create a swap file on our filesystem, allocate a file name as a swapfile in the root (/) directory using the fallocate program. Allocating a file size depends on your needs. To keep the tutorial simple, and easy to understand, we’re creating a 1G file and dedicating 1G of RAM. Use the sudo command to create a swap file:

sudo fallocate -l 1G /swapfile

Next, type this command to verify the reserved space:

$ ls -lh /swapfile

You’ll see the output:

Now that we have created a swap space in our system, let’s enable it in the next step.

Step 4: Enable the Swap File

Once we have the correct size available, let’s convert it into swap space. For security reasons, it is crucial to make our file accessible to root users and prevent accessibility from normal users. We’ll add restrictions so that users with root privileges can view the file and read its contents. Use the following root command to make the file only accessible to root users:

sudo chmod 600 /swapfile

Then, use the ls command to verify the changes:

$ ls -lh /swapfile

As you can see, only the root user has the read and write flags enabled:

After that, mark the file as swap space:

sudo mkswap /swapfile

You’ll see the output:

After that, enable the swap file and permit our system to start using it:

sudo swapon /swapfile

Next, verify the swap availability:

$ sudo swapon --show

You’ll see the output:

As we did in Step 1, we’ll check the free utility space once again:

free -h

You’ll see the output:

Our swap has been set up successfully and our operating system will begin to use it as necessary. In the next step, we will make the swap file permanent.

Step 5: Make the Swap File Permanent

By default, the server will not retain the swap settings automatically whenever we reboot. To change the default settings and play on the safe side, we will add the swap file to our /etc/fstab file. Use the sudo command to back up the /etc/fstab file:

sudo cp /etc/fstab /etc/fstab.bak

Next, add the swap file information to the end of your /etc/fstab file by typing:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

After that, let’s review some settings and make modifications so that we can tune our swap space.

Step 6: Swap Settings Modification

In this step, we’ll configure the settings and make some modifications. Let’s adjust the swappiness property and change the change cache pressure.

  • Adjust the Swappiness Property

The swappiness parameter is a tunable kernel parameter. It changes the balance between swapping out runtime memory and accounts for the exchange of data between swaps data out of RAM to the swap space. This parametric value is expressed in percentage, ranging from 0 up to 100.

The vm.swappiness value has a direct influence on the behaviour of system swappiness. The higher the vm.swappiness, the more the system will swap and vice-versa. If the value is close to zero, the kernel will not swap data which may lead to a reduction in performance. Permitting your system not to depend much on the swap is an excellent way to enhance its performance and gain robust responsiveness.

Alternatively, vm.swappiness that is closer towards 100 attempts to put more data in the swap and less on the physical memory. Unlike the low-to-mediocre vm.swappiness value, the high system swapping has very undesirable effects when there are loads of data being swapped actively in-and-out of the RAM.

Check the current swappiness value by typing:

cat /proc/sys/vm/swappiness

You’ll see the output:

You can also set the swappiness of your choice using the sysctl command. Let’s set the swappiness value to 15 by typing:

sudo sysctl vm.swappiness = 15

You’ll see the output:

Until the system is rebooted, the settings will remain as is. Add the line to our /etc/sysctl.conf file to set the value automatic at restart/reboot:

sudo nano /etc/sysctl.conf

You can add the following code at the bottom:

Vm.swappiness = 15

Then, save and close the file.

  • Adjust the Cache Pressure

Next, we are going to modify vfs_cache_pressure that will take care of inode and dentry information over other data. These vfs_cache_pressure settings also control the tendency of the kernel to reclaim the memory used for caching of the directory. Navigate the proc filesystem again to check the current value:

cat /proc/sys/vm/vfs_cache_pressure

You’ll see the output:

As the configuration is already done, our system will eliminate the inode information from the cache. Let’s set the sysctl vm.vfs_cache_pressure to a more stable setting like this:

sudo sysctl vm.vfs_cache_pressure = 60

You’ll see the output:

Similar to what we did in our swappiness setting, we can change the configuration here as well:

sudo nano /etc/sysctl.conf

At the bottom, insert a line indicating the new value you created:

Vm.vfs_cache_pressure = 60

Finally, save and close the file.

Conclusion

You can add swap space as a dedicated swap partition, a swap file, or a combination of both. You can create swap space and leverage its benefits to run applications seamlessly without much worrying about out-of-memory warnings. While creating swap space is an added advantage in systems with limited memory space, it must not be treated as an alternative for more RAM space. Remember, swap space is located on a hard disk therefore you may experience a slightly slow access time compared to physical memory.

Thanks me later for saving your server cost. :wink:

5 Likes

Expert sysop skills here, thank you! Combining this method with using Ghost on a CDN such as https://bunny.net/cdn/ should take care of all potential performance issues.

2 Likes

Thank you for your feedback. This is the best way that I have used to manage RAM for low budget VPSs

Hi,

Thank you @thimiraonline for this tutorial. I will try to follow it. It may be a solution to all my problems. I have several questions that I would like to try to ask here, hoping that someone can help me.

I am not a developer, but I believe in open source. So I got motivated a year ago to learn how to host a ghost blog on a Digital Ocean droplet. I followed a tutorial from @martinverbic who I thank.

I have different writing projects and stupidly, without thinking, I created 4 droplets. 40€/month currently, oops.

They all have several problems, two major ones that I can’t seem to solve.

  1. I imagine that the first one, the cpu spikes, could be solved by swap spaces. Is this the case? There is almost no traffic on my blogs and randomly I get downtime alerts.
  2. The second problem is this cli error that I encounter at every update. I am simply forced to manually remove Ghost from the server in order to do the update. @RyanF gave me this workaround, fortunately. It’s not unbearable, but okay. I can imagine something better for my future. Can swap space also solve this problem?
  3. Before adding swap space on my droplets, shouldn’t I migrate all my blogs to the same droplet? Or start from 0 on a droplet? Can we correctly separate the DNS zones anyway? Is the migration complicated? Any help is welcome. I am willing to pay for this topic. I specify that I hope to get a lot of traffic on each of my blogs in the long run. These are long term projects.
  4. Are mySQL and Nginx important in this optimization process? Cloudflare also ? Here is a topic where @jazmy is mentioning these points.

I then have 2 secondary questions:

A. Docker: I own 4 Ghost blogs, one n8n, one Baserow, 1 cal.com. All these tools are open source and Docker compatible. Is it worth it to stick to it? Will it be cheaper ? more efficient ? I’m afraid it’s too technical for me. But in the end I wonder if this is not the solution I should go for. Any help is welcome. I can also pay for this topic.

B. Cloudways: DO has been offering Cloudways for some time. Does anyone know about it? Advise, recommend? It sounds expensive.

Thank you for your attention,

Have a great evening (it’s evening in France),

Bastien.

Swap space (1GB or 2GB) is going to solve most of your CPU spikes problems. It’s insane all these problems are not addressed by Digital Ocean.

I still have to update Ghost, but if there is this CLI maybe I will think about it and do on a monday after the sunday backup is done on DO…

SWAP is only for RAM management. It will not add any CPU performances.

Do not use DO oneclick installation option. If you want to make this right, I can help with manual installation and migration for free. :grin:

You can add SWAP anytime. No need to use new droplet for that.

Not applicable for SWAP.

Please explain more.

Do not use this. instead of the use droplets.

3 Likes

Yes, tuning both MySQL and Nginx can improve performance when memory is low. There are some detailed things to change about MySQL here and I’ve left comments to explain what they do. Note they are almost all trade-offs that use more CPU or Disk I/O to use less memory:

Nginx can also be tuned to deliver more performance and save CPU by example having Nginx serve static files directly or by having Nginx work as cache. Optimizations are often about trade-offs, and saving CPU like that means storing more content cached in memory for fast retrieval. Cloudfare’s CDN service could also further reduce CPU through caching.

Docker can be technical and complex to manage. But if are asking if it’s a better idea to have four small servers or one big one, having one big one will allow you to share memory and CPU between the instances. That will smoother over some performance problems and likely cost less overall. The trade-off is that if you break the system, all the blogs go down.

1 Like

Thank you very much for this answer.

Help with the migration and swap add would be really great.

Indeed, I think I had used the oneclick for my 4 droplets.

Very clear, thank you very much. I will look into this after I correct my swapspace and distribution issues.