You need a reverse proxy like nginx or traefik. Your mastodon server is using the web ports. Lemmy also wants to use the same ports. Obviously the can't both use them.
The solution is to let neither use the ports and set them up on some other ports.
The reverse proxy is then set up as your main "web server". It will then look at every request coming in and based on the domain name or url requested redirect (or rather forward or proxy) the request to the correct service.. mastadon or Lemmy.
I run dozens of services on the same server. And use traefik to sit in front and manage it all.
I had a Commodore 64 and really really wanted an Amiga. Saw all the games for the Amiga and Atari ST in magazines and wept. It wasn't really available in my country so I didn't know anyone with one but even if it was available we wouldn't have been able to afford it.
I was in the UK last month and went to the computer history museum in Cambridge. They have working versions of every single computer and console ever and you can play with them.
It was great nostalgia to use a ZX Spectrum, my trusty VIC-20 and C64 again. But there it was... An Amiga 500. I played a bit and the graphics and sounds still blew me away now in 2023 as a late 40s middle aged man. It was everything I dreamed it would be.
Another thought: how many db connections do you have? Could it be starved because there are so many selects happening and it needs to wait for them to finish first?
*raccacoonie