The assault on Paris by the Islamic State in Iraq and the Levant (ISIL) on Nov. 13 was an attack on life’s innocent pleasures. The terrorists shot anyone who strayed into their gunsights — ordinary mecs out for a gig, sharing a drink or watching a football friendly. It could have been any big city. It could have been you.

The deadly grasp of ISIL now reaches far from its base in Syria and Iraq. Paris, scarred by jihadist violence only 10 months ago, had become a city whose world-class intelligence service was on high alert — and still ISIL got through to maim and kill.

Plainly and tragically, the world needs to build stronger defenses. The question is how?

First, know thy enemy.

ISIL bases its terrorism on a vicious calculation. It believes that successful attacks will inspire the would-be Muslim radicals that it is trying to recruit. But it also wants to provoke a backlash in order to convince those same radicals that the world despises them and their religion. In February, ISIL propaganda described a “gray zone” in which some Muslims’ loyalty is divided between radical Islam and a country where they do not feel that they completely belong. ISIL wants terrorism to drive Muslims out of this gray zone and into the black-robed embrace of the caliphate.

The response must be just as calculating. Leading the mourning after the attack, Francois Hollande, France’s president, vowed to destroy ISIL. That is a worthy aim, but a partial one, because other jihadist groups with equally murderous intent will thrive in the violent crevices of the Middle East. The struggle will be long. Countries therefore need policies that they can sustain even as the Middle East remains turbulent and as, inevitably, the terrorists sometimes get through.

Remember that the West has two things to defend: the lives of its citizens, and the liberal values of tolerance and the rule of law that underpin its society. Where these are in conflict, it should choose policies that minimize the damage to values in order to make large gains in protection. Sadly, in the scramble for security, that principle often seems to be the first thing to go.

The starting point for a safer world is at home, with the right legal powers. Jihadists are often radicalized online, in small groups. They communicate electronically. When they travel, they leave a trail. The intelligence services need controlled access to these data. Terrorists thrive on secrecy, yet the security services may abuse their powers. The solution is a legal framework subject to political and judicial scrutiny.

The law can be flexible — but only up to a point. After Hollande declared a state of emergency, which he will seek to extend to three months using a vote in parliament, the police were able to stage raids across France without the need for a warrant. Some raids led to arrests, and one, on Nov. 18 in the suburbs of Paris, to shootings and to another plot being thwarted. Short-lived emergency powers are justified, because of the heightened risk of such follow-up attacks. But the French parliament needs to be careful. If warrantless searches later become routine, abuses will surely follow.

Resources count, too. Last week, Britain announced a 15 percent increase in the size of its security services and a doubling of spending on cyberdefense. Hollande has promised to recruit more police officers and judges. Yet some other states seem out of their depth. Several of the jihadists who attacked Paris came from Molenbeek, a suburb of Brussels with a large Muslim population that it struggles to integrate. Proportionately, more people have gone to join ISIL in Syria from Belgium than from any other country in Europe. The Belgian security services are a weak link.

That matters because the Schengen agreement abolished border controls between 26 European countries. Schengen has symbolic and economic value, but it also drags intelligence down toward the level of the weakest. Once a semi-automatic weapon crosses into a Schengen country from the Balkans, there is little to stop it reaching the hands of jihadists in France. A terrorist can put together a suicide vest undetected in Brussels and travel unimpeded to Paris to detonate it.

The Schengen countries need to adapt to a more dangerous world. First, they need a stronger outer frontier. The French want to create an enhanced European border force, financed and staffed by all of Schengen’s members. This is a good idea, but an overdue one.

Second, within the borderless zone, Schengen’s members need to take down the barriers to policing. The E.U.’s database for migrants does not sync with the one at Europol, the law-enforcement agency. The European Parliament, worried about privacy, has been blocking a plan to give police access to passengers’ names on flights. Countries can do spot checks at the border, but not systematic ones. In Europe more broadly, requests for other countries’ records on, say, ballistics and criminals’ DNA, can be clumsy and time-consuming. To change such things would enhance security, but entails no infringement of fundamental rights. Do it.

The fight for freedom

By contrast, the use of a strong external border to shut out refugees would gravely undermine liberal values without making Europeans any safer. Yet excluding refugees is what politicians in Europe and America have proposed, after one of the Paris attackers entered Europe through Greece, possibly on a false Syrian passport.

The coalition partners of the German chancellor, Angela Merkel, argue that, after Paris, the flow of refugees must be controlled. Poland’s Europe minister has written that it would be dangerous to take them in. Although America has a rigorous system for checking the backgrounds of refugees, over two dozen state governors now say a modest scheme to accept 10,000 Syrians should be halted. So does Marco Rubio, a Republican presidential candidate. (Jeb Bush, another candidate, is broader-minded — he would allow in Syrian Christians.) As of Thursday, so does the U.S. House.

In voters’ minds, Islamic terrorism and large-scale immigration seem to blend together, perhaps because the parents of some terrorists were themselves once migrants, perhaps because both denote a state that cannot control its own territory. Mainstream politicians, fearful of the populist right, seem reluctant to challenge such perceptions. But they have a duty to defend the values of a free society — if they want to live in one.

The logic of turning away refugees is deeply flawed, practically and morally. Clearly, there is a risk of infiltration, and Europe should monitor new arrivals. But at least five of the Paris terrorists were European citizens, not refugees. Someone determined to blow himself up in a terrorist attack could always pay a people-smuggling network to get him in. Some of the refugees arriving on Greek islands were themselves the victims of jihadist violence, occasionally at the hands of Europeans who went to Syria to join ISIL. For Europe to put up a wall to Muslims would suggest that, as ISIL says, Europeans despise them all. That could be a pathway to terrorism, too.

The fight for territory

As well as securing its borders and making terrorists easier to detect within them, the world needs to fight ISIL in its territory in Iraq and Syria. As a last resort, that should include the deployment of Western ground troops.

Some, particularly on the left, argue that military engagement will defend neither Western values nor Western security. Unless it is clear how military action would end, killing people is hard to justify. Moreover, jihadist violence will only rise from the ashes and expose the world to greater danger. That, they say, is what the wars in Afghanistan and Iraq tell you.

Those conflicts do indeed hold sobering lessons. Yet — for the narrow purposes of ISIL — the wrong ones are being drawn. Modern armies are good at driving jihadists off territory, even if they are bad at rebuilding countries afterward. Military action forced Al-Qaida out of Afghanistan. Later, the sustained strikes on its leaders hiding in Pakistan gravely weakened it.

Crucially, ISIL holds territory, as Al-Qaida once did in Afghanistan. Dislodging it would be worthwhile, because ISIL uses this territory to raise money and attract, train and coordinate many thousands of potential terrorists. For as long as it controls its would-be capital, Raqqa, and the Iraqi city of Mosul, ISIL remains a symbolic “homeland” for radical Muslims. The fact of having withstood the world’s great powers — because Allah wills it — serves as potent inspiration.

An alternative strategy to warfare might be to wait for ISIL to wither by itself. But that is a forlorn hope. ISIL continues to exist because the Middle East is consumed by a titanic struggle between Sunni and Shia Islam. This is overlaid by the clash between regional powers and the rivalry of America and Russia. Whatever the opponents of military action say, the West is not what sustains this fight. Were Western countries to withdraw and focus on diplomacy alone, the violence would still go on, possibly for decades.

The case for military action, then, is that the alternative is worse. And yet, partly because it has been a low priority, progress in America’s campaign to “degrade and ultimately destroy” ISIL has been agonizingly slow.

After Paris, however, the mission has taken on some urgency. Hollande, vowing to be “merciless,” has ordered intense French bombing on Raqqa. America has been gradually increasing its efforts, including recently by saying it will deploy 50 special-forces troops against ISIL. Britain’s government is likely to seek parliamentary backing for bombing raids in Syria — about time it supported its allies.

All this is welcome, but it is unlikely to be enough. To destroy ISIL means taking Raqqa and Mosul. That requires an army. So far, the plan has been to train Iraqi forces to use in Iraq, and to look to Kurds and sympathetic fighters in Syria. That plan is not going well. The Kurds have other worries. Despite the training (and money), neither the Iraqis nor the Syrians are ready for a big fight.

The first step now must be to try harder to make the existing plan work, using more trainers and many more special forces fighting alongside Iraqi units. But if that fails, troops will have to come from elsewhere. Nicolas Sarkozy, the former French president, spoke for many last week when he suggested making common cause with Russia, and putting up with Syria’s Bashar Assad in order to use their fighters against ISIL. That approach has a superficial appeal, but would lead to a bloodbath — because both Assad and his Iranian backers are sworn enemies of the Sunnis whose home is in ISIL’s territory. Better to assemble a U.N.-mandated force using Turkish, Saudi and Gulf Arab troops. That will not be easy, but all these countries have an interest in stabilizing a Sunni region that threatens them — directly in the case of Turkey, which has itself been the victim of ISIL terrorism.

President Obama and other Western leaders have an incentive to shepherd such an alliance, because, if that scheme fails, a military campaign would depend on troops from NATO. Such a deployment still lacks political support. Speaking last week, Obama seemed keener to dismiss suggestions of ground troops than he did to pursue ISIL. Yet, with each attack that ISIL unleashes on the West, the imperative to use Western troops against it will grow. In the terrible event of a large strike on American soil, the matter would be settled.

The fight for peace

Military force is not enough on its own, though. It will make the rest of the world safer in the short run, but the critics are right that Islamic terror will end only when the Middle East lives in peace. The parallel aim, therefore, must be for regional powers to stop fighting through their proxies, and for the creation of federal states in Syria and Iraq that give Sunnis, Shias, Alawites and Kurds confidence that they can live together with decent representation in government. That requires strengthening the administration in Baghdad. And it means bringing an end to Syria’s civil war.

The diplomacy will not be easy, and military action should not be forestalled by its lack of progress. But the pursuit of political settlements must be earnest and involve all the parties, including Russia and Iran. The sticking point is Assad, whom both countries support. If there is to be peace in the remnants of his country, he has too much blood on his hands to remain in power indefinitely. Yet Russian President Vladimir Putin has his own jihadist threat — exacerbated by the departure of Islamists from the south Caucasus to Syria. Just perhaps, Putin can be persuaded that Russia does not need Assad to get rid of ISIL and also end up with an ally in western Syria.

Raqqa seems a terribly long way from the streets of Paris on a carefree Friday night. But the killings showed how easily violent ideas cross borders. Innocent lives are still at risk. They will probably be at risk for many years. All the more reason to act now, against every link in the chain.