Can I use a MacBook as a server with the lid closed?
To use a MacBook as a server with the lid closed, all you need to do is to ensure that it's powered on and not in sleep state.
Closing the lid of a MacBook by default puts it into sleep mode irrespective of whether it is plugged into power or running on battery.
One common way to keep the MacBook powered on with the lid closed is to connect an external display (and keeping it plugged-in to power, to make sure that it doesn't run out of power) to your MacBook.
If that is not feasible, or is not an option that you are looking for, you can refer to the solution in the linked discussion:

https://apple.stackexchange.com/a/361398/279727


You can accomplish this in  terminal. No additional software needed.Display global power settings: pmset -g
System-wide power settings:
Currently in use:
 lidwake              1
 autopoweroff         1
 standbydelayhigh     86400
 autopoweroffdelay    28800
 proximitywake        1
 standby              1
 standbydelaylow      10800
 ttyskeepawake        1
 hibernatemode        3
 powernap             1
 gpuswitch            2
 hibernatefile        /var/vm/sleepimage
 highstandbythreshold 50
 womp                 0
 displaysleep         10
 networkoversleep     0
 sleep                1 (sleep prevented by sharingd)
 tcpkeepalive         1
 halfdim              1
 acwake               0
 disksleep            10

To stop sleep entirely:sudo pmset -a disablesleep 1To revert, allowing sleep again:sudo pmset -a disablesleep 0