I ran a middle node for a few months a looooong time ago. Was hit over and over about running an open proxy by the abuse reporting system on the VPS host haphost ;which confused me as it was it has not an endpoint on either end just a middle hop for other nodes. Plus... Everytime I have setup a node since following official configs it's been broken into and had every file apart from those essential to run the tor binary deleted lol. Three times and the exact same thing happened, Crazy stuff. The only port I had open was ssh and that had a crazy long password so there must have been some other way they got in 😂 but as I said it was a long time ago and I was not as experienced as I am now, but it did put me off contributing to the network.
It's safe unless you run an exit node. All my VPS's run tor relays (mostly bridges) except for the IPV6-only servers where the support still isn't there. In my opinion all VPS's should be running Tor relays unless you have a specific reason not to (like if you have less than 1GB RAM you probably shouldn't). All the documentation is on the Tor website. Read up & decide if you want to run a normal relay or a bridge and follow the instructions accordingly.
As an alternative to (or in addition to) a relay/bridge, you can also run one or more instances of Snowflake Proxy which is a special type of Tor on-ramp that doesn't require running a full Tor instance, making it more suitable for low-RAM systems.
It's not illegal but I wouldn't run an exit node, exit's are what reach out to the clear net so any illegal activity it might be used for would appear to be coming from your IP address. You can successfully defend yourself in court over this, many have, but is it worth the hassle?
Tor Relays you can run easily enough but keep in mind many organizations get the IP addresses of tor relays/exits (they are all published online) and block them all so you may find your home IP getting randomly blocked by certain companies and you won't be able to explain it away.
If you want to run something at home consider a bridge, those are unpublished and must be manually connected to after requesting one from a country that censors Tor.
Relays/Exit nodes I would run on VPS instances if you want to contribute to the network, make sure they support/approve exit nodes, many do but have some reverse dns stuff you must setup to mention tor-exit and be sure the http page tor exits host is working, this is how they avoid the legal consequences of random illegal activity coming out of a tor exit if they don't support exits just run a relay.
Exit nodes host a basic http page explaining its an exit and what that means so they are easily identifiable as a tor exit if you browse to their IP in a browser.
I encountered ddos attacks on my entry/relay node. So I stopped it again.
Right after the node was fully provisioned and registered in the tor database it started. Had internet crashes multiple times a day.
Give it a try, better not an exit node, just relay or entry and have a look on it. It's always good to help the tor network in any way. But be aware of what I encountered.