WASHINGTON — President Trump defied opposition from his own party and protests from overseas as he signed orders on Thursday imposing stiff and sweeping new tariffs on imported steel and aluminum. But he sought to soften the impact on the United States’ closest allies with a more flexible plan than originally envisioned.

After a week of furious lobbying and a burst of last-minute internal debates and confusion, Mr. Trump agreed to exempt, for now, Canada and Mexico, and held out the possibility of later excluding allies like Australia. But foreign leaders warned of a trade war that could escalate to other industries and take aim at American goods.

“The actions we are taking today are not a matter of choice; they are a matter of necessity for our security,” Mr. Trump said in a ceremony at the White House where he officially authorized the tariffs, which will go into effect in 15 days.

Flanked by a handful of steel and aluminum workers, some wearing coveralls and holding hard hats, Mr. Trump presented the move as a way to rebuild vital industries decimated by foreign competition. “Our factories were left to rot and to rust all over the place; thriving communities turned into ghost towns,” he said. “That betrayal is now over.”