By Humeyra Pamuk and Nick Tattersall

ISTANBUL (Reuters) - President Tayyip Erdogan told a rally of more than one million people on Sunday that July's failed coup would be a milestone in building a stronger Turkey, defying Western criticism of mass purges and vowing to destroy those behind the putsch.

The "Democracy and Martyrs' Rally" at the Yenikapi parade ground, built into the sea on the southern edge of Istanbul, was a show of strength by Erdogan, who has been angered by European criticism of his combative response to the coup and by U.S. reluctance to hand over the man he accuses of masterminding it.

Banners in a sea of red Turkish flags read "You are a gift from God, Erdogan" and "Order us to die and we will do it". It was the first time in decades that opposition leaders joined a rally in support of the government, with pockets of secularists, nationalists and others alongside his core Islamist supporters.

"That night, our enemies who were rubbing their hands in anticipation of Turkey's downfall woke up the next morning to the grief that things would be more difficult from now on," Erdogan said of the July 15 abortive coup, drawing parallels to times past when Turkey was occupied by foreign forces.

"From now on, we will examine very carefully who we have under us. We will see who we have in the military, who we have in the judiciary, and throw the others out of the door."

The parade ground, built to hold more than a million people, was overflowing, with the streets of surrounding neighborhoods clogged by crowds. One presidency official put the numbers at around five million and the event was broadcast live on public screens at smaller rallies across Turkey's 81 provinces.

Since the coup bid, Turkish authorities have suspended, detained or placed under investigation tens of thousands of people, including soldiers, police, judges, journalists, medics and civil servants, prompting concern among Western allies that Erdogan is using the events to tighten his grip on power.

Erdogan vowed to rid Turkey of the network of U.S.-based cleric Fethullah Gulen, whose followers in the security forces, judiciary and civil service he accuses of orchestrating the attempted power grab and of plotting to overthrow the state.

Erdogan said he would approve the restoration of the death penalty if parliament voted for it, a move which would sink any hopes of European Union membership. Shrugging off EU concerns, he said much of the rest of the world had capital punishment.

Gulen - an ally of Erdogan in the early years after his Islamist-rooted AK Party took power in 2002 - has denied any involvement in the coup, which came at a critical time for a NATO "frontline" state facing Islamist militant attacks from across the border in Syria and an insurgency by Kurdish rebels.

In a rare appearance at a public rally, military chief Hulusi Akar said the "traitors" behind the plot would be punished and he thanked civilians for their role in putting it down. Many of the more than 240 people killed on July 15 were civilians who tried to prevent the takeover of power.

The leader of the main secularist opposition, Kemal Kilicdaroglu, said a "new door of compromise" had been opened and that politics must be kept out of the mosques, courthouses and barracks. "There is a new Turkey after July 15," he said.

"FREEDOM OR DEATH"

Erdogan, a polarizing figure seen by opponents as intolerant of dissent, invited the heads of the secularist and nationalist opposition parties to address the crowds in a display of national unity in defiance of Western criticism.

"We're here to show that these flags won't come down, the call to prayer won't be silenced and our country won't be divided," said Haci Mehmet Haliloglu, 46, a civil servant who traveled from the Black Sea town of Ordu for the rally.

"This is something way beyond politics, this is either our freedom or death," he said, a large Turkish flag over his shoulder and a matching baseball cap on his head.

Turkey's top Muslim cleric and chief rabbi also attended. But the pro-Kurdish HDP, the third-largest party in parliament, was not invited due to its alleged links to Kurdish militants, prompting anger on social media from its supporters.

The brutality of July 15, in which rogue soldiers commandeered fighter jets, helicopters and tanks, shocked a nation that last saw a violent military power grab in 1980. Even Erdogan's opponents saw his leadership as preferable to a successful coup renewing the cycle of military interventions that dogged Turkey in the second half of the 20th century.

"Erdogan has been brutal and unfair to us in the past, but I believe he has now understood the real importance of the republic's values," said Ilhan Girit, 44, a musician and CHP supporter, carrying a flag of Mustafa Kemal Ataturk, founder of the modern secular republic.

A convoy of nationalists on motorbikes passed as he spoke.

Such solidarity may not last. There are already opposition concerns that the restructuring of the military lacks parliamentary oversight and is going too far, with thousands of soldiers discharged, including around 40 percent of generals.

WESTERN CRITICISM

The extent of the purges in Turkey, which has NATO's second largest armed forces and aspires to membership of the European Union, has drawn criticism in the West.

In comments published on Sunday, the leader of Germany's liberal Free Democrats said he saw parallels between Erdogan's behavior and the aftermath of the Reichstag fire in 1933, portrayed by the Nazis as a Communist plot against the government and used by Adolf Hitler to justify massively curtailing civil liberties.

Turkish officials have angrily rejected suggestions that the purges are out of proportion, accusing Western critics of failing to grasp the magnitude of the threat to the Turkish state and of being more concerned about the rights of coup plotters than the brutality of the events themselves.

Amid the cooling of ties with the West, Erdogan is due to meet Russian President Vladimir Putin on Tuesday in St Petersburg for talks intended to end a period of tension after Turkey downed a Russian fighter jet near the Syrian border last November.

"At the talks with my friend Vladimir, I believe, a new page in bilateral relations will be opened. Our countries have a lot to do together," Erdogan told the TASS news agency in an interview published on Sunday.

In Washington on Sunday several hundred people clad in red and waving Turkish flags gathered in front of the White House in support of Erdogan and to demand that U.S. President Barack Obama deport Gulen to Turkey.

"He (Erdogan) has made some mistakes but he is not a dictator," said Okan Sakar, 35, a Turkish tax inspector currently studying in the United States.

(Additional reporting by Tuvan Gumrukcu in Istanbul, Caroline Copley in Berlin, Maria Kiselyova in Moscow, Jason Lange in Washington; Writing by Nick Tattersall; Editing by Ralph Boulton and Gareth Jones)