PARIS — Three people were killed in a knife attack at a church in the southern French city of Nice on Thursday, an act President Emmanuel Macron referred to as "an Islamist terror attack" and which prompted the country to raise its security alert to the highest level.

The attack came amid tensions over cartoons denigrating the prophet Muhammad, and it echoes the beheading less than two weeks ago of a suburban Paris teacher who had shown the cartoons in a class about free expression.

The French government has defended the cartoons as representing the right of blasphemy against any religion. The bigger concern, the government says, is the need to "reform" the practice of Islam in France as a means of combating "Islamist separatism" and violence.

Many Muslims, in France and abroad, interpret the cartoons as a deeply offensive provocation. There have been calls for a boycott of French products across the Muslim world.

Speaking at the scene in Nice on Thursday afternoon, Macron condemned the attack and doubled down on his defense of freedom of expression, although he did not expressly mention the Muhammad cartoons.

"We will cede nothing," Macron said, announcing the deployment of 3,000 to 7,000 members of France's anti-terror security force throughout the country, especially at churches, schools and other religious sites, through the All Saints holiday on Sunday.

Radical Islamist groups had threatened more attacks, and France had warned its citizens in certain Muslim countries to exert extra caution. Also on Thursday — the birthday of the prophet — a security guard outside the French Consulate in Jiddah, Saudi Arabia, was stabbed. He did not suffer life-threatening injuries, the French Consulate said in a statement.

The attack in Nice occurred at the Basilica of Notre-Dame de L’Assomption, shortly before 9 a.m. Three people were confirmed dead, according to anti-terror prosecutor Jean-François Ricard: a 60-year-old woman, whose throat was slashed in the church; a 55-year-old man, stabbed to death inside the church; and a 44-year-old woman, who died in a restaurant across the street after fleeing the basilica.

Ricard identified the suspect as a 21-year-old Tunisian national who arrived in Europe via the Italian island of Lampedusa last month. When police arrived at the church, the man approached them, shouting, “Allahu akbar,” or “God is greatest” in Arabic, Ricard said. He was shot by French police and taken to a hospital, where he underwent an operation and remained in critical condition, the prosecutor said.

A copy of the Koran was found in the suspect’s possession, as well as a nearly 12-inch knife apparently used in the attack, Ricard said, adding that two other knives were found in the church.

The French media additionally reported Thursday that police fatally shot a man in Avignon who had been threatening a passerby with a knife. Authorities apprehended another man, a 26-year-old Afghan, brandishing a nearly 12-inch knife in central Lyon, near the Perrache train station, according to a statement from the Lyon prefect’s office to France’s Le Monde newspaper. It was not clear whether those attacks were connected to the Nice stabbings.

According to the SITE Intelligence Group, after the attacks on Thursday, jihadists following al-Qaeda and the Islamic State filled social media platforms and forums with cheers, many sharing a quote by Osama bin Laden: “If your freedom of expression respects no boundaries, be prepared to face our freedom of action.”

The Islamic State called on followers to target French companies operating in Muslim countries, according to SITE.

The attacks, however, momentarily eased official anger toward France.

Turkish President Recep Tayyip Erdogan had clashed with Macron this week, saying his defense of the caricatures and treatment of Muslims in France suggested he “needs treatment on a mental level.” Turkey — which also has strategic disputes with France over the civil war in Libya and Turkey’s claims to energy deposits in the eastern Mediterranean — had led the calls for a boycott of French products.

On Thursday, though, Turkey condemned the bloodshed in Nice and offered condolences to relatives of the victims.

“No reason can excuse the killing of a person and legitimize violence,” said a statement released by Turkey’s Ministry of Foreign Affairs. “It is clear that those who organized a brutal attack like this one in a holy place of worship do not have their share of religious, human, and moral values.”

President Trump voiced his support for France on Thursday, while using the attack as an opportunity to bash his Democratic opponent, Joe Biden. “Under the Biden plan, the horrifying attacks in France will come to our cities and our towns,” Trump said.

The French caricatures, published by the satirical newspaper Charlie Hebdo, have now been tied to multiple incidents of violence.

Charlie Hebdo itself was victim of a major terrorist attack in January 2015, when two brothers killed 12 journalists and claimed they had “avenged the prophet” as they fled the scene.

Last month, after Charlie Hebdo republished the cartoons, two people were stabbed outside the newspaper’s former offices.

And then, two weeks ago, Samuel Paty, the middle school teacher, was beheaded, a gruesome incident that shocked the nation.

In the eyes of the French government, defending the caricatures, as offensive as they may be to some French citizens, is a fundamental part of national identity.

“In France, there is only one community — the national community,” Macron said in Nice. “We will cede nothing to the spirit of division.”

The cartoons — and the right of blasphemy the government says they represent — are deeply rooted in French history. But France is also a country that abides by hate speech laws and seeks to protect minority rights.

“Civil liberties, including freedom of speech and freedom of the press, have historically been gained as an emancipation from religion,” said Cécile Alduy, a French political analyst and Stanford University professor.

“In France, it was against the church that the freedom of expression was won,” she said. “It still informs very deeply the French notion of secularism and liberties. The caricatures are seen, even when one does not endorse them, as a symbol that blasphemy is no longer a crime and that religions cannot dictate what can be said or created.”

Patrick Weil, an expert on the history of France’s secularism, or laïcité, said the Muhammad caricatures are permissible because they attack a religion, not a particular group of individuals.

“You can attack a religious authority because it’s not a human person, it’s a god. But if you attack Muslims as Muslims, you are condemned by the courts,” he said. “Of course, the distinction is sometimes complicated.”

In fact, the French government has not always defended the publication of Muhammad cartoons.

“Anything that can hurt the convictions of someone else, in particular religious convictions, should be avoided,” said President Jacques Chirac in 2006, speaking after Charlie Hebdo had published its first set of Muhammad caricatures.

But the 2015 Charlie Hebdo attack forced a hardening of convictions, said Gérard Araud, France’s former ambassador to the United States. It backed the government into a corner.

“We are trapped with these caricatures. We can’t simply drop them, or say publicly that they’re vulgar, because people have died for them and are still dying for them,” Araud said. “We are obliged to fight for them, whatever we think of them.”

The string of attacks in France has additionally given impetus to Macron’s plans to “reform” the practice of Islam in France, mostly by targeting foreign funding received by Muslim communities in the hopes of combating links to foreign radicals. The government also announced a crackdown on more than 50 Muslim organizations it has accused of fomenting terrorist violence.

Macron prompted anger from Muslims in France and abroad with a declaration that "Islam is a religion that is in crisis all over the world."

His quest to intercede into the practice of the religion is about promoting integration but also preventing the sort of terrorist attacks that have scarred France in recent years.

Nice was the site of a devastating attack on Bastille Day in 2016, when a Tunisian man living in France intentionally drove into the coastal city’s busy Promenade des Anglais, killing 86 people.

Kareem Fahim in Istanbul and Sarah Dadouch in Beirut contributed to this report.