Ghost woes: swap memory, 502s

Solving memory problems with upgrading Ghost on a basic Ubuntu box with 1GB RAM.

Earlier this year I began moving this blog from a Wordpress CMS hosted by Wordpress to a Ghost CMS hosted by Digital Ocean on an Ubuntu box. Needless to say, it's been a learning journey maintaining this blog. Compared to Wordpress, I have to be a lot more hands-on with keeping Ghost updated, as well as my Ubuntu box. Part of me is a bit annoyed at the overhead for a slightly more expensive yet more Markdown-friendly and ad-free alternative to my former hosting needs. I especially feel this way when I get caught up in the middle of problems to debug. But the other part of me, of course when I eventually solve my technical problems, feels grateful for what I've been forced to learn so far. One of those learned lessons is about swap memory.

What is swap memory?

Swap memory is memory you take from the disk drive to use as additional memory – a supplement to your RAM. It is used when your dedicated RAM runs out of memory.

How do you set up swap memory?

On Linux, set up the swapfile with an amount. Here I'm setting aside 1GB of disk space for swap memory, giving the file the right modification permissions, creating a swap memory with the file using mkswap, and then turning it on. Your amount may vary.

$ sudo fallocate -l 1G /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=2af502dd-d50f-4269-a95f-89eaf60bd567
$ sudo swapon /swapfile

This turns off the next time you reset your machine. Check free -m to verify. To keep it around, add this line to your /etc/fstab file:

/swapfile               none    swap    sw              0 0

How much memory did I start with?

By default, my Basic Plan Ubuntu box from Digital Ocean comes with 1 GB of RAM and 25 GB of disk space. I use very little disk space.

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            97M  1.1M   96M   2% /run
/dev/vda1        25G  8.0G   17G  34% /
tmpfs           485M     0  485M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/vda15      105M  5.3M  100M   5% /boot/efi
tmpfs            97M  4.0K   97M   1% /run/user/1000

However, I use "a lot" of memory relative to what is available to me:

$ free -h
     total        used        free      shared  buff/cache   available
Mem: 969Mi       543Mi        59Mi       0.0Ki       365Mi       259Mi

Upgrading Ghost

I noticed, whenever I upgraded Ghost in the shell box, my website wouldn't load, and ghost doctor would tell me that I would often run out of memory. I would do a lot of tricks to get myself back to the minimum recommended free memory (150 MB), and that kept me going until version 1.5.9.

Unfortunately, when I upgraded to 1.26.1, in addition to some other things like my Node.js and Ghost CLI versions, none of my tricks worked. I kept getting errors saying I didn't have enough space.

Memory troubleshooting

I checked what was hogging up the most memory:

$ htop

It was mysqldb. I went into a little rabbit hole, trying to figure out if the ~1000mb of memory was unreasonable: refreshing how to play with MySQL in CLI, whether I should change the buffer_pool_size (also SO). Apparently that amount of memory was OK.

I found this forum thread as well for clues. They mentioned using swap memory as a solution, but I didn't understand what that was (!), so I just ignored it.

Eventually I sighed and resorted to upping my memory from 1GB to 2GB via the Digital Ocean interface. But I hated that solution – it would bump my monthly fee from $6 to $12, and that just seemed ridiculous for my humble little blog.

502 Gateway error

In addition, even after bumping my memory, while ghost doctor no longer complained about that issue, I was now stuck with a 502 Gateway Error problem when hitting my domain https://jasna.me!

Interestingly hitting my direct IP addresses gave me a "Welcome to nginx!" page....

Circling back to swap memory

I eventually found this SO post about 502 errors after upgrading Ghost. This particular answer led me to this wayback machine post that had a ton of great tips on setting up Ghost for cheap on DO, i.e., without needing to get a bigger memory plan.

Funnily, here is where the swap memory strategy finally caught my attention – it helped that the post explained its importance, of course.

I enabled it, resized my machine back to the lowest plan of 1 GB memory, and then rechecked ghost doctor. No memory complaints still! Yay!

$ free -h
       total        used        free      shared  buff/cache   available
Mem:   969Mi       547Mi        58Mi       0.0Ki       363Mi       260Mi
Swap:  1.0Gi       166Mi       857Mi

But I still had a 502 error with my domain, and the "Welcome to nginx!" page on my IP address.

Force upgrading Ghost?

Back at that SO post, another person said that doing a stop and force update did the trick. I ignored this solution at first since it seemed odd, but that ended up being it.

$ systemctl stop [your ghost instance]
$ ghost update --force

Back Online

I don't know whether my resizing would've caused this 502 error, or whether it was my Ghost/Ghost CLI/Node.js upgrades did it. Regardless, the blog is finally back, with all the upgrades, on the same basic plan, and a few additional tweaks. Whew.

Stick around, or change things up?

For a few hours, in my frustration, I considered again other alternatives.

  • For a Ghost alternative, Grav seems cool, as well as other flat-file CMSes. I just need Markdown and a simple search engine. Ideally a simple UI as well.
  • For a Digital Ocean alternative, Raspberry Pi?? That would be a whole new project to undertake, but more things to learn.

For now I'll stick with what I have, but inevitably I may revisit this topic sometime.