The two territories, which have the European Union's only land borders with Africa, have seen a surge in recent months in illegal border crossings by African migrants seeking a better life in Europe.
In two separate incidents last month, between 800 and 1,000 migrants climbed over the six-metre-high (20-foot) triple border fence at Melilla.
Most of the money set aside by the government will be used to install nets that will make it harder to climb the fence that surrounds the two territories.
"The goal is to reduce, as soon as possible, the assaults of the border and limit the serious and immediate risks that they pose" to migrants and border police, the government said in a statement following a cabinet meeting that approved the measure.
Aside from trying to storm the border fence around Melilla, would-be immigrants also try to sail or swim to Melilla and Ceuta which sit on the Mediterranean opposite mainland Spain.
On about 15 migrants drowned in Moroccan waters while trying to swim to Ceuta from a nearby beach.
Last year 4,235 migrants entered Ceuta and Melilla illegally, a 48.5 percent rise over the number that entered the year before.
Spain's Interior Minister Jorge Fernandez Diaz on estimated there were 40,000 migrants waiting to cross from Morocco into Ceuta and Melilla.
Another 40,000 were waiting to enter Morocco from Mauritania en route to the two cities, he said.
Spain has urged the European Union to provide more money and resources to stop the growing number of African migrants trying to enter Europe.