Change Default version of PHP that is called on the command line - php

is there a way to change the default version of PHP that is called on the command line?
I am trying to install Laravel using composer the problem is composer is calling php (which is version 4.4) when it needs to call php5.4, i have changed the binary in the header from #!/usr/bin/env php to #!/usr/bin/env php5.4 but this throws an error!
here is my php set-up
php: /usr/bin/php /usr/bin/php5.4 /usr/bin/php5.5 /usr/lib/php /usr/lib/php.ini-nourl /usr/lib/php.ini /usr/lib/php5.4 /usr/lib/php5.5 /usr/local/bin/php /usr/local/bin/php5.4 /usr/local/bin/php5.5 /usr/local/lib/php.ini-nourl /usr/include/php /usr/include/php5.4 /usr/include/php5.5 /usr/local/php /usr/share/php

There are a couple options here, but really this is not php specific, this is about basic execution path rules.
The #!/usr/bin/env php header means that when you run composer alone it runs it with php, so if you manually run it like php5.4 /path/to/composer you can bypass that header.
The other option is to define a symlink in /usr/local/bin/php or something that:
points to /usr/bin/php5.4
is in a directory that has precedence over /usr/bin inside your PATH environment variable.

Related

How can I use a composer generated bin script with a different php binary?

I'm using a PHP library that has a cli script file with shebang on top:
#!/usr/bin/env php
<?php
This file is meant to be used by cli so it's defined as a bin on composer config as expected.
When I do a composer install, the composer generates a link to this file on vendor/bin folder like this:
#!/usr/bin/env sh
dir=$(d=${0%[/\\]*}; cd "$d" > /dev/null; cd "../lib-owner/lib" && pwd)
dir=$(echo $dir | sed 's/ /\ /g')
"${dir}/lib" "$#"
The problem is that with this setup, every call to vendor/bin/the-library is just forwarded to the php script file, and it lets the shebang resolve the PHP version, but I need this script to be called with a specific php binary.
I can resolve it by calling the script file directly: php73 /vendor/lib-owner/lib/lib/the-script.
Or by cloning the script into my root folder and calling it there in the place of the vendor: php73 the-script.
The problem is that both ways has some drawbacks and I would prefer something more stable and less hacky.
Is there anyway?
You could do this:
env PATH="/path/to/folder/where/desired/php/resides:$PATH" vendor/bin/the-library
What it does is alter the PATH environment variable just for the duration of that command, by prepending the path to the desired PHP interpreter.
This way, /usr/bin/env will use that PHP version instead of the default one (and the PATH environment variable will revert back to its previous value as soon as the script is finished).

how to create autoload under vendor directory for php rabbitmq

I m new to php and rabbitmq in debian(Linux). I have installed xampp, rabbitmq and also installed composer.phar in project directory using below command
/opt/lampp/htdocs/rabbitmq_demo# curl -s https://getcomposer.org/installer | /opt/lampp/bin/php
Now I use Composer to install the dependencies of the project using below command
composer.phar install
but it thrown an error as below
bash: php: command not found
I have preferred the link https://getcomposer.org/doc/00-intro.md
I want to prepare autoload.php
Please help me to create autoload under vendor directory.
You do not have a php cli program installed on your computer or it is not in your current $PATH variable. Please install PHP first or correct your $PATH environment variable.
Once you have this, run the composer.phar install again. This will download all dependencies listed in your composer.json file. Once the program completes, you will have a file ``vendor/autoload.php`. You can just require this file at the beginning of your own script and everything will be taken care of.
You need to add the path to the PHP command line (CLI) in the XAMPP install, to your bash environment. (You'd think the installer would do this!)
The XAMPP PHP CLI on Debian is at /opt/lampp/bin/php
So you need to add /opt/lampp/bin to your $PATH environment variable.
See this answer for the various options in modifying your path depending on who you want to be able to run PHP.
/etc/login.defs
/etc/environment
/etc/profile
~/.bashrc
In one of those files, you append to the path thus:
PATH=$PATH:/opt/lampp/bin
and re-login.

Is #!/usr/bin/env required to run PHP from command line?

Often times when I see PHP that is meant to be ran from the command line, it will have this line #!/usr/bin/env php at the top of the file like this...
#!/usr/bin/env php
<?php
// code
?>
I was wanting to know if this is meant just for when the file is ran on a Linux/Unix system or is needed for running on Windows as well?
The shebang line is required for auto-detection of the type of script. It enables this sort of usage:
[pfisher ~]$ chmod +x run-me.php
[pfisher ~]$ ./run-me.php
That line is not needed if you pass the filename as an argument to the php interpreter, like so:
[pfisher ~]$ php run-me.php
Edit: replace "hashbang" with shebang.
No it's not, you can directly use
#!/path/to/php
Running php (or anything else) through the env utility is a weak security measure. Dpending on the platform, will "fix" PATH, LIB, and other environment variables according to various config files and potentially remove some of the dangerous values in there (e.g. env on HPUX).
It is also to limit the scope of shell-expansions on certain environments. (See man 1 env on Linux).
the magic belongs to the executable flag (chmod +x FILE)
the shebang: and if it exists with that version you may expect
/usr/bin/env cliVersion -> /usr/bin/env php
php -v # tells you which version you have by default as: 'cli' in a shell
alternativly use e.g:
php8.0 /to/php/script.php
to run it without a shebang at the first line of the script.
(it will work even if it stays there but check the real php version on execution if important for you)
"standard"? over the last 10 years is /usr/bin/env depending on what version you set to be the default on your system:
debian systems (debian,unbuntu,kubuntu...): #root: update-alternatives --config php will guide you

PHP: How can I tweak PHPUnit to use a different PHP interpreter?

My system has two PHP interpreters. One came bundled with the OS and the other I installed via the XAMPP package. All of my PHP extensions are applied to the XAMPP installation but PHPUnit seems to only run the version of PHP that came with my machine.
Does anybody know of a way I can configure or rebuild PHPUnit so that it uses my XAMPP PHP interpreter exclusively?
For Mac/Linux, the first line of the phpunit script with starts with
#!/usr/bin/php
change that to
#!/Applications/XAMPP/xamppfiles/bin/php
or whatever other php interpret you want to use.
Find the folder you installed PHPUnit in. There should be a file called phpunit.bat. It should have a line that reads something like
set PHPBIN="C:\php\php.exe"
%PHPBIN% "C:\php\phpunit" %*
Change it to read
set PHPBIN="C:\xampp\php\php.exe"
%PHPBIN% "C:\xampp\php\phpunit" %*
Or whatever the path to your PHP executable is
Since modifying phpunit file did not work for me because of phar signature errors, I was running phpunit with different php version by calling interpreter explicitly (on Linux):
php7.1 /usr/local/bin/phpunit
php5.6 /usr/local/bin/phpunit
Following the example with XAMPP, full path to php interpreter could be provided:
/Applications/XAMPP/xamppfiles/bin/php /usr/local/bin/phpunit
In agreement with Thomas' statement, additionally there's a line further below
if (strpos('/Applications/MAMP/bin/php5.3/bin/php', '#php_bin') === 0) {
set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
}
That I've been told you're also supposed to change to reflect the PHP you're interested in using
(I've set mine to MAMP obviously)
I've switched back and forth from 5.2 and 5.3 a lot recently :)
This applies to phpunit installed using Homebrew on Mac OS 10.9. I’ve editing the file located at /usr/local/Cellar/phpunit/4.2.6/bin as seen below. CAVEAT: I don’t know how Homebrew will handle this on a PhpUnit update, but for now it’s working to be able to select the php version that PhpUnit is using for it's testing.
#!/usr/bin/env bash
php=/Applications/MAMP/bin/php/php5.3.14/bin/php
#php=/Applications/MAMP/bin/php/php5.4.4/bin/php
/usr/bin/env $php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/Cellar/phpunit/4.2.6/libexec/phpunit-4.2.6.phar $*
On Windows, this may be achieved using a similar approach to the ones mentioned in other replies.
In your /path/to/composer/phpunit directory, open the phpunit file in an editor. The first line should look like this:
#!/usr/bin/env php
Simply download your desired version of PHP for Windows, place the contents of the ZIP file somewhere to your liking, and reference the fully quantified path to the php.exe file, instead of just php. Like so:
#!/usr/bin/env /c/misc/php-5.5.9-nts-Win32-VC11-x86/php.exe
In my case, I put it in /c/misc/php-5.5.9-nts-Win32-VC11-x86/, which corresponds to C:\misc\php-5.5.9-nts-Win32-VC11-x86\ using Windows path syntax.
Remember to verify that the correct php.ini file is being used (php --ini or in a script file php_ini_loaded_file()).

linux - running php script from command line when php is installed as apache module

Normally when I want to run a php script from the command line I just create a php page, add a shebang pointing to the php binary, then ./file.php to run it. Since I have php installed as an apache module, I'm not even sure what my shebang should look like. Any ideas?
The CLI version of PHP had been part of the default installation since 4.3 and has to be explicitly turned off when PHP is being built. If you have access to the command line try
$ php -v
If you don't get a command not found error then you should be ready to go.
To actually run a php file from the command line do this:
$ php -f file.php
If it's just an Apache module, I don't think you can do it… At least, not without using a script like this:
$ cat run_php_with_apache
#!/bin/sh
cp "$1" /var/www/
curl "http://localhost/`basename "$1"`"
rm "/var/www/`basename "$1"`"

Categories