Multiple PHP versions using phpenv, php-build and php-version - php

I'm on Arch Linux, trying to configure multiple PHP versions for testing my scripts.
I'm basing myself on these three tools:
https://github.com/phpenv/phpenv
https://github.com/CHH/php-build/
http://wilmoore.com/php-version/
First I installed Apache and PHP (and their integration package) with pacman:
# pacman -S php apache php-apache
Next, I followed the instructions for installing the tools on the links, everything is working fine.
I manage to install two versions with phpenv-install from php-build tool. I got something like this:
-- ~
|---.phpenv
|---lib
|---versions
|---5.5.1
|---5.4.17
|--- (some more folders)
On CLI environment everything is working fine, I can easily switch between the two versions with php-version:
$ php-version 5.5.1
$ php --version
PHP 5.5.1 (cli) (built: Aug 5 2013 22:54:47)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
with Zend OPcache v7.0.2-dev, Copyright (c) 1999-2013, by Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
$ php-version 5.4.17
$ php --version
PHP 5.4.17 (cli) (built: Aug 5 2013 23:19:44)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
Now the problem: integrate this with apache.
As I said in the beginning of this post, I also had to install PHP from Arch repositories, which coincidentally is on version 5.4.17. This installation is working fine with Apache.
On php-env readme there are these instructions:
phpenv support dynamic switching for Apache apxs libraries and install
will build and install a libphp5.so shared library for Apache under
the versions libexec folder.
By calling phpenv global to show or change the global PHP version a
link is created under ~/.phpenv/lib/libphp5.so for the appropriate
release build. This link can be used for Apache's LoadModule
php5_module directive and requires Apache to restart when changed.
The problem is that there is no file or symlink named libphp5.so in lib directory neither in the whole .phpenv folder, because
$ find ~/.phpenv -name libphp5.so
returns nothing.
Maybe because english is not my first language, I'm having some problem to intepret the second paragraph of the quote above.

There is a package in the AUR for phpenv
https://aur.archlinux.org/packages/phpenv/
As well as packages for many different versions of PHP
https://aur.archlinux.org/packages/?O=0&C=0&SeB=nd&K=phpenv&outdated=&SB=n&SO=a&PP=50&do_Search=Go
First install phpenv:
yaourt phpenv
Then install the php versions you need, for example:
yaourt php53_29_env
Note: even with this though, you will likely have a problem. Checking the package build, it looks like it is missing the patch to disable PHP automatically being compiled with thread safe features when apache is running a thread safe mpm. Not quite sure since on my systems, I want PHP to enable thread safe code so it will work for me.

Related

Set default version of Php in CentOS 7

I have two versions of PHP in opt/remi folder php56 and php72
but when I php -v on cmd it shows:
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans
How to set default version to PHP 7.2?
I have two versions of PHP in opt/remi folder php56 and php72
how to set default version to PHP 7.2
SCL are designed for parallel installation so don't alter default version in base system
Once the collection is enabled, the version will be used
$ scl enable php72 bash
$ php -v
PHP 7.2.8 (cli) (built: Jul 17 2018 05:35:43) ( NTS )
If you want 7.2 to be the default version (base system) you should install it, according to Wizard instructions for "Default / single version" (and keep 5.6 as secondary version)
Change php cli version in Centos 7
First, find your php7, run phpinfo() and get path or you can do with other ways. for me, it is:
/usr/local/lsws/lsphp73/
then:
cd ~
. ~/.bash_profile
And:
alias php='/usr/local/lsws/lsphp73/bin/php'
Now:
php -v
PHP 7.3.13 (cli) (built: Dec 20 2019 16:02:35) ( NTS )
Create a file "/etc/profile.d/php.sh". Use pathmunge to add the path to your php bin you want as default on line one and save the file.
Example:
pathmunge /opt/remi/php73/root/bin
Reload your profile afterwards by logging in again.
Now if you do a which php and php -v you should see the following output in my case
[root#host etc]# which php
/opt/remi/php73/root/bin/php
[root#host etc]# php -v
PHP 7.3.4 (cli) (built: Apr 2 2019 13:48:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.4, Copyright (c) 1998-2018 Zend Technologies
with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v10.3.4, Copyright (c) 2002-2019, by ionCube Ltd.
This is the preferred way to accomplish this task using tools that are already supplied on a minimal install. This also allows scripts and commands to hit the correct php binaries when accomplishing other tasks. Commands like, pear, pecl, phar, php-config. You want your experience to be global when setting the default, otherwise you might wind up still getting version 5.6's tools when trying to install an extension or complete another task.
module enable php74
for your understanding:
cat /opt/remi/php74/enable
export PATH=/opt/remi/php74/root/usr/bin:/opt/remi/php74/root/usr/sbin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/opt/remi/php74/root/usr/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export MANPATH=/opt/remi/php74/root/usr/share/man:${MANPATH}

Why composer uses other PHP version then php command points to?

Here's my environment configuration (Ubuntu).
I have installed PHP 5.5.9 version via apt-get, and using PHPBrew also installed version 7.0.6. The PHP7 version is turned on.
$ which php
/home/username/.phpbrew/php/php-7.0.6/bin/php
$ php -v
PHP 7.0.6 (cli) (built: May 12 2016 08:54:46) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans
I have composer installed globally by putting it in proper directory:
$ which composer
/usr/local/bin/composer
Now, when I run composer require <something>, it uses PHP 5.5.9 and returns error like (when the package being installed requires it of course):
Problem 1
- This package requires php >=5.6.0 but your PHP version (5.5.9) does not satisfy that requirement.
Why doesn't composer use PHP version proper for current CLI configuration?
Please note that my question is why it happens and not how to solve the problem, because I've found already a few workarounds for that. I want to understand what happens here.

Use different PHP version CLI executable for one command

So I have Gentoo box with three PHP versions installed (nevermind the reasons):
/usr/bin/php -> /usr/lib64/php5.4/bin/php
/usr/bin/php5.5 -> /usr/lib64/php5.5/bin/php
/usr/bin/php5.6 -> /usr/lib64/php5.4/bin/php
I want to install Laravel framework using composer:
$ composer create-project laravel/laravel --prefer-dist
This however throws an error because Laravel requires PHP > 5.5.9 and the default php interpreter is 5.4.
So I issue another command:
$ /usr/bin/php5.6 /usr/bin/composer create-project laravel/laravel --prefer-dist
This takes me one step further, but then some post-install commands from Laravel's composer.json comes into play, and installation crashes.
This is due to the fact, that composer.json commands look like this:
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
As you can see, the "default" interpreter is used again!
Now, proper PHP files start with following shebang:
#!/usr/bin/env php
This is nice feature as PHP interpreter can be found under different locations on different systems.
Unfortunatelly, in this case env command returns path to the first executable it finds in $PATH environmental variable.
How could I possibly alter current session environment or what kind of trick to perform so for the execution of whole Laravel installation process php command would invoke /usr/bin/php5.6 instead of /usr/bin/php?
I don't want to change $PATH variable or modify files like composer, composer.json or Laravel's CLI utility artisan.
Edit: also assume that I want to do this from regular user account (i.e. with no root permissions).
Default PHP executable can be found using:
$ which php
In most cases it is link to particular PHP version:
lrwxrwxrwx 1 root root 21 aug 15 2016 /usr/bin/php -> /usr/bin/php7.1
To change it to different version just relink it to another
$ sudo rm /usr/bin/php
$ sudo ln -s /usr/bin/php5.6 /usr/bin/php
Before relink you have to make sure target PHP version is installed.
Maybe you can try to fix the environnement!
$ php -v
PHP 5.4.x (cli) ...
$ set PATH="/usr/lib64/php5.6/bin:$PATH"
$ php -v
PHP 5.6.x (cli) ...
Or, if you don't want to modify the PATH for your shell session, you can scope the change for the current command only:
$ php -v
PHP 5.4.x (cli) ...
$ env PATH="/usr/lib64/php5.6/bin:$PATH" php -v
PHP 5.6.x (cli) ...
$ php -v
PHP 5.4.x (cli) ...
Identify where the current generic php command is and to which binary it points to with which php.
It will give you a path to a symlink like you mention in your question
/usr/bin/php -> /usr/lib64/php5.4/bin/php
Edit the symlink to point to which ever php version you want for now, see here
https://unix.stackexchange.com/questions/88824/how-can-i-edit-symlinks
When you are done just reverse the process.
UPDATE:
you can also add an alias for the current user by editing ~/.bashrc and adding the following
alias php='/usr/bin/php5.6'
see if this works out
Since PHP7 came around Debian Linux creates different executables for PHP versions 5 and 7 in /usr/bin by default (if you install both versions that is).
Calling those different versions from the command line is as simple as ever now:
kkarski#debian:~ $ php5 -v
PHP 5.6.26-0+deb8u1 (cli) (built: Sep 21 2016 12:37:50)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
kkarski#debian:~ $ php -v
PHP 7.0.9-1~dotdeb+8.1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.9-1~dotdeb+8.1, Copyright (c) 1999-2016, by Zend Technologies
This is obviously only good for simple scripts. For anything larger (composer, artisan etc.) you'll have to change the PATH variable.
To change the version your Apache server is using all you have to do is:
root#debian:~# a2dismod php5 && a2enmod php7.0
Module php5 disabled.
To activate the new configuration, you need to run:
service apache2 restart
Considering conflict php5 for php7.0:
Enabling module php7.0.
To activate the new configuration, you need to run:
service apache2 restart
and vice versa if you want to use the lower PHP version.
Mentioning it in case someone has similar problems on Debian.
I find the easiest to achieve the same like just create a softlink like for example
ln -s /opt/php-7.0.32/bin/php /usr/bin/php7
ln -s /opt/php-7.1/bin/php /usr/bin/php71
ln -s /opt/php-5.6/bin/php /usr/bin/php56
then as you use your default version say it is php7.2 as just php for alternative version you can you php7 or php71 or php56
here ln -s /opt/php-7.1/bin/php /usr/bin/php71 is the source/orginal file and /usr/bin/php7 is the destination / link
For anyone else who found no solution in the above, because they use composer update and somehow the wrong PHP version gets used. By using composer self-update I got some more info and eventually found out that in the composer.json you can specify a platform in the config section, which overrides what php version is used by composer. Simply changing this value or removing this config solved my issue.
composer.json
"config": {
"platform": {
"php": "7.1"
},
It's possible to do using alias, but keep in mind that aliases are not expanded by default.
You must also enable expanding of those.
$ shopt -s expand_aliases
$ alias php="/usr/local/bin/php-5.6"
$ ./some-script.sh
$ unalias php # back to previous version
I worked on a "script + docker image" to make multiple php versions available whenever I want during development: https://github.com/jclaveau/docker-php-multiversion
You can use it this way:
$ php 5.6 -v
PHP 5.6.40-15+ubuntu18.04.1+deb.sury.org+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
$ php 7.3 -v
PHP 7.3.13-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Dec 18 2019 14:48:49) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.13, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.13-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.9.0, Copyright (c) 2002-2019, by Derick Rethans
As it uses Docker.io, you do not need to alter any part of your system configuration.
Hoping it would help you

Installing Xdebug for use with PHPStorm

I want to install a debugger for PHP for my MAMP environment on Mac OS 10.8.5, where I run PHP 5.5.3. I am looking at PHPStorm and Xdebug. I am doing my best to follow the instructions on the PHPStorm site.
Here are my steps:
1) Download the Xdebug extension compatible with your PHP version
$ su admin
$ cd /Applications/MAMP/bin/php/
$ git clone git://github.com/xdebug/xdebug.git
I understand that this installs Xdebug 2.3.2 (Release date: 2015-03-22)
2) Copy the extension file to your web server in a location that is accessible by the web server. I do not understand what this extension file is, so I have been unable to complete this step.
3) Add the following lines at the end of your php.ini file:
[Xdebug]
zend_extension="/Applications/MAMP/bin/php/xdebug/php_xdebug.h"
xdebug.remote_enable=1
xdebug.remote_host="localhost"
xdebug.remote_port="9000"
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/Applications/MAMP/tmp"
Note the additional final line, suggested by this page.
4) Restart your web server
5) Check your installation
$ php --version
PHP 5.3.28 (cli) (built: Jan 23 2014 18:02:48)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
Not the right version of PHP. Trying again:
$ /Applications/MAMP/bin/php/php5.5.3/bin/php --version
PHP 5.5.3 (cli) (built: Sep 18 2013 14:31:13)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
There is no sign of "With Xdebug..."
What exactly should I do at step 2? Have I made a mistake at any of the other steps? Are there further steps that I am missing?

OSX xdebug and relation to PEAR

I have the Acquia Drupal stack 7.4.4 - which is using PHP 5.2.17 installed on an OSX 10.6.8
Drupal's PHP Stack:
User [~/]> php -v
PHP 5.2.17 (cli) (built: Jun 30 2011 12:12:11)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
OS X's PHP Stack:
User [~/]> /usr/bin/php -v
PHP 5.3.4 (cli) (built: Dec 15 2010 12:15:07)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
As seen the Xdebug binary for PHP 5.2 downloaded from http://code.activestate.com/komodo/remotedebugging/ is correctly loaded with PHP 5.2.17 This is also verified with phpinfo.
The purpose for my questions is to reach a stage where I can compile the xdebug.so for the Acquia Drupal stack to be used with NetBeans 7.
So - which version of PEAR / PECL is the correct one to have installed on my system, the one for PHP 5.2.17 or the one for 5.3.4 - or does it not matter?
Thanks
You need a different .so for 5.2 and 5.3 as they have incompatible APIs. A "PEAR/PECL" version makes little sense as Xdebug only uses PEAR as the installer. Just make sure you have a PHP 5.2 and a PHP 5.3 Xdebug.so. The Komodo download provides both.
Great - "Xdebug only uses PEAR as the installer" - I had, with my limited experience, been wondering if those libraries were also used within xdebug/Acquia/NetBeans while actually debugging the code. So PEAR/PECL's sole pupose is now clarified, and it also remains clear that xdebug needs to be compiled for PHP 5.2 as that is the PHP version used by the Acquia 7.4.4 stack.
Now relying on Komodo binaries, without knowing how they were derived and why they do actually work, or even worse - why my own compilations does not work - that is a possible short cut and it does work for me for the time being.
but
... neither Xdebug, Drupal, Acquia or Netbeans provides an up to date description on how to compile the xdebug on a Mac for these tools used on a OS X 10.6.8 (64-bit intel). I assume a simple but up to date and clear step-by-step-guide e.g. as bullet points would benefit a lot of developers-want-to-be ... any takers?

Categories