Skip to content

How to upgrade a web host’s command-line PHP

The following is a very geeky, very niche1Where 'niche' is defined as 'of interest to maybe one person' tip, and I'm only documenting it here because it took me a while to figure it out, and I'd like to not have to go through that again if the need arises.

This site, and a few other personal projects, are hosted at Ionos, with whom I've been generally happy. On the web/GUI side, Ionos makes it really easy to control which version of PHP is used on your sites.

But I also have command line access to my server there, and I ran into an issue trying to run a PHP script (to upgrade another site). It threw an error, so I thought I'd check which version of PHP was in use…

$ which php
$ /usr/bin/php --version
PHP 4.4.9 (cgi-fcgi) (built: Aug 29 2019 12:59:15)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

PHP 4.4.9 was discontinued in 2008—no wonder the script threw an error!

There are newer versions of command-line PHP installed on the server which I could run, but the script (which is part of a package, so I shouldn't edit it) uses this as its first line:

#!/usr/bin/env php

For whatever reason, this forces PHP 4.4.9 to be used. I first tried creating an alias to PHP7 in my profile, but that didn't work—it worked fine in the shell, but the script still tried to use PHP4.4.9.

After much digging, I found the solution: I had to link to the newer version of PHP from a directory in my path. In case that page ever goes away, here's what I did:

  1. Made a new directory at the root of my server: mkdir phpnew
  2. Created a soft link in the new directory, pointing to the new version of PHP on the system: ln -s /usr/local/bin/php7.3 phpnew/php
  3. Edited my .profile to update the $PATH variable to include the new directory: PATH="/path/to/phpnew:$PATH"

After that, everything worked: My command line session is now using PHP7.3, and in the future, it'll be easy to upgrade to newer versions of PHP as they come out—I'll just need to recreate the soft link.