Where is php executable linux?

I have installed Apache and PHP. I know PHP works as I have tested a simple PHP file on an Apache server.

I'm writing a simple webserver which should be able to process PHP files. So once I get a request for a PHP file, I want to do something like 'exec php test.php' and get the output and pass it to the client.

As I'm not much into Ubuntu, I don't know where the PHP executable is (should be in \bin right?) to do it. But there is no PHP file inside \bin or \usr\bin.

When I run 'which php' it shows nothing. How do I do this?

The default executable can be found with:

which php

which, on my system, results in /etc/bin/php. However, if I list this file, it turns out that it is a symbolic link to /etc/alternatives/php:

$ ls -l /usr/bin/php
lrwxrwxrwx 1 root root 21 mei 10  2016 /usr/bin/php -> /etc/alternatives/php

This, in turn, is (on my system) a symbolic link to /etc/bin/php7.2:

$ ls -l /etc/alternatives/php
lrwxrwxrwx 1 root root 15 jun 20  2018 /etc/alternatives/php -> /usr/bin/php7.2

And this is the actual executable:

$ file /usr/bin/php7.2
/usr/bin/php7.2: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=1b669fd85cfcca25b9733cacf7e7ebaafc0ce17e, stripped

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.

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.
  6. Press esc, :wq! , return key to save the edited file and exit.
  7. Run php ./test.php in command line. You can get below output messages.
    $ php ./test.php
    phpinfo()
    PHP Version => 7.3.11
    ......
    ......

Reference

  1. phpinfo

Where is the PHP executable in Linux?

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 ). If whereis command returns multiple PHP install path, then you can run which command to get current PHP executable file path.

Where is my PHP installed?

On Windows the default path for the php. ini file is the Windows directory. If you're using the Apache webserver, php. ini is first searched in the Apaches install directory, e.g. c:\program files\apache group\apache .

Where is PHP file in Ubuntu?

The default location for the php. ini file is: Ubuntu 16.04: /etc/php/7.0/apache2.

How do I open a php file in Linux browser?

You just follow the steps to run PHP program using command line..
Open terminal or command line window..
Goto the specified folder or directory where php files are present..
Then we can run php code using the following command: php file_name.php..