During the First Fitna, Caliph Ali (r. 656–661) appointed Muhammad ibn Abi Bakr as governor of Egypt, but Amr led an invasion in summer 658 that defeated Ibn Abi Bakr and secured the country for the Umayyads. Amr then served as governor until his death in 664. From 667/8 until 682, the province was governed by another fervent pro-Umayyad partisan, Maslama ibn Mukhallad al-Ansari. During the Second Fitna, Ibn al-Zubayr gained the support of the Kharijites in Egypt and sent a governor of his own, Abd al-Rahman ibn Utba al-Fihri, to the province. The Kharijite-backed Zubayrid regime was very unpopular with the local Arabs, who called upon the Umayyad caliph Marwan I (r. 684–685) for aid. In December 684, Marwan invaded Egypt and reconquered it with relative ease. Marwan installed his son Abd al-Aziz as governor. Relying on his close ties with the "jund", Abd al-Aziz ruled the country for 20 years, enjoying wide autonomy and governing as a "de facto" viceroy. Abd al-Aziz also supervised the completion of the Muslim conquest of North Africa; it was he who appointed Musa ibn Nusayr in his post as governor of Ifriqiya. Abd al-Aziz hoped to be succeeded by his son, but when he died, Caliph Abd al-Malik ibn Marwan (685–695) sent his own son, Abdallah, as governor in a move to reassert control and prevent the country from becoming a hereditary domain.