Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get PHP working again in the command line?

I'm completely at loss here and am about to wipe my hard drive clean and start from a fresh OS install. I've been trying for two days to create a new yii app in the terminal and have finally figured out that the terminal or command line can not even execute PHP all of a sudden. I had no problem in past creating an executing php from the command line, But now it's not working. When I type which php i get nothing. When I type php -v I get:

 -bash: php: command not found.

And when I try to create a new yii application I get:

env: php: No such file or directory 

I am using mac osx-lion and my path looks like this at the moment:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin

I've tried looking through the php manual and I'm getting nowhere. How can I reconfigure the command line to execute php? Any help is greatly appreciated.

like image 545
MikeTheCoder Avatar asked May 11 '12 19:05

MikeTheCoder


People also ask

How do I know if PHP is installed CMD?

1. Type the following command, replacing [location] with the path to your PHP installation. 2. Typing php -v now shows the PHP version installed on your Windows system.

How do you fix PHP is not recognized as an internal or external command?

If you are getting “php is not recognized as an internal or external command” from your Microsoft Windows Command Prompt on Windows 7, Windows 8, and Windows 10. Then you need to add php folder location path to the system environment variables.

What is PHP CLI command?

PHP's Command Line Interface (CLI) allows you to execute PHP scripts when logged in to your server through SSH. ServerPilot installs multiple versions of PHP on your server so there are multiple PHP executables available to run.


4 Answers

Hopefully this will save someone a lot of headache. If, for whatever reason, you are unable to locate php in your command line, and unable to execute php from the command line, below is a list of steps to get PHP up and running again.

  1. double check to make sure PHP is no where to be found by opening your terminal, and typing find /usr -name php and hit enter. The main thing you want to look for here is a path with /bin/php at the end. In my case it's, now that I've installed it, it's /usr/local/php5-20120508-102213/bin/php. If you don't see anything like that then go to the next step. If you see something like that then make note of that path with the /bin/php at the end, and go to step 4.

  2. Go to the terminal and type in curl -s http://php-osx.liip.ch/install.sh | bash -s 5.4, hit enter. It will ask for your password. Your installing a php package. After you enter your password just follow the steps like any other download. For more information on that download you can visit the binary package website.

  3. After you've installed php, open the terminal and type in find /usr -name php and hit enter. You should see a few lines of paths. Make note of the one that has /bin/php at the end of the path. You will be needing that path for the next step.

  4. Next, open a text editor, I used TextWrangler for this purpose, go to file up in on the menu bar, and select Open file by name.Then type in ~/.bash_profile. Select Open and at the end of the .bash_profile file type in

    PATH=$PATH:/usr/local/php5-20120508-102213/bin/
    export PATH
    

    the /usr/local/php5-20120508-102213/bin/ part of that is the path that I mentioned to make note of, minus the php at the end. If your path was different, substitute it. Just remember to leave off the php at the end. save and exit.

  5. Last step, open the terminal and type in php -v. Hit enter. You should see something like:

    PHP 5.4.2 (cli) (built: May  8 2012 09:48:57) 
    Copyright (c) 1997-2012 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.0rc2, Copyright (c) 2002-2012, by Derick Rethans
    

    if you're seeing that then everything is working.

NOTE: Here is a good resource for working with Command line PHP - located about 1/3 of the way down the page.

like image 183
MikeTheCoder Avatar answered Oct 28 '22 19:10

MikeTheCoder


There is one of two things going on here, either you didn't install PHP, or PHP is installed and not currently in any of system aware paths. First I would try to find PHP and make sure it exists:

$ find / -name php -type f

You should see something like:

/path/to/your/php/bin/php

If PHP binary does exist, check your path:

$ echo $PATH

If it does not exist, recompile php.

If PHP exists on your system, make sure the path to the /bin/php file is included. You can edit your ~/.bash_profile and add the custom path like this:

PATH=$PATH:/path/to/your/php/bin/
....  
export PATH

Then save and reload the file to ensure your current session has access to new path changes:

$ source ~/.bash_profile

With any luck you can now do a php -v and see the version response.

-- Update --

Setting actual path:

$ vi ~/.bash_profile

...
# Add your custom php path
PATH=$PATH:/bitnami/mampstack-osx-x86/output/php/bin/
....  
export PATH

Save and close, then source it:

$ source ~/.bash_profile

And now you should be able to run PHP from cli:

$ php -v
like image 42
Mike Purcell Avatar answered Oct 28 '22 17:10

Mike Purcell


In response to @MikeTheCoder, the posted export path syntax didn't work for me, but the slightly modified one following did:

export PATH=/usr/local/php5-5.6.27-20161101-100213/bin/:$PATH

I'm using El Capitan 10.11.6 which defaults to an earlier PHP version.

like image 45
Ali Schlereth Avatar answered Oct 28 '22 19:10

Ali Schlereth


(On macOS Mojave 10.14.6)

A shortcut for the mentioned 'source' command above is just the period. So you can do the following:

vi ~/.bash_profile

Add in the lines

export PATH="/usr/local/opt/[email protected]/bin:$PATH"
export PATH="/usr/local/opt/[email protected]/sbin:$PATH"

Hit colon W :w to write and colon Q to :q to quit VIM editor (bonus learning)

And now you "apply" it by entering into the terminal: . ~/.bash_profile

You can doublecheck the new PHP version now with php -v or which php

like image 27
Grant Avatar answered Oct 28 '22 17:10

Grant