The Port Authority bomber fashioned his crude explosive device out of pipe, a battery, sugar and Christmas-tree lights, following instructions in an online Islamist propaganda publication, law-enforcement sources said.

The homemade weapon, allegedly strapped to Akayed Ullah with Velcro and plastic ties and hidden under his jacket, went off underground near the Port Authority Bus Terminal at around 7:20 a.m. Monday.

But the bomb was apparently a bust — and did not properly detonate because it was poorly constructed, sources said.

If it had worked, it could have caused serious destruction.

“Fortunately, the device he had didn’t function as he intended,” a senior law-enforcement official said.

Instead, the blast seriously injured just Ullah himsef, and left three others with only minor wounds.

The accused bomber, sources said, got the explosive recipe from reading Inspire magazine — al Qaeda’s Web-based English-language publication, founded by the late American Islamist Anwar al-Awlaki and called “the Vanity Fair of terrorism.”

The Internet has proven to be a powerful tool for terror organizations seeking to radicalize and recruit young men in the United States. A study of 129 American militants found that 101 frequently download and share extremist propaganda.

“Militants in the United States today become radicalized after reading and interacting with propaganda online, and generally have little or no physical interaction with other extremists,” the group wrote in a September report.

Many other attackers also got their bomb-building instructions from the slickly-produced Inspire, which features an “open-source jihad” section.

Manhattan resident Jose Pimentel was busted in 2011 after constructing a pipe bomb per Inspire’s instructions, which featured a detailed how-to across eight pages in one edition.