Linux php executable path

How can I get the current PHP executable from within a script?

I want the inner script to be run with the same PHP version. In Perl, I would use $^X to get the Perl executable. It appears there isn’t any such variable in PHP. Right now, I’m using $_SERVER[‘_’] , because Bash (and zsh) sets the environment variable $_ to the last-run program. But, I’d rather not rely on a shell-specific idiom. UPDATE: Version differences are but one problem. If PHP isn’t in PATH, for example, or isn’t the first version found in PATH, the suggestions to find the version information won’t help. Additionally, csh and variants appear to not set the $_ environment variable for their processes, so the workaround isn’t applicable there. UPDATE 2: I was using $_SERVER[‘_’] , until I discovered that it doesn’t do the right thing under xargs (which makes sense. zsh sets it to the command it ran, which is xargs , not php5 , and xargs doesn’t change the variable). I am falling back to using:

$version = explode('.', phpversion()); $phpcli = "php"; 

Is it possible to simply conditionally include the other PHP file? That’s the easiest way to insure the included code runs with the same interpreter as the including code.

Interesting point. In this particular case, part of the reason it’s being run as a subprocess is that the inner script calls ‘exit()’ at various places.

9 Answers 9

It is worth noting that now in PHP 5.4+ you can use the predefined constant PHP_BINARY:

PHP_BINARY

Specifies the PHP binary path during script execution. Available since PHP 5.4.

This returns C:\php on Windows where no such file or directory exists (PHP 7.1.11, Apache 2.4.29, Windows 7 Pro) — as, as noted in a comment on another answer, does PHP_BINDIR .

And on the hosting company’s Linux box it also returns the same as PHP_BINDIR . Here it is /usr/bin and does contain a php executable, but that is the version 5.3 executable not the 7.0.25 version of the calling environment. So I don’t think this answer is useful.

For php-fpm (on Debian) this returns /usr/sbin/php-frpm7.3 which cannot be used to launch any PHP script. While technically correct, it’s completely useless for OP’s purpose.

On my server I’ve PHP 5.3.14.

I’ve found a predefined constant: PHP_BIN_DIR

Then, supposing the file name of the executable file is always ‘php’, $php_cmd = PHP_BIN_DIR.’/php’ points to my PHP executable file.

According to the manual, which doesn’t say when it was added, it’s PHP_BINDIR (no second underscore). This is a pretty decent answer. Thanks.

I upvoted the answer and the comment but took the upvotes back after trying it in Windows. Will always return C:\php no matter the reality.

Nice answer, however, it won’t work as expected if multiple versions of PHP are installed on a Linux system, since they will likely all be in /usr/bin .

Читайте также:  Create root password linux

Update; nowadays PHP_BINARY works with XAMPP as well (Tested with XAMPP that comes with PHP 7.4).

Old Answer

Unfortunately PHP_BINARY is returning the httpd binary (on Windows XAMPP), so I’m back to using paths.

 if (defined('PHP_BINARY') && PHP_BINARY && in_array(PHP_SAPI, array('cli', 'cli-server')) && is_file(PHP_BINARY)) < return PHP_BINARY; >else if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') < $paths = explode(PATH_SEPARATOR, getenv('PATH')); foreach ($paths as $path) < if (substr($path, strlen($path)-1) == DIRECTORY_SEPARATOR) < $path = substr($path, 0, strlen($path)-1); >if (substr($path, strlen($path) - strlen('php')) == 'php') < $response = $path.DIRECTORY_SEPARATOR . 'php.exe'; if (is_file($response)) < return $response; >> else if (substr($path, strlen($path) - strlen('php.exe')) == 'php.exe') < if (is_file($response)) < return $response; >> > > else < $paths = explode(PATH_SEPARATOR, getenv('PATH')); foreach ($paths as $path) < if (substr($path, strlen($path)-1) == DIRECTORY_SEPARATOR) < $path = substr($path, strlen($path)-1); >if (substr($path, strlen($path) - strlen('php')) == 'php') < if (is_file($path)) < return $path; >$response = $path.DIRECTORY_SEPARATOR . 'php'; if (is_file($response)) < return $response; >> > > return null; 

Has someone tried it? Does it work? Consistently and across Win and Unix? The code is ugly, but if it works, I’ll normalize it.

I know this is an old answer an no one has answers @XedinUnknown but I can confirm this works perfectly on Windows running XAMPP. Has anyone else tested this in more environments?

Okay, so this is ugly, but it works on Linux:

It doesn’t look like there’s any easy way to do this for Windows. Some Windows executables like tasklist can give you the name of the executable, but not the full path to where the executable is. I was only able to find examples for finding the full path given a PID for C++, AutoHotkey and the like. If anyone has any suggestions on where else I could look, let me know.

PS: To get the PID in PHP for Windows, you apparently have to call getmypid().

I was looking into doing this too. I thought I might be able to use the w32api or ffi PECL extensions to call the GetModuleFileName API function, but neither extension seems to be actively maintained at the moment.

You could use phpversion() to get the current version of PHP before you execute the «inner» script.

After being dissatisfied with the answers, I came up with my own. It tries to use PHP_BINARY if it looks reasonable, otherwise it looks for an interpreter with the same version as the currently running one.

/** * Return a suitable PHP interpreter that is likely to be the same version as the * currently running interpreter. This is similar to using the PHP_BINARY constant, but * it will also work from within mod_php or PHP-FPM, in which case PHP_BINARY will return * unusable interpreters. * * @return string */ public function getPhpInterpreter(): string < static $cachedExecutable = null; if ($cachedExecutable !== null) < return $cachedExecutable; >$basename = basename(PHP_BINARY); // If the binary is 'php', 'php7', 'php7.3' etc, then assume it's a usable interpreter if ($basename === 'php' || preg_match('/^php\d+(?:\.\d+)*$/', $basename)) < return PHP_BINARY; >// Otherwise, we might be running as mod_php, php-fpm, etc, where PHP_BINARY is not a // usable PHP interpreter. Try to find one with the same version as the current one. $candidates = [ 'php' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION, 'php' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, 'php' . PHP_MAJOR_VERSION, ]; $envPath = $_SERVER['PATH'] ?? ''; $paths = $envPath !== '' ? explode(':', $envPath) : []; if (!in_array(PHP_BINDIR, $paths, true)) < $paths[] = PHP_BINDIR; >foreach ($candidates as $candidate) < foreach ($paths as $path) < $executable = $path . DIRECTORY_SEPARATOR . $candidate; if (is_executable($executable)) < $cachedExecutable = $executable; return $executable; >> > // Fallback, if nothing else can be found $cachedExecutable = 'php'; return $cachedExecutable; > 

Let me know if this works for you. I tested it on Debian Buster, from CLI and from FPM.

Читайте также:  Linux открыть файловый менеджер через терминал

Источник

What is php_executable_path in LAMP ubuntu12.04?

I am installing wordpress using Google App Engine and using this command to run the application, app_dir contains app.yaml, php.ini and wordpress:

google_appengine/dev_appserver.py app_dir/ 

File «/home/g1m/google_appengine/google/appengine/tools/devappserver2/php_runtime.py», line 222, in new_instance self._check_environment(php_executable_path) File «/home/g1m/google_appengine/google/appengine/tools/devappserver2/php_runtime.py», line 147, in _check_environment ‘flag (%s) does not exist.’ % php_executable_path) _PHPBinaryError: The path specified with the —php_executable_path flag () does not exist.

Kindly help me to solve this, what is the value of php_executable_path in LAMP as I am using UBUNTU12.04 operating system, is it /etc/php/cgi ? Kindly let me know where I am doing wrong.

I figure it out, to know what is executable path install php-cgi binar, developers.google.com/appengine/docs/php/gettingstarted/…

2 Answers 2

Make sure you install it first by doing:

sudo apt-get install php5-cgi 

then locate it by running a search for php-cgi

in my case i found it in : /usr/bin/php-cgi

I think the reason for this error is that GAE need to work with cgi not cli. The difference in them is that cli (command line interface) is for standalone application, not for web app (it didn’t output html header by default). If php-cgi is installed , you can specify its path like this when you start dev server

app_devserver.py --php_executable_path=/usr/bin/php-cgi

If you are not sure, you could search for it like dsb005 suggested. If it’s not installed. hmm. Maybe you miss this one on GAE document :

HP 5.4 is not packaged on most Linux distributions so it may be easiest to install it from source. On Debian-based Linux systems, you can use the following commands to install PHP 5.4 in such a way that it won’t effect any other versions of PHP that you may have installed:

Источник

Setting PHP Executable Path in VS Code in Ubuntu

Getting Unexpected ‘Unknown’ , ‘;’ expected. , ‘VariableName’ expected. error.

These kind of lines are causing error

namespace App\Http\Controllers; //Unexpected 'Unknown' and ';' expected Error use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; 

Here, Screenshot of my error:

enter image description here

I’ve have added PHP executable path VS Code article. But still not working

php: /usr/bin/php8.0 /usr/bin/php.default /usr/bin/php /usr/lib/php /etc/php /usr/share/php8.0-mysql /usr/share/php8.0-readline /usr/share/php8.0-common /usr/share/php8.0-zip /usr/share/php8.0-xml /usr/share/php8.0-bz2 /usr/share/php /usr/share/php7.1-common /usr/share/php8.0-opcache /usr/share/php8.0-mbstring /usr/share/php8.0-gd /usr/share/php7.1-mcrypt /usr/share/php8.0-curl /opt/lampp/bin/php /usr/share/man/man1/php.1.gz 

Giving me following message

Cannot validate since usr\bin\php is not a valid php executable. Use the setting ‘php.validate.executablePath’ to configure the PHP executable.

1 Answer 1

I resolved with this executable path to configure the PHP executable in VS Code:

  < "php.validate.executablePath": "/usr/bin/php8.1" >whereis php php: /usr/bin/php /usr/bin/php8.1 /usr/lib/php /etc/php /usr/share/php /usr/share/php8.1-bcmath /usr/share/php8.1-xml /usr/share /php8.1-readline /usr/share/php8.1-zip /usr/share/php8.1-mysql /usr/share /php8.1-ldap /usr/share/php8.1-mbstring /usr/share/php8.1-common /usr/share /php8.1-intl /usr/share/php8.1-bz2 /usr/share/php8.1-gd /usr/share/php8.1- pgsql /usr/share/php8.1-curl /usr/share/php8.1-soap /usr/share/php8.1-opcache /usr/share/man/man1/php.1.gz 

Источник

How To Check PHP Version And PHP Install Path On Linux And Windows

After you install PHP or LAMP on a Linux server ( or XAMP on a Windows Server ), if you want to run command php in a terminal to execute a .php script file, you should first find the PHP install path and add the php executable file path in system environment variable PATH‘s value.

Читайте также:  Top команда в линуксе

But if there are multiple PHP version installed on the server, you should find the PHP version and related PHP install path which you need, and then you can run it accordingly. For example, you can invoke your required PHP version executable file on the Linux Cron job.

This article will tell you how to check current PHP version and PHP install path in both Linux and Windows. It will also tell you how to change current PHP version to another one by edit the system environment variable PATH‘s value.

1. Check PHP Install Path On Linux.

The whereis command returns the executable file path. From below example, we can see the PHP executable file path is /usr/bin/php , and it is linked to /www/server/php/73/bin/php file ( this is the real PHP executable file ).

$ whereis php php: /usr/bin/php $ $ ls -l /usr/bin/php lrwxrwxrwx. 1 root root 26 Dec 21 09:08 /usr/bin/php -> /www/server/php/73/bin/php

If whereis command returns multiple PHP install path, then you can run which command to get current PHP executable file path.

$ whereis php php: /usr/bin/php /usr/local/bin/php /usr/local/lib/php.ini $ $ which php /usr/local/bin/php

2. Check PHP Install Path On Windows.

It is very easy for you to check PHP install path on Windows, because install PHP on Windows is just download the PHP zip file and unzip it to a local folder, then you can run it in a dos window like below. In below example, the php install path is C:\xampp\php\.

C:\WorkSpace>C:\xampp\php\php -v PHP 8.0.0 (cli) (built: Nov 24 2020 22:02:57) ( ZTS Visual C++ 2019 x64 ) Copyright (c) The PHP Group Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies

If you want to run above example just by input command php -v, then you need to add the PHP install path ( C:\xampp\php\ ) in Windows system environment variable PATH‘s value. You can read article How To Set Windows Environment Variables.

# First make sure php install path has been added in windows environment variable PATH's value. C:\WorkSpace>echo %PATH% . ;C:\xampp\php # Now you can run command php in command console. C:\WorkSpace>php -v PHP 8.0.0 (cli) (built: Nov 24 2020 22:02:57) ( ZTS Visual C++ 2019 x64 ) Copyright (c) The PHP Group Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies

3. Check Current PHP Version.

Run php -v command in a terminal to get the current executed PHP version.

# php -v PHP 7.9.9. (cli) (built: Dec 21 2020 09:06:30) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.25, Copyright (c) 1998-2018 Zend Technologies

3. Use phpinfo() Function To Get PHP Version & PHP Install Path.

  1. The phpinfo() function can return a lot of useful information ( includes PHP Version and Install Path ) about currently used PHP.
  2. We can write a .php script file and contain the phpinfo() function in this file. Then we can execute it both in command-line or from HTTP web server.
  3. Open a terminal, run command vi test.php to create a .php script file.
  4. Press esc , i key to enter insert mode.
  5. Copy below source code into the test.php file.
$ php ./test.php phpinfo() PHP Version => 7.3.11 . .

Источник

Оцените статью
Adblock
detector