The siege of Alexandria (April-August 1167) was S first recorded independent command, and he managed to hold the city until both sides were exhausted and a peace treaty could be negotiated.
Saladin had entered Egypt as part of a Syrian army sent by Nur ad-Din with the aim of overthrowing the Shi'a Fatimid Caliphate of Cairo. The army was commanded by Saladin's uncle Shirkuh, who was leading an army into Egypt for the second time. The first expedition, in 1164, had ended with Shawar, vizier of Egypt allied with Amalric, king of the Crusader Kingdom of Jerusalem. Shirkuh had been besieged in Bilbais and had been rescued by Nur ad-Din's efforts further north, which convinced Amalric to negotiate a peace agreement.
In the years between the two campaigns Shirkuh convinced Nur ad-Din to launch an invasion of Egypt with the aim of overthrowing the Shi'a Fatimid Caliphate. Vizier Shawar had the support of Amalric, who led a Crusader army into Egypt as allies of the government. Shirkuh, who was probably outnumbered, moved to the west bank of the Nile but eventually the Franks and Egyptians crossed the river and followed him south. The resulting battle of Babain (18 March 1167) was a victory for Shirkuh, but not a conclusive one. He wasn't strong enough to risk a direct attack on Cairo, and so instead led his army north to Alexandria.
Egypt's second city had rebelled against Shawar and the Fatimid regime before the battle of Babain. Shirkuh and his men were thus welcomed in the city.
Amalric and Shirkuh still had strong armies in Egypt. They moved to besiege Alexandria, which was already being blockaded by the Frankish fleet.
After about a month Shirkuh decided to split his army, take part of his force and try break through the Egyptian and Frankish lines. Once he was away he would try and raise new troops and distract the besiegers. At first Amalric was tempted to follow him, but he was soon convinced to resume the siege.
This left Saladin in command of the beleaguered city. The Franks built powerful catapults and the city came under bombardment. The blockade was firmed up, and by July food was running out in the city. Saladin had a loyal core of Syrian troops, and at first the support of an enthusiastic population. When Shawar demanded that the citizens handed over Saladin they refused.
By July the food had run out and the defenders were close to surrender. Saladin managed to smuggle a message out to his uncle, whose efforts out the city had failed. For a second time Shirkuh was forced into negotiations with the Franks and Egyptians.
Arnulf of Tell Bashir, a Latin captive in Alexandria, represented Shirkuh at the early peace negotiations. Amalric was also ready to negotiate, and a peace treaty was soon agreed.
Both sides agreed to exchange prisoners. Shirkuh and Amalric would evacuate Egypt, although Amalric was allowed to leave a small force in Cairo, and Shawar agreed to pay him an annual tribute. The Crusaders were then given tours of Alexandria, and reportedly couldn't understand how such an impressive city with such a large city could have been so closely pressed by such a small army.
After the negotiations were over Shawar went back on the amnesty. Ibn al-Zubayr, one leader of the city, was executed, and Ibn Masal, who later went into Saladin's service, had to escape. Saladin made a personal appeal to Amalric to stop these breaches of the peace, and Amalric forced Shawar to stop. This care for his supporters became one of Saladin's hallmarks. This was also the period where he first came into close contact with some of the Crusaders, the first steps towards his later impressive reputation in the west.