KNOWNHOST WIKI

User Tools

Site Tools


control-panels:directadmin:wrong-php-version-detected-using-multiple-php-versions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

control-panels:directadmin:wrong-php-version-detected-using-multiple-php-versions [2019/11/07 21:05] (current)
Karson N. created
Line 1: Line 1:
 +====== Workaround- Wrong PHP Version Detected by WP-CLI When Using Multiple PHP Versions ======
 +
 +\\
 +
 +WP-CLI, like many other software installations and tools, will detect the PHP version based off of the default/​primary PHP version installed on a server rather than the PHP version actually assigned to the site via a multiple PHP version setup. Your site may be configured to use PHP 7.3, but the following error may still occur if your primary PHP version is set to an older PHP version:
 +
 +//Your server is running PHP version 5.5.38 but WordPress 5.2.3 requires at least 5.6.20.//
 +
 +The error above occurred for me with a WP-CLI phar installation for a site hosted via a cPanel server. This cPanel server had PHP 5.5 set as the default/​primary PHP version while the site was set to use PHP 7.3:
 +
 +<​code>​
 +[~]# /​usr/​local/​cpanel/​bin/​rebuild_phpconf --current
 +DEFAULT PHP: ea-php55
 +ea-php54 SAPI: suphp
 +ea-php55 SAPI: suphp
 +ea-php56 SAPI: suphp
 +ea-php70 SAPI: suphp
 +ea-php73 SAPI: suphp
 +[~]# 
 +</​code>​
 +
 +I had confirmed that the PHP version assigned to the site was PHP 7.3 like so (which works thanks to the PHPRC patch in cPanel). By changing into the user's (username) DOCROOT for the site and then checking the PHP version in use, I was able to confirm that the PHP version assigned to the site was PHP 7.3 and thus concluded that WP-CLI should work perfectly fine with the site:
 +
 +  cd /​home/​username/​public_html
 +  php -v
 +
 +For cPanel servers, you could alternatively check the .htaccess/​.user.ini/​php.ini files local to the document root for the site. If no PHP version is explicitly specified, then the default PHP version is in use.
 +
 +Note that these commands are written in a manner that allows the root user to execute WP-CLI as the user via a bash shell but still remain logged in as the root user. Thus, the commands are executed with the proper ownership but do not require you to su to the user for the remaining terminal session. ​
 +
 +WP-CLI documentation states that you can specify the PHP version to use via an environment variable **only in non-phar installations**. ((https://​make.wordpress.org/​cli/​handbook/​config/​))
 +
 +So, for a non-phar installation of WP-CLI, one could set the env variable on demand just before your command:
 +
 +  WP_CLI_PHP=/​opt/​cpanel/​ea-php73/​root/​usr/​bin/​php
 +
 +Running as the user '​username'​ while logged in as root, you would issue the following command (for a non-phar installation):​
 +
 +   su -s /bin/bash -c "​WP_CLI_PHP=/​opt/​cpanel/​ea-php73/​root/​usr/​bin/​php /​usr/​local/​cpanel/​3rdparty/​bin/​wp --info"​ username
 + 
 +This will give output detailing the environment and PHP binary and information used (in which you'll want to pay close attention to the PHP version detected). Note that when ran with a phar installation of WP-CLI that the PHP binary shown is that which you manually specified in the command, however, the PHP used is still that of the default PHP version, PHP 5.5:
 +
 +<​code>​
 +root@host [/​home/​username/​public_html]#​ su -s /bin/bash -c "​WP_CLI_PHP=/​opt/​cpanel/​ea-php73/​root/​usr/​bin/​php /​usr/​local/​cpanel/​3rdparty/​bin/​wp --info"​ username
 +OS: Linux host.domain.tld 3.10.0-957.12.2.vz7.96.21 #1 SMP Thu Jun 27 15:10:55 MSK 2019 x86_64
 +Shell: /bin/bash
 +PHP binary: /​opt/​cpanel/​ea-php73/​root/​usr/​bin/​php
 +PHP version: 5.5.38
 +php.ini used: /​opt/​cpanel/​ea-php55/​root/​etc/​php.ini
 +WP-CLI root dir: phar://​wp-cli.phar/​vendor/​wp-cli/​wp-cli
 +WP-CLI vendor dir: phar://​wp-cli.phar/​vendor
 +WP_CLI phar path: /​home/​username/​public_html
 +WP-CLI packages dir:
 +WP-CLI global config:
 +WP-CLI project config:
 +WP-CLI version: 2.1.0
 +root@host [/​home/​username/​public_html]#​
 +</​code>​
 +
 +As we have stated, specifying the WP_CLI_PHP environment variable doesn'​t work with phar installations of WP-CLI. So, how can we resolve this?
 +
 +I found the following, which worked for me for the cPanel server:
 +
 +   su -s /bin/bash -c "/​opt/​cpanel/​ea-php73/​root/​usr/​bin/​php /​usr/​local/​cpanel/​3rdparty/​share/​cpanel-wp-cli/​wp-cli.phar --info"​ username
 +
 +For DirectAdmin,​ you may want to try the following:
 +
 +  su -s /bin/bash -c "/​usr/​local/​php73/​bin/​php ​ /​usr/​local/​bin/​wp --info"​ username
 +
 +Note that the command above was ran for the user //​username//​ inside the user's site document root (/​home/​username/​public_html/ ​ is the default document root for primary domains in cPanel).
 +
 +Notice that upon running this command like so, the correct PHP version is shown in both the binary and the PHP version used:
 + 
 +<​code>​
 +root@host [/​home/​username/​public_html]#​ su -s /bin/bash -c "/​opt/​cpanel/​ea-php73/​root/​usr/​bin/​php /​usr/​local/​cpanel/​3rdparty/​share/​cpanel-wp-cli/​wp-cli.phar --info"​ username
 +OS: Linux 3.10.0-957.12.2.vz7.96.21 #1 SMP Thu Jun 27 15:10:55 MSK 2019 x86_64
 +Shell: /bin/bash
 +PHP binary: /​opt/​cpanel/​ea-php73/​root/​usr/​bin/​php
 +PHP version: 7.3.9
 +php.ini used: /​opt/​cpanel/​ea-php73/​root/​etc/​php.ini
 +WP-CLI root dir: phar://​wp-cli.phar/​vendor/​wp-cli/​wp-cli
 +WP-CLI vendor dir: phar://​wp-cli.phar/​vendor
 +WP_CLI phar path: /​home/​username/​public_html
 +WP-CLI packages dir:
 +WP-CLI global config:
 +WP-CLI project config:
 +WP-CLI version: 2.1.0
 +root@host [/​home/​username/​public_html]#​
 +</​code>​
 +
 +Hopefully this information can save someone else valuable time by preventing the need for switching from the root to the user shell and by preventing them from needing to manually perform changes that could otherwise be done much quicker via WP-CLI as long as the correct PHP version is detected. ​
 +
 +\\
 +
 +===== Quick Workaround Reference =====
 +
 +The following details how to run WP-CLI with a specified PHP version as a user //​username//​ without leaving the root user's shell for a phar WP-CLI installation. Be sure to replace phpXX with the correct PHP version (i.e., php56, php70, php73, etc.,​). ​
 +
 +\\
 +
 +==== cPanel ====
 +
 +From within the site's document root as the user //​username//​ while logged in terminal as root:
 +
 +   su -s /bin/bash -c "/​opt/​cpanel/​ea-phpXX/​root/​usr/​bin/​php /​usr/​local/​cpanel/​3rdparty/​share/​cpanel-wp-cli/​wp-cli.phar --info"​ username
 +
 +\\
 +
 +==== DirectAdmin ====
 +
 +From within the site's document root as the user //​username//​ while logged in terminal as root:
 +
 +  su -s /bin/bash -c "/​usr/​local/​phpXX/​bin/​php ​ /​usr/​local/​bin/​wp --info"​ username
 +
  
control-panels/directadmin/wrong-php-version-detected-using-multiple-php-versions.txt · Last modified: 2019/11/07 21:05 by Karson N.