PHP Upgrade Story: Four Lessons Learned

Back at the day job, we have your usual PHP code base that runs a lot of the business and we needed an upgrade from 5 to 7. Business involves a variety of small to large services plus different one-time projects that may require maintenance. So, upgrading had to be a gradual process of isolating a domain that could be upgraded, making/testing changes and benchmarking PHP performance. I've already written about a specific challenge with database encoding that was probably the most difficult and time-consuming to overcome. You can read all about it here. This time, I'm looking back with a more general look on managing different PHP versions with cPanel's MultiPHP approach. MultiPHP edits .htaccess files and maintains different php.ini configurations, making it seem easy to jump between versions with just a few clicks. As one might expect, things are never so simple. Here's what I learned:

1) Be Warned

Starting with the more obvious one, the history of PHP moving forward is one that goes from seeing you shoot yourself in the foot and showering you with guns to tapping you on the shoulder and letting you know what you might be aiming at. For years, PHP has let you push code with potential problems that later on may explode in ways that are hard to pin down. With newer versions, PHP becomes more strict, so the easiest first step when considering an upgrade is to turn up your errors. Let those warnings all come out so you can investigate any type mismatches or assumptions that are unsafe to rely on. This already makes your code more resilient even before upgrading.

2) I Am Root

Onto a more practical matter, MultiPHP does nothing about your cron jobs, so you need to manually configure them to call on a specific php bin like /opt/cpanel/ea-php73/root/usr/bin/php instead of just php. However, as you do so, you will find out that the default working directory will be the root of your home and not the folder where your .php file resides. This may have lots of unexpected consequences if you're relying on relative paths. You should probably change those to absolute, but for simple scripts you can get away with simply adding something like chdir(dirname(__FILE__)) to maintain the same behavior.

3) Runs In the Family

If cPanel is just another tool that you use while still setting up your own folders in your server, eventually MultiPHP is lying to you when it says you are assigning some version to a specific domain. What it does do is write in an .htaccess file at the root folder of that domain, which means that anything you have under it will be affected by that configuration, even if being called from a different domain. This normally shouldn't be a problem as some projects you might want to isolate probably have nothing to do inside the directory structure of another domain.

4) Apples to Apples

I've been using to benchmark web applications, so I also wrote a few tests to monitor possible differences in performance with the upgrade from 5 to 7. If you try this after applying a default upgrade to some domain, you might be surprised to see much worse performance. What's happening really is that the new php.ini is probably not allowing each process to have as much memory. You need to check whatever variables are relevant (like memory_limit) and manually match each one so that both versions are indeed running under the same conditions. Depending on your workloads, version 7 should indeed perform better than 5. In my case, the tests that matched our use cases were very much dependent on database performance, so the difference in speed was not considerable.

With PHP being used by about 80% of all websites with a known server-side programming language, I hope these few tips may still be of use, specially for people upgrading out of 5, which is still a considerable percentage of installations. Thank you for reading and I wish you all the best if you are working on one of these legacy code bases.

If you liked this article, you might want to subscribe to the RSS feed, maybe follow my Twitter or learn more about me.

More from 🌍; view from the web
All posts