What is the solution?

It's multifaceted. It might include the following measures taken: 1. On the battlefield (Syria and Iraq, for example) we destroy the Islamist forces. Unfortunately the US has focused on toppling Assad instead of defeating Islamist terrorists. 2. Intelligence and anti-terrorism must be beefed up in our countries. 3. Migrants and/or asylum seekers must be detained in a secure place until they can be processed. 4. Anyone with links to Islamism must not be allowed in Europe. 5. We must strengthen and promote secularism in or own countries, including through education, and make it clear that secular, liberal, enlightenment values are not to be compromised by any religious ideologies. For example, we should have no sharia law/councils and the wearing of Islamist burka/niqab should be restricted. 

I'm sure there's a lot more we could do than this.