The French defender spent the second half of last season on loan with the Rossoneri after falling out of favour at Spanish side Valencia.
Milan had an option to make the deal permanent this summer but were unwilling to pay the asking price set by Los Che for the 28-year-old.
After weeks of negotiation, both clubs have reached an agreement that will see Rami move to Milan for a reported fee of 4.25million euros (£3.375million).
"The player will be at (Milan's headquarters) Milanello with the rest of the squad on Friday," Galliani told Gazzetta dello Sport. "We don't have the signature yet, as the president is busy, but the agreement is very close to completion.
"We improved our offer to Valencia, as the difference was paid by the player to ensure the transfer.
"He deserves a lot of credit for that."
Rami scored three goals in 18 league appearances for Milan last season.