Initially. The first Opium War was mostly the result of some very rich, but highly leveraged, and politically connected British merchants (including the East India Company, which before the institution of the Raj, was nearly a state unto itself in south-east asia) who had a massive quantity of illegal opium seized and destroyed by the Qing governor, who then proceeded to close the markets and quarantine the merchants in their factories. Eventually, amidst high tensions the next year, there was an incident between Britsh, merchant and Qing ships off Canton, which flared things up into a shooting war. The Brits rushed in troops and ships from India, and owing to the Qing empire's traditional focus on suppressing more recently conquered regions of Inner Asia and opposing Russian expansionism, the British forces met very little resistance. The blockaded the coast, swept the very much over-matched Qing navy aside (the Qing navy was more of an anti-piracy force, really), and launched an attack towards the Imperial capital.
There was a long tradition in imperial China of buying off invading barbarians to get them to go home and stop causing havoc. To some extent, this was formalized in the imperial tribute system, which was used relatively successfully to placate the Khitan, Mongol, Turkic and Manchu neighboring states in various periods. It is possible to view the Treaty of Nanjing in this tradition, at least from the Qing perspective; although from the Western side it's clearly a punitive concession of defeat. This then opened up a floodgate of other commercial nations wanting the same trade concessions (access to additional trade ports, abolition of tariffs) to keep up.
Later, with the advent of steam power, the different Western nations desired naval bases and coaling stations, to protect trade and project power. And eventually, Western nations began building infrastructure in China, at the cost of further, sometimes extensive territorial concessions. In some sense, this was accelerated by the global land-grab and prestige race between the great powers during the Victorian Age, which also coincided with the rapid modernization of Meiji Japan, which sought to wring the same kinds of concessions from the Qing Empire as the western nations.