There are two ways to build PHP. First way is the simplest and no-mess way, using Homebrew – or any equivalent package manager depending on your system. If you are not already familiar with package managers, witI strongly suggest you to be. They make lives easier for sure.
The second way is the slow & messy way; building it from scratch without any package manager. It may be slow and messy but actually, it gives you more options while building. This is my way of installing PHP on my servers, but on my personal computer; since I don’t have any deep specific requirements – and for simplicity too – I’ll be using and talking about the Homebrew way. I’ll publish a tutorial about this too, but now let’s start.
First, update the Homebrew and get the required formulas – formulas are just packages, like those in aptitude. If you don’t have Homebrew yet, visit this page for installation and detailed usage: http://brew.sh
brew update && brew tap homebrew/php
See which versions of PHP we can have.
brew search php
There will be a long list. If you see something like “homebrew/php/php70” then it’s OK. Now let’s look at the options we have.
brew options php70
You can use any of these options while installing PHP, but do not forget to include –with-thread-safety, it is essential for threading extensions. Now start the installation
brew install php70 --with-thread-safety --with-homebrew-curl
After some time – brace yourself, it might take a little long – depending on your internet and Mac’s speed, installation finishes.
Now install some extensions. My recommendations are here but you can find all the extensions by “brew search php70-”
brew install php70-opcache php70-mcrypt php70-pthreads php70-xdebug --build-from-source
brew install php70-memcached --build-from-source --HEAD
Installation is done, let’s continue with configuration.
I start by adding PHP support in Apache. First you have to find the path to your httpd.conf file: (mostly it’ll be /private/etc/apache2/httpd.conf)
httpd -V | grep SERVER_CONFIG_FILE
Open the file with a text editor, also with privileges otherwise you can’t make changes – use sudo for example.
sudo nano PATH-TO-YOUR-CONFIG
Add these at the end of the file
LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so
Then find this line and change
DirectoryIndex index.php index.html
You can close the config file. Now let’s set up the php.ini. First make a link to the new php.ini file
sudo mv /etc/php.ini /etc/php.ini_old
sudo ln -s /usr/local/etc/php/7.0/php.ini /etc/php.ini
/etc is the path to my .ini file. You can see yours by entering php –ini. Then this needs some configuration too.
Find “;date.timezone =“ and set your timezone. Mine is date.timezone = “Europe/Istanbul”. Find ;opcache.enable , ;opcache.enable_cli and set them both to 1, also delete the ;(semicolon) prefixes. For detailed opcache settings, visit this page: https://www.scalingphpbook.com/best-zend-opcache-settings-tuning-config/ There are tons of options in the php.ini and httpd.conf, I can’t include them all here but I suggest you to make some research and find the best options suit your case.
After these, it should be all set. Open a new terminal window, fire up your server and check the PHP version. You can also see your installed extensions by php -m.
sudo httpd -k restart
My next post will be about using pThreads. If you have questions or recommendations about PHP 7 , please let me know.