I feel your pain! I would love to have a 1 or 2 U height Mac system. Sadly, Apple has long since gone a different direction.
If you can find one of the last Xserv systems you should be able to do what you want. Here is a good write up: [https://en.m.wikipedia.org/wiki/Xserve|Xserve].
There is no means to reconfigure the iMac logic board and its sub systems to fit into a rack mount chassis. The system is based on a stove pipe design so the logic board & power supply heat is carried up in the vertical position. Lastly, Apple designs its own parts as such you can't use off the shelf parts except the HD & memory.