TRIPOLI, Libya — “The fire is inside the airport!” a militiaman cried, as he fired an antiaircraft cannon on the back of a pickup truck toward the runway of Libya’s main international airport. “God is great, the flames are rising!”

“Intensify the shooting,” responded his commander, Salah Badi, an ultraconservative Islamist and former lawmaker from the coastal city of Misurata.

Captured on video by the proud attackers just one month ago, Mr. Badi’s assault on Libya’s main international airport has now drawn the country’s fractious militias, tribes and towns into a single national conflagration that threatens to become a prolonged civil war. Both sides see the fight as part of a larger regional struggle, fraught with the risks of a return to repressive authoritarianism or a slide toward Islamist extremism. Three years after the NATO-backed ouster of Col. Muammar el-Qaddafi, the violence threatens to turn Libya into a pocket of chaos destabilizing North Africa for years to come.

Libya is already a haven for itinerant militants, and the conflict has now opened new opportunities for Ansar al-Shariah, the hard-line Islamist group involved in the assault on the American diplomatic Mission in Benghazi in 2012.