<P> The Viceroy of Peru, Joaquin de la Pazuela named Jose de la Serna commander - in - chief of the loyalist army to protect Lima from the threatened invasion of San Martin . On 29 January, de la Serna organized a coup against de la Pazuela which was recognized by Spain and he was named Viceroy of Peru . This internal power struggle contributed to the success of the liberating army . In order to avoid a military confrontation San Martin met the newly appointed viceroy, Jose de la Serna, and proposed to create a constitutional monarchy, a proposal that was turned down . De la Serna abandoned the city and on 12 July 1821 San Martin occupied Lima and declared Peruvian independence on 28 July 1821 . He created the first Peruvian flag . Alto Peru (Bolivia) remained as a Spanish stronghold until the army of Simón Bolívar liberated it three years later . Jose de San Martin was declared Protector of Peru . Peruvian national identity was forged during this period, as Bolivarian projects for a Latin American Confederation floundered and a union with Bolivia proved ephemeral . </P> <P> Simon Bolivar launched his campaign from the north liberating the Viceroyalty of New Granada in the Battles of Carabobo in 1821 and Pichincha a year later . In July 1822 Bolivar and San Martin gathered in the Guayaquil Conference . Bolivar was left in charge of fully liberating Peru while San Martin retired from politics after the first parliament was assembled . The newly founded Peruvian Congress named Bolivar dictator of Peru giving him the power to organize the military . </P> <P> With the help of Antonio José de Sucre they defeated the larger Spanish army in the Battle of Junín on 6 August 1824 and the decisive Battle of Ayacucho on 9 December of the same year, consolidating the independence of Peru and Alto Peru . Alto Peru was later established as Bolivia . During the early years of the Republic, endemic struggles for power between military leaders caused political instability . </P> <P> From the 1840s to the 1860s, Peru enjoyed a period of stability under the presidency of Ramón Castilla, through increased state revenues from guano exports . However, by the 1870s, these resources had been depleted, the country was heavily indebted, and political in - fighting was again on the rise . Peru embarked on a railroad - building program that helped but also bankrupted the country . In 1879, Peru entered the War of the Pacific which lasted until 1884 . Bolivia invoked its alliance with Peru against Chile . The Peruvian Government tried to mediate the dispute by sending a diplomatic team to negotiate with the Chilean government, but the committee concluded that war was inevitable . Chile declared war on 5 April 1879 . Almost five years of war ended with the loss of the department of Tarapacá and the provinces of Tacna and Arica, in the Atacama region . Two outstanding military leaders throughout the war were Francisco Bolognesi and Miguel Grau . Originally Chile committed to a referendum for the cities of Arica and Tacna to be held years later, in order to self determine their national affiliation . However, Chile refused to apply the Treaty, and neither of the countries could determine the statutory framework . After the War of the Pacific, an extraordinary effort of rebuilding began . The government started to initiate a number of social and economic reforms in order to recover from the damage of the war . Political stability was achieved only in the early 1900s . </P>

What is the meaning of peru in english