Just reading the wiki page of Muhammad Pasha and it seems like for two decades he worked quite hard to do what Japan did successfully in the same century. He reformed military basing it on western armies, had build a domestic armament industry, paved the way for industrialization employing western managers etc, purged old nobility i.e. Mamluks, created a central bueracaracy and much more.
But even though Egypt was much closer to Europe, it seems like that Egypt failed in modernizing even while actively trying to attempt it at this point in time whereas a faraway Japan succeeded, why is that? What undid all the advances made in Egypt during Muhammad Pasha?
Egyptian modernization efforts was funded via cotton export. Ongoing Industrial Revolution in Europe generate massive demand for cotton used in textile mills across continent. One of reforms introduced by Muhammad Ali Pasha in 1822 was encourage cotton farming in Egypt, while it was highly successful and makes Egypt 5th largest cotton producer in 1850s. Model was simply, encourage farmers to produce cotton, sell it to Europe and gained funds put into army and administration reforms and new state-owned industries which was based on government contracts or was export oriented.
Problem was "Dutch Desease" (apparent causal relationship between the increase in the economic development of a specific sector and a decline in other sectors), which take control over Egypt economy in similar manner to cotton growing regions in post-communist Central Asia. Cotton is a labor-intensive crop, so a lot of egyptian farmers was tied to planting cotton, so whole families was working on fields just to produce quality cotton, but to competitive, their margin on cotton sales had to be as low as possible. But high cotton prices makes this model more profitable than pre-1822 grain based agriculture in Egypt. Until cotton prices collapse.
When US Civil War start, global cotton prices skyrocket leading to "Cotton Starvation". European powers which was tied to quality cotton import from USA and Egypt for their burgeoning textile manufacturing. Civil War and "Plan Anaconda" cut off access to cotton (and this historical map from Civil War era clearly show this)).
While global demand for cotton was all time high, other countries start looking for their own cotton supply. Most notably cases are Brazil, India, Portugal colonies in Mozambique and Angola, France in West Africa, Tsarist Russia which encourage cotton production in Central Asia and of course USA which goes back on global cotton market after Civil War. This development become problematic for Egypt as local farmers had to compete on global market, so it tax revenue from cotton export. Even worse for Egypt, european industrialists during war improve their industrial parks, so they could use low-quality cotton for textile production. Their high-quality cotton, popular for their properties and how easy it can be used in industrial spinning mills was losing their export advantages. Post-war global cotton prices crash show up all ills of Egyptian economy.
Cotton crash decrease export income for the egyptian treasury, which had to cut their spending, including government contracts for local industries and arnament programs in state-own armories and shipyards. But situation was worse in rural areas. Labor-intensive cotton production mixed with it's low prices, decrease consumption spending which deeply affect local artisans and industries as they were dependent on rural consumption. Farmers was also in debt as they try increase their cotton production by buying land, quality sowing material and tools, so in next season they could earn more money. Major lender was landlords estates which take control over debtor land which increase rural poverty and decrease population consumption which decrease artisans and industrial activity in country, while undercuts small and medium farms with raw scale cash crops production in large estates which use indebted farmers families as increasing source of cheap labor in similar fashion to US sharecropper. Egyptian economy was in structural crisis, mixed with dependence on european credits and european markets, technical revolution in arnament industry makes already expensive army and navy unable to keep pace with european counterparts and political instability in 1860s and 1870s which hurts european investments in country.