Satisfying the first concern is a matter of ordering a mantel that is sized correctly for your fireplace. The National Fire Code says that all combustible material must be 1 inch away from the firebox opening for every 1/8 inch it protrudes from the surface, with a minimum 6-inch clearance all around. (If stricter local codes exist, these supersede national codes). If you have an odd-size firebox, a stock mantel may create a disproportionate-looking reveal around the opening, so you may have to custom order to get the mantel to look right.
Satisfying the second concern is all about good carpentry. Many mantel-kit companies suggest merely toenailing the piece through the shelf directly to the wall, or even gluing it with silicone caulk. Since neither option is very sturdy, we recommend you attach the mantel to a cleat—a simple piece of lumber securely fastened to the wall. One cleat runs horizontally under the mantel shelf, and the others run vertically inside the legs (most mantels are hollow in back). Then you screw the mantel to the cleats along the back of the mantel shelf and the sides of the legs.