Tuning and maintaining a low spec Ubuntu server

My site is running on a fairly low spec server on AllSimple VPS. Being a cheap server means that it does require a great deal more care and attention to ensure it performs well.

The main limitations are storage and memory which I have applied some strategies and tactics to deal with.

Kill unecessary services

Ubuntu server starts up with a variety of services, some of which aren't necessary.

systemctl list-units --type=service --state=running

The tricky part is deciding which services aren't needed, then its a simple case of disabling the service with.

systemctl disable <servicename>

In my case I still had Apache running, when I was using nginx as well as journald which used around 30-40mb of RAM.

Increase Swap Space

I decided to increase my swap space from 200mb to 512mb.

Firstly you need to turn off swap and its availability for any processes.

sudo swapoff -a

Then you can resize the swap to an appropriate amount

sudo dd if=/dev/zero of=/swapfile bs=512mb count=8

  • if - input file
  • of - output file
  • bs - block size b,kb,mb,g
  • count - block multiplier

Enable the resized swap partition

sudo mkswap /swapfile

Re-enable swap on linux

sudo swapon /swapfile

Verify the swap file size with

grep SwapTotal /proc/meminfo

= 557052kb

Clear Old Packages and APT Cache

A nice simple one, is to check for old or redundant packages with.

sudo apt-get autoremove

Then to clear out the cache of APT, first run

sudo apt-get clean --dry-run

This will show a list of packages that can be cleaned out from the cache, then just run

sudo apt-get clean --dry-run

Clear /tmp

Clear any files older than a day from the /tmp folder. Note you might need to use sudo to remove system temp files.

find /tmp -ctime +1 -exec rm -rf {} +

Clear Large Folders Out

This step requires a bit more of a Sherlock approach as you need to analyse storage usage on a directory basis.

To get started run this command, which checks all storage from root in the filesystem.

sudo cd / && du -h --max-depth=1

This will print out a list view

2.5M ./tmp
19M ./boot
4.3M ./root
4.0K ./mnt
6.2M ./sbin
4.0K ./srv
4.9M ./etc
4.0K ./media
9.1M ./bin
595M ./usr
3.9M ./run
16K ./lost+found
2.0G ./var
3.9M ./lib32
683M ./home
0 ./sys
0 ./dev
4.0K ./opt
4.0K ./lib64
86M ./lib

Then its a case of navigating down each directory to find culprits.
/var uses quite a large amount of space for log outputs. So you can try and clear files that are older than 7 days etc.

/var/log is generally a big user of space, so its good to truncate your logs at least or just wipe them if they aren't too important.

/home is another directory which can be cleaned out.

Conclusion

All in all I was able to reclaim about 1Gb of Storage and increase Swap usage, which should keep my server running well.

In hindsight many of these tasks could be scheduled with a cron job and executed through a shell script.