Libya, virtually a failed state in recent years, has succeeded in one way: It's providing a perfect opportunity for the Islamic State group to expand from Syria and Iraq to establish a strategic foothold closer to European shores.

Extremists loyal to the group have taken control of two Libyan cities on the Mediterranean coast, have moved toward oil facilities and are slowly infiltrating the capital, Tripoli, and the second-largest city, Benghazi. They have siphoned off young recruits from rival militant groups linked to al-Qaida and in some places taken over those groups' training camps, mosques and media networks.

Notably, there appears to be strong coordination between the Libya branch and the group's central leadership in Syria and Iraq. One of its top clerics, Bahraini Turki al-Binali, has visited the Libyan city of Sirte to preach: in 2013 and again at the end of last year, soon before it fell into the hands of the group's supporters, according to a rival militia official based there. The official spoke on condition of anonymity for fear for his life.

A video released last week showing the beheading of a group of Egyptian Christians abducted from Sirte was produced by the IS media branch.

About 400 mostly Yemeni and Tunisian fighters are in Sirte, according to Libyan Interior Minister Omar al-Sinki. The militia official said Islamic State fighters have set up headquarters in the city's convention complex, the Ouagadougou Center, built by former dictator Moammar Gadhafi as a symbol of his secular regime's aspirations to be a pan-African leader. An Associated Press reporter who briefly visited Sirte on Wednesday saw masked militants deployed along the main road linking the convention center to downtown.

The close connection between the Libya branch and the central leadership around Islamic State chief Abu Bakr al-Baghdadi underscores the strategic importance of the North African country to the group. Libya boasts oil resources — something the extremists have exploited for funding in Iraq and Syria. There are vast amounts of weapons, a legacy of the turmoil since Gadhafi's 2011 ouster. Its borders with Egypt, Tunisia and Algeria are porous.

And the southern shore of Italy is about 400 miles (660 kilometers) away, a distance Libyans fleeing their country's chaos regularly try to cross in rickety boats. Italy and France favor some sort of international action in Libya, while Egypt is pressing for a U.N.-backed coalition air campaign.

Besides Sirte in the center of the country, Islamic State loyalists control the city of Darna, farther east along the coast. This week, Egyptian warplanes struck IS training facilities and weapons depots in Darna in retaliation for the beheadings.

In Tripoli, which is controlled by powerful militias, IS militants have infiltrated some neighborhoods, destroying statues they consider forbidden by Islam and distributing pamphlets to spread their message. They also claimed responsibility for a deadly attack at a luxury hotel that killed several foreigners, including an American.

Some IS extremists have entered Benghazi and are battling government troops, fighting beside other Islamic militias who dominate the city. IS fighters from Sirte recently were seen waving their black banners in a parade of vehicles in the town of Nofaliya, heading toward Libya's oil ports of Sidr, Ras Lanouf and Brega.

The IS leadership in Iraq has named an "emir of Tripoli" to oversee the eastern half of Libya. He is a Tunisian known by the nom de guerre of Abu Talha, according to Interior Minister al-Sinki, although it was not possible immediately to verify his account. In charge of the western half is a Yemeni emir based in Darna and known as Abu al-Baraa el-Azdi, according to local activists and a former militant from the city.

The Islamic State has established is presence in Libya by exploiting the country's breakdown since Gadhafi fell. After his ouster and death, hundreds of militias took power, and some of them have militant ideologies, including Ansar al-Shariah, an al-Qaida-associated group. A militia coalition known as Libya Dawn, which backs Islamist political factions, has taken over Tripoli, where Islamists set up their own parliament and government, and Islamist militias control Benghazi.

The elected, Western-backed government has been pushed to the remote eastern city of Tobruk, from which remnants of the military led by Gen. Khalifa Hifter and some allied militias have been battling the Islamists. The fighting has displaced hundreds of thousands of people and damaged large parts of Benghazi.

The violence also appears to have radicalized some militia members, making them easy recruits for the Islamic State. The group was kicked out of al-Qaida's network for being too extreme, and it made a bid to become the leader of jihadis worldwide last year by declaring a "caliphate" in parts of Iraq and Syria it controls.

It took over Darna last year, while its move to dominate Sirte came more recently.

Once a showcase for Gadhafi's rule, Sirte was devastated by the 2011 civil war, and little has been done to repair it. Schools operated sporadically, banks ran short of cash and bakeries were low on wheat, while garbage piled up in the streets, said Reem el-Breki, a Benghazi activist who runs a news portal that has covered Sirte. "The city was buried alive," she said.

In 2013, it fell under the control of Ansar al-Shariah, which made alliances with local tribes and an uneasy truce with other militias and the few military troops in Sirte. Ansar militants took the Ouagadougou Center as their base, and the group boasted a TV and radio station in the city.

But the Islamic State group appears to have taken over Sirte though a slow infiltration. In 2013, al-Binali — the prominent radical cleric — made his visit to preach in the city's central mosque. Fighters from Mali, Tunisia, Egypt, Syria and the Palestinian territories began to move in, according to the militia official from the city.

Al-Binali — now firmly established in the top echelons of the Islamic State leadership — visited again late last year, the militia official said. Soon after, the 21 Egyptian Christians in Sirte were abducted, and there was a wave of assassinations Jan. 22, with three top security and militia officials killed.

Afterward, Ansar al-Shariah disappeared from Sirte, replaced by the Islamic State group, according to an activist who runs a Facebook page called Sirte Steadfast Youth.

Radio stations played speeches by IS leader al-Baghdadi and songs urging people to pledge allegiance to him. Gunmen forced government workers to sign "repentance" statements. Militia vehicles switched their markings from Ansar to the Islamic State. Local media said IS gunmen looted Sirte's banks.

The Islamic State group posted photos purportedly from Sirte showing religious police touring shops to remove sleeveless dresses. Schools and hospitals were segregated by gender and curriculum was censored, said the activist, who spoke on condition of anonymity for fear of retaliation.

Sirte's fall caused alarm in nearby Misrata, whose powerful militias make up the bulk of Libya Dawn and effectively control Tripoli. The city called on its allies in Tripoli to take action, and a militia official told AP that some forces from the capital have moved to the outskirts of Sirte, although they have not attacked.

___

An Associated Press reporter in Libya contributed to this report.