There really is no optimal distance. Each room will vary a good bit, and distance of the ports to the wall is probably a minor issue compared to where the sub is situated in the room (corner, along a wall, near the opening to another room, used as an end table, whatever).
In any case 5" is more than enough for the ports to flow, cable clearance etc. You could probably close the distance some without any problems too leave a min. of 3"-4" though.