Ricardo Tavares

November 12, 2022

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 at 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 many unexpected consequences if you rely 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 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 is write in a .htaccess file at the root folder of that domain, meaning that anything you have under it will be affected by that configuration, even if it is 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 with the directory structure of another domain.


4) Apples to Apples


I've been using artillery.io 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 a 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, especially 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.

(Originally written on February 5, 2021)

About Ricardo Tavares

Creates things with computers to understand what problems they can solve. Passionate for an open web that anyone can contribute to. Works in domains where content is king and assumptions are validated quickly.

Mastodon  |  Twitter  |  GitHub


View From the Web