Following the Treaty of Fontainebleau signed between France and Spain in October 1807, which provided for the invasion and subsequent division of Portuguese territory into three kingdoms, French troops under the command of General Junot entered Portugal, which requested support from the British. In July 1808 troops commanded by the Duke of Wellington, at the time known as Arthur Wellesley, landed in Portugal and defeated French troops at the Battles of Roliça and Vimeiro. This forced Junot to negotiate the Convention of Cintra, which led to the evacuation of the French army from Portugal.
However, the threat of further invasions by the French led Wellington, on October 20, 1809, to order the construction of defensive lines in order to protect Lisbon from Napoléon Bonaparte's troops and also to provide a route for his own evacuation should it be necessary. Work on the Fort of Alqueidão, as part of the first Line of defence, began on November 8, 1809, at more or less the same time as improvements to the Fort of São Julião da Barra, close to Lisbon, and construction of the new Fort of São Vicente at Torres Vedras, under the overall supervision of Colonel Richard Fletcher who was commander of the Royal Engineers. By October of the following year 126 forts, redoubts and other defences forming three lines of defence had been constructed over 80 kilometres, reinforcing the natural obstacles that the land offered and making maximum use of the existing topography.
A new French invasion was headed by Marechal Masséna. He was defeated in September 1810 by the Anglo-Portuguese army commanded by Wellington at the Battle of Buçaco. Despite this he forced the allies to retreat to the Lines of Torres Vedras, where a stalemate ensued for several months. Masséna’s troops were gathered around Sobral de Monte Agraço but he found the line of defences, centered on the Fort of Alqueidão, to be impregnable. He eventually had to retreat due to lack of food and supplies, withdrawing to the Spanish border.