Install php-cgi on Mac OS X Yosemite - php

Is it possible to install php-cgi on Mac OS X Yosemite keeping the standard PHP version provided by the OS?
Thanks,
Alberto

You can install PHP easily with Homebrew:
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
brew install php54
Homebrew doesn't change the version of the system PHP. System and Homebrew versions are located in different directories. This is how it looks like for me:
System /usr/bin/php:
/usr/bin/php -v
PHP 5.4.30 (cli) (built: Jul 29 2014 23:43:29)
Homebrew /usr/local/bin/php:
/usr/local/bin/php -v
PHP 5.6.2 (cli) (built: Nov 14 2014 10:30:28)

If you wish to use PHP CGI on Mac OS X you will need PHP build with the CGI enabled. By default, PHP version 4.3 and higher is built with "-enable-cli" option so default interface is CLI (Command Line Interface). You can check this by running "php -v" from the shell. Output will probably be something like this:
PHP 4.4.4 (cli) ....
CLI does not output any headers and output is plain text by default. More details about this is available here.
In order to use PHP with the wxWebServer (or any other CGI application), you will need to build your own PHP binaries with CGI SAPI. It's simple, it takes only 15 minutes and you will be proud of yourself for having a "personal" build of the PHP. Here is the procedure, step by step:
Download the complete source code for the latest PHP version from php.net. Instructions here are for PHP 5.2.3 but should work for PHP 4 as well. I tested with 4.4.7 too.
Unpack downloaded archive somewhere, for example ~/Temp/php-5.2.3
Open terminal window (shell) and go to folder where you unpacked files:
cd ~/Temp/php-5.2.3
Decide where you want to install this custom PHP build and create folder. This document will assume it's custom folder under your home folder, for example if user name is webdev it's /Users/webdev/custom/php5
Create that folder (in Finder or in Terminal with "mkdir ~/custom; mkdir ~/custom/php5")
Go back to that Terminal window, you should still be in ~/Temp/php-5.2.3.
Run ./configure with CLI disabled if you are planning to use this private PHP build only for CGI. Modify paths and options to reflect your preferences.
You don't need option "--disable-cli", you can replace it with "--enable-cli" if you plan to use this PHP for something more. You will get both binaries but you will need to use php-cgi in wxWebServer.
Here is a full command line for .configure that should produce PHP with all common options and CLI disabled:
./configure --prefix=/Users/webdev/custom/php5 --disable-cli --enable-cgi --enable-trans-sid
--enable-mbstring --with-xml --enable-exif --enable-mbregex --enable-dbx --enable-sockets
--with-iodbc=/usr --with-curl=/usr --with-config-file-path=/etc --sysconfdir=/private/etc
--with-mysql=/usr/local/mysql --without-pear
When configure is completed, run
make install
Edit your profile (~/.profile) to add new php path before existing one. Add something like this to the end of your .profile file:
PATH=~/custom/php5/bin:$PATH
export PATH
If you don't know how to edit profile from the command line, just create a plain text file in TextEdit containing those 2 lines and save it in your home folder (~/Users/webdev) as newpath.txt. Then open Terminal and enter command:
cat newpath.txt >> .profile
The easiest way for changes to take effect is to log out and log back in (or you can use source command).
Congratulations, you now have a custom php installation! If you wish to use other PHP just comment your changes to .profile
Verify your new PHP is the default one:
# which php-cgi
/Users/webdev/custom/php5/bin/php-cgi
php-cgi -v
PHP 5.2.3 (cgi) (built: Jul 11 2007 00:48:59)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
If you wish, you may now delete the source files in ~/Temp/php-5.2.3. To remove you custom PHP just delete target folder, i.e.
"/Users/webdev/custom/php4".
Once you have custom php installation, configure wxWebServer to use new php binaries. Regardless if you enabled or disabled CLI, you can use php-cgi (output of "which php-cgi", in this case
"/Users/webdev/custom/php5/bin/php-cgi").
If you disabled CLI build, you may also use php binary as it will be CGI build. That's it, enjoy your personal PHP build!
Found It Here

Related

How to make the right PHP version on my terminal? [duplicate]

I have a problem with PHP which is call through bash command "php":
In bash:
php -v # PHP 5.3.17
/usr/bin/php # PHP 5.3.15
I want secondary PHP version 5.3.15 as mainly, because in this version I have correctly installed extensions etc.
How can I set /usr/bin/php as main -> I want when call "php -v" return my 5.3.15 version.
Execuse my bad English, I hope that you understand.
Thank you so much.
I have Mac OSx Lion
Another way without changing the original php file / link is if you edit your .bashrc file as follows (works with other commands as well):
Open your Terminal / Commandline and type in:
vim ~/.bashrc
Then press "i" to get into edit-mode (if there shows up a problem before that, because of a swap file, then press "E" to edit the file anyway)
Add the following line (change the path as needed):
alias php="/Applications/MAMP/bin/php/php5.4.10/bin/php"
Press "ESC" and type in ":wq"
If you need the php-alias to work right out of your current shell-session, then you have to use the following command (that's optional, so only if u r continuing to work in your current shell):
source ~/.bashrc
That's it, no change in the /usr/bin folder needed, and u can export your bashrc file / settings to any other linux / bsd like system if needed without even touching your OS /usr/bin files. You can also add new aliases for different php versions, aliases like "php52", "php53", "php54", etc. for testing purposes or whatsoever.
Have fun,
ioCron
OS: OSX Yosemite
Just sort out a similar issue by vim ~/.bash_profile
export PATH=/usr/local/bin:$PATH
export PATH=/Applications/MAMP/bin/php/php5.5.10/bin:$PATH
Make sure the order is correct. Put export PATH=/usr/local/bin:$PATH before export PATH=/Applications/MAMP/bin/php/php5.5.10/bin:$PATH
/usr/local/bin contains the mac default php.
Hope this helps.
For brew users, you might not need php55 if you already have 54 - but regardless you should probably run these commands
$ brew install php54 php54-xdebug php54-mcrypt
you can substitute 54 with the latest number if you want. last i checked it was on 56
then based on some of the comments above you should run:
$ php --version
it will probably display:
PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
if it does, then change your .bash_profile like some of the other commenters
my brew instance (i think its default) lives in
/usr/local/Cellar
/usr/local/bin
as things get installed using brew, they get put in Cellar and linked through the bin. but in this case we need to override the php
$ sudo nano ~/.bash_profile
mine kinda looks like:
PATH=$PATH:~/bin
PATH=$PATH:/usr/local/bin
PATH=$PATH:/usr/local/sbin
PATH=$PATH:/usr/local/Cellar/r/2.15.1/R.framework/Versions/Current/Resources/li$
export PATH
which means that it already has access to the /usr/local/bin - but i use the alias to specify
by adding this line, near the bottom:
alias php="/usr/local/bin/php"
save and close the terminal window, open up another one, and it should show something like:
$ php --version
PHP 5.4.27 (cli) (built: Apr 24 2014 17:16:35)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.4, Copyright (c) 2002-2014, by Derick Rethans
You should look up the path and check all directories in the path for a symlink to the wrong php version.
Then you can remove that symlink and replace it with a link to the correct version.
You can also use find to see all instances:
$ sudo find / -name "php"
I would recommend you to install php through "Mac Ports".
You have multiple versions of php installed.
With the cli command "which" you can:
DESCRIPTION
The which utility takes a list of command names and searches the path for each executable file
that would be run had these commands actually been invoked.
So read the man pages of the cli command "which".
DO this from your cli:
which php
and then you will see where is located your php executable. What i would recommend you is to replace your php executable for a symbolic link to your /usr/bin/php.

Where does homebrew install PHP on Mac High Sierra?

I'm using Mac High Sierra. I installed PHP (with GMP) using
brew install php72-gmp
Everything installed successfully, per the messages given, but it would seem I still have an old version of PHP 7.1 somewhere on my machine because when I run
localhost:php-7.2.1 davea$ which php
/usr/bin/php
localhost:php-7.2.1 davea$ /usr/bin/php -v
PHP 7.1.7 (cli) (built: Jul 15 2017 18:08:09) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
So I'd like to figure out where to put the homebrew version of PHP ahead in my PATH so that that version gets picked up first.
EDIT:
As of 2018 and the latest updates to Brew your php formula names have the following format php#7.2.
When you run brew info php#7.2 your path should be /usr/local/Cellar/php#7.2/
And there will be a symbolic link to /usr/local/bin/opt/php#7.2/
So in order to change your php installation you just need to run this in terminal:
echo 'export PATH="/usr/local/opt/php#7.2/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/php#7.2/sbin:$PATH"' >> ~/.bash_profile
or put it manually in your .bash_profile like this:
export PATH="/usr/local/opt/php#7.2/bin:$PATH"
export PATH="/usr/local/opt/php#7.2/sbin:$PATH"
===========================================================================
The location of your Homebrew installed php should be /usr/local/Cellar/php72
These are the steps you need to do to setup your PHP on macOS:
After you install php with brew run this in terminal:
export PATH="$(brew --prefix homebrew/php/php72)/bin:$PATH"
Then run:
source ~/.bash_profile
To check your current active version of php run this in terminal:
which php
EDIT:
run brew info php72 in console to get all info, hints and caveats for php, it is really useful, for example this comes from brew info:
✩✩✩✩ PHP CLI ✩✩✩✩
If you wish to swap the PHP you use on the command line, you should add the
following to ~/.bashrc, ~/.zshrc, ~/.profile or your shell's equivalent
configuration file:
export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"
Check if you have two php directories in '/usr/bin/' or '/usr/local/bin/'. If yes do a link to the good directory. See this link, may be it helps you.
As you are familiar with linux, homebrew has its own /opt and /etc path
# > echo $(brew --prefix)
# /opt/homebrew
so the paths are (for Apple Silicon):
/opt/homebrew/opt
/opt/homebrew/etc
in linux, php is installed in /etc/php/7.4
in mac homebrew, php is installed in /opt/homebrew/etc/php/7.4.
Please find the homebrew document on the default installation path for Apple Silicon 1
ref 2

OS X Mavericks how to upgrade PHP 5.4 to 5.5+ latest?

I would tring to update my php version to the version 5.5+ latest
When I'm looking via /usr/local/bin/php -v in the console I see the version 5.5+ and with php -v it tell me there is the version 5.4 installed.
How can I remove the older version 5.4 and install latest 5.5+ latest?
Typically I've used the PHP package from liip.ch
From terminal, run:
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.5
It will not remove the default installation of PHP 5.4 from your system, but it does not have any impact on using PHP 5.5. They address the issue with php -v in their FAQ.
Prerequisites
Xcode and it's Command line utilities (install form preferences) will give you a gcc compiler to build the php with libjpeg, libpng etc. if you want to build with these.
OR run this command on terminal to install Command line tools
xcode-select --install
Building and installing php:
Download the distribution from www.php.net/downloads.php
Untar the archive and point your console into the location
Now, having still the original old php installed, run php -i | head in the console. What you want is to configure your new php with these flags to ensure you have proper pcre support and everything works just fine.
In the php source directory you untarred, run the ./configure script with the flags you just got
Build php by running make and sudo make install afterwards. That will actually replace the original php distributed with OS X.
Example sample run terminal commands:
Download latest version from http://php.net and after ruin below commands
tar -zxvf php_scource.tar.gz
cd php_source/
php -i | head
./configure --prefix=/usr --with-snmp # ... same flags as previous build
make
sudo make install
end of the workflow for building php and just check latest version from terminal commands
$ php -v
sample output of php -v
PHP 5.5.10 (cli) (built: Mar 27 2014 16:50:31)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
OSX Mavericks runs PHP from /usr/bin/php so in order to run the new version that you want from /usr/local/bin/php, you need to copy this file over to the /usr/bin directory. If you want to save your old version of php, first go to the /usr/bin directory
cd /usr/bin
and rename your old php file
sudo mv php php.old
Then go to the /usr/bin/local directory:
cd /usr/local/bin
and then copy the php file over to /usr/bin using sudo:
sudo cp php /usr/bin
OSX Mavericks has /usr/bin first in its environment path, so the php in /usr/bin is found before it gets to the php in /usr/local/bin. Once you copy your new php file over, when you run
php -v
Which will display something like:
PHP 5.6.23 (cli) (built: Jun 24 2016 21:08:07)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
It will have your new version that was installed at /usr/local/bin/php that you copied over to /usr/bin/php instead of version 5.4. If you're running Apache and want your webserver to also use the new php version, you'll also need to update the httpd.conf file to point at the new php_module, but that's a different question.

Upgrade to PHP 5.4 on MAC and remove version 5.3.10

I would tring to update my php version to the version 5.4.
When I'm looking via /usr/local/bin/php -v in the console I see the version 5.4.3 and with php -v it tell me there is the version 5.3.10 installed.
How can I remove the older version?
Update:
As of early 2014, you are probably better of installing from http://php-osx.liip.ch/ builds as it contains most extensions. In case you really do need to compile your own, follow the original answer.
Original answer:
Mac comes with a preinstalled php you can easily overwrite.
This is a list of steps I take to update the php I have installed:
Prerequisites:
Xcode and it's Command line utilities (install form preferences) will give you a gcc compiler to build the php with
libjpeg, libpng etc. if you want to build with these.
Building and installing php:
Download the distribution from www.php.net/downloads.php
Untar the archive and point your console into the location
Now, having still the original old php installed, run php -i | head in the console. What you want is to configure your new php with these flags to ensure you have proper pcre support and everything works just fine.
In the php source directory you untarred, run the ./configure script with the flags you just got
Build php by running make and sudo make install afterwards. That will actually replace the original php distributed with OS X.
And dont forget to run make test...
Ex.: my workflow for building php
wget http://url_to/php_source.tar.gz
tar -zxvf php_source.tar.gz
cd php_source/
php -i | head
./configure --prefix=/usr --with-snmp # ... same flags as previous build
make
sudo make install

How do I upgrade PHP in Mac OS X?

I feel this is an awfully obtuse question to ask, but strangely, this problem is poorly documented.
I would like to upgrade PHP, but there are several problems:
There is no built-in package manager. MacPorts doesn't recognize php as an installed package because it didn't install PHP itself.
Running locate php indicates there are probably many dependencies.
I don't know HOW php was installed, as it was included with the OS, so I don't know whether I should install from source or download binaries. I also don't know the proper way to uninstall the previous version without breaking dependencies.
I am running on Leopard. I have a feeling Apple doesn't want you to upgrade. Would buying Snow Leopard and upgrade solve this problem (and future ones like it)?
You may want to check out Marc Liyanage's PHP package. It comes in a nice Mac OS X installer package that you can double-click. He keeps it pretty up to date.
http://php-osx.liip.ch/
Also, although upgrading to Snow Leopard won't help you do PHP updates in the future, it will probably give you a newer version of PHP. I'm running OS X 10.6.2 and it has PHP 5.3.0.
I use this: https://github.com/Homebrew/homebrew-php
The command is:
$ xcode-select --install
$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php
$ brew options php56
$ brew install php56
Then config in your .bash_profile or .bashrc
# Homebrew PHP CLI
export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"
I think one simple way to do it, is:
1 - Check you where is your current PHP:
$ which php
$ /usr/local/bin/php
You see? Usually, our commands that we run is a link in /usr/local/bin so...
2 - Unlink this current link of PHP
unlink /usr/local/bin/php
If you prefere, before unlink it, check the path and then remove php files (do ls -al /usr/local/bin | grep php and then rm -rf into desired path)
3 - Install PHP 7.1
curl -s http://php-osx.liip.ch/install.sh | bash -s 7.1
4 - Create new link (using php 7.1 bin that you have installed)
ln /usr/local/php5-7.1.9-20170914-100859/bin/php /usr/local/bin/php
Like I said, its a simple way I think.
There is no built-in package manager. MacPorts doesn't recognize php as an installed package because it didn't install PHP itself.
You could still install it with MacPorts. sudo port install php52 (or whichever version you want) will install PHP.
It won't overwrite the Apple-supplied version. It'll install it under /opt/local. You can add /opt/local to the beginning of your $PATH, and use the MacPorts version in your Apache config.
Option #1
As recommended here, this site provides a convenient, up-to-date one liner.
This doesn't overwrite the base version of PHP on your system, but instead installs it cleanly in /usr/local/php5.
Option #2
My preferred method is to just install via Homebrew.
Before I go on, I have the latest version (v5.0.15) of OS X Server (yes, horrible, I know...however, the web server seems to work A-OK). I searched high and low for days trying to update (or at least get Apache to point to) a new version of PHP. My mcrypt did not work, along with other extensions and I installed and reinstalled PHP countless times from http://php-osx.liip.ch/ and other tutorials until I finally noticed a tid-bit of information written in a comment in one of the many different .conf files OS X Server keeps which was that OS X Server loads it's own custom .conf file before it loads the Apache httpd.conf (located at /etc/apache2/httpd.conf). The server file is located:
/Library/Server/Web/Config/apache2/httpd_server_app.conf
When you open this file, you have to comment out this line like so:
#LoadModule php5_module libexec/apache2/libphp5.so
Then add in the correct path (which should already be installed if you have installed via the http://php-osx.liip.ch/ link):
LoadModule php5_module /usr/local/php5/libphp5.so
After this modification, my PHP finally loaded the correct PHP installation. That being said, if things go wonky, it may be because OS X is made to work off the native installation of PHP at the time of OS X installation. To revert, just undo the change above.
Anyway, hopefully this is helpful for anyone else spending countless hours on this.
Upgrading to Snow Leopard won't solve the your primary problem of keeping PHP up to date. Apple doesn't always keep the third party software that it bundles up to date with OS updates. And relying on Apple to get you the bug fix / security update you need is asking for trouble.
Additionally, I would recommend installing through MacPorts (and doing the config necessary to use it instead of Apple's PHP) rather than try to upgrade the Apple supplied PHP in place. Anything you do to /usr/bin risks being overwritten by some future Apple update.
Saving on keystrokes, this worked on MacOS Sierra:
$ brew install homebrew/php/php71
$ /usr/local/opt/php71/bin/php -v
PHP 7.1.4 (cli) (built: Apr 14 2017 15:02:16) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
Check your current php version in terminal with the following command,
$ php -v
You see current php version in terminal, and next command run in terminal if you want to upgrade your php version with php concat with version liked as,
$ brew install homebrew/php/php71
Please restart terminal if you finished php version upgrade installed and run the command.
$ php -v
Now you see the current php version in terminal....thank
Use this Command:
curl -s http://php-osx.liip.ch/install.sh | bash -s 7.0
You can use curl to update php version.
curl -s http://php-osx.liip.ch/install.sh | bash -s 7.3
Last Step:
export PATH=/usr/local/php5/bin:$PATH
Check the upgraded version
php -v
best way to upgrade is
compile it from source
see this tutorial that may be helful for you
http://www.computersnyou.com/2012/09/how-to-upgrade-php-in-mac-osx-compiling.html
to upgrade php7 to latest stable version brew upgrade php7
or for php5.X to latest stable version
brew upgrade php56
use brew list to check installed version

Categories