Is there a way to keep nextcloud running when my laptop is closed?

running ubuntu on an old laptop. please help

Try running it on a different machine, so you will not need to rely on the laptop for the server component, since it will be on a dedicated box. This is the way.

https://www.google.com/search?q=disable+sleep+and+hibernate+if+lid+is+closed+ubuntu

Maybe you can logout and stop service gdm3 and not close laptop.

Depending on laptop generation (younger than 4 years) and BIOS you may be able to disable “close lid”-events under “Config”-“Power” in BIOS.

You can also handle this under power settings of Ubuntu.