How do i download php on mac terminal?

Part two of a two part series to set up an Apache/PHP development environment on macOS

How do i download php on mac terminal?

This article is for web developers who want Apache and PHP installed as part of their development environment setup on their Mac.

Make sure to check out Part One; my article on How to Install Apache on macOS 10.13 High Sierra and 10.14 Mojave using Homebrew is a pre-requisite to this article.

In the past, I used to rely on the version of Apache and PHP that came pre-loaded with the OS on my MacBook Pro. I would then install the PHP extensions I needed (e.g. Xdebug, Redis, etc.) from Homebrew.

As of April 1, 2018, Homebrew deprecated the “Homebrew/php” tap in favor of a new “Homebrew/core” approach. In addition, although some critical extensions are still supported (e.g. MySQLi), most PHP extensions are no longer distributed through Homebrew at all (e.g. php71-xdebug, php71-redis, etc.) and require installation through PECL (PHP Extension Community Library).

With the ever changing security rules in macOS along with the complications of using the macOS pre-installed PHP version along with PECL, I’ve decided to change my whole approach and instead use a setup entirely from Homebrew. This new approach allows for a more simple installation, more flexibility for versioning and will continue to work across all macOS versions.

These instructions work the same for macOS 10.13 High Sierra and 10.14 Mojave. I’ve not tested on prior versions of macOS, but it should work similarly. Also, you don’t have to start with a fresh OS install to use this article, but these instructions start at the beginning with the assumption you have nothing installed because that’s just the easiest way to go.

Let’s get started!

Install Homebrew

If you haven’t done this already, then you’ll need to:

(Please note the following command is a single line that has wrapped due to page width constraints in Medium. Make sure to copy the entire line)

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Create PHP Log File Directories

PHP comes pre-configured with its own log directories, but we’re going to create our own “/usr/local/log/” folder so they’re easy to find when we need to. Follow these steps to create the appropriate directories and set their permissions. Disregard any “already exists” messages you might get;

$ sudo mkdir /usr/local/log$ sudo mkdir /usr/local/log/php$ sudo chgrp -R staff /usr/local/log/php$ sudo chmod -R ug+w /usr/local/log/php/

Install and Configure PHP

Up until the end of March 2018, all PHP related brews were handled by Homebrew/php tap, but that has been deprecated. So now we use what’s available in the Homebrew/core package. This should be better maintained, but is a much less complete set of packages.

$ brew install 

It should be noted that to install other versions of PHP you can simply change the version number (e.g. ). You also have the ability to have multiple versions of PHP installed at the same time and switch between them. However this document is specifically designed for installing and configuring PHP 7.1.

Your new php.ini file is located in the following directory:

/usr/local/etc/php/7.1/php.ini

Enable the Necessary Apache Module

Now we need to configure Apache to use the newly installed PHP module. To do this we’ll need to edit the httpd.conf file we installed in Part One of this series:

$ open -e /usr/local/etc/httpd/httpd.conf

Scroll to the bottom of the LoadModule entries and add the following line after the mod_rewrite module line:

(Please note the following command is a single line that has wrapped due to page width constraints in Medium. Make sure to copy the entire line)

LoadModule php7_module /usr/local/opt//lib/httpd/modules/libphp7.so

Set Directory Index and File Handlers

You also need to set the Directory Indexes for PHP explicitly, so search for the following block of text:

<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

… and then select and replace it with the following lines:

<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

Save and exit the text editor.

To start the PHP service and make sure it starts upon reboot, enter the following command in Terminal:

$ brew services start 

Validate the PHP Install

To test if PHP is installed and running as expected, create a file called index.php in the Sites folder you created in Part One of this two part series:

$ echo "<?php phpinfo();" > ~/Sites/index.php

Restart Apache:

$ sudo apachectl -k restart

Now do a hard refresh on your browser (or close and exit your browser and start it back up again). Then browse to your local host and you should see a PHP info page similar to the one below:

How do i download php on mac terminal?

If you see a similar PHP info page, then congratulations! You now have Apache and PHP running successfully.

If you have a problem and it doesn’t appear that PHP is loading properly, it’s possible your browser session is preventing it from refreshing. Try shutting down and quitting your browser completely. Run the apache reset again and open your browser and try navigating to your localhost. It should work.

Install PECL Extensions

As of March 2018, Homebrew no longer distributes most PHP extensions and they must be installed using PECL. This includes the xdebug, igbinary and redis extensions.

The good news is that PECL is included in your PHP install and all you need to do is add its bin location to your system PATH to make it start working from your command line.

** Note: If you’re installing PHP 7.2, then you can skip this section as PECL has already been added to your system PATH.

** Note: Before running the command below, it would be wise to cd into your /usr/local/Cellar// folder and verify the version number that’s listed in the command (e.g. “7.1.22”) it could be something different.

export PATH=$PATH:/usr/local/Cellar//7.1.22/bin/

This will add your PHP directory to your system path and restart bash. You should be good to go!

Open Terminal and enter the command below. You should see a list of options instead of “command not found”. If you see a list of options, you’re good go.

$ pecl

Install XDebug

This will install XDebug v2.6.0 which is the current stable release (as of this writing) that supports PHP 7.1.

There are other versions of XDebug. You can check them out here and just substitute the version number you desire.

$ pecl install xdebug-2.6.0

This will add a line to the top of your php.ini file that we will fix after we install the remaining extensions.

Install Igbinary

This will install Igbinary v2.0.6 which is the current stable release (as of this writing) and supports PHP 7.1.

There are other versions of Igbinary. You can check them out here and just substitute the version number you desire.

**Note: You must install the Igbinary extension before you install Redis.

$ pecl install igbinary-2.0.6

This will add a line to the top of your php.ini file that we will fix after we install Redis and its extension.

Install Redis

First install the Redis server component for our dev environment.

$ brew install redis

Once that’s complete start Redis and set it to restart on reboot.

$ brew services start redis

Install the Redis PHP Extension

This will install Redis v4.0.2 which is the current stable release (as of this writing) and supports PHP 7.1.

There are other versions of Redis. You can check them out here and just substitute the version number you desire.

**Note: You must install the Igbinary extension before you install Redis.

$ pecl install redis-4.0.2

You will be asked some questions during install. Answer them as follows:

  • enable igbinary serializer support? [no]: yes
  • enable lzf compression support? [no]: no

Edit PHP Configuration Settings

Now that we’ve got all the necessary extensions installed, we can make some final entries to the php.ini file for our development environment.

$ open -e /usr/local/etc/php/7.1/php.ini

Find and remove these three lines that were added to the very top of the php.ini file in the prior steps. We’ll re-add them in a bit:

extension="redis.so"

extension="igbinary.so"

zend_extension="xdebug.so"

Scroll to the bottom of the php.ini file and paste the following lines in:

; CUSTOM variables
short_open_tag = Off
expose_php = Off

error_reporting = E_ALL & ~E_STRICT
display_errors = On
error_log = "/usr/local/log/php/php_errors.log"

upload_tmp_dir = "/tmp/"
allow_url_fopen = on

[xdebug]
zend_extension="xdebug.so"
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_host=localhost
xdebug.remote_handler=dbgp
xdebug.remote_port=9000

[redis]
extension="igbinary.so"
extension="redis.so"
session.save_handler = "redis"
session.save_path = "tcp://127.0.0.1:6379?weight=1&timeout=2.5"

Save and exit the text editor.

Restart Apache:

$ sudo apachectl -k restart

Open your browser and navigate to your http://localhost and you should see your PHP info page. Scroll down through and you should see all the necessary PHP extension information displayed for igbinary, Redis and XDebug with the proper version numbers you installed in the prior steps.

Congratulations! You now have Apache and PHP with associated extensions running on your Mac development environment.

How do I install PHP on my Mac?

Install PHP on macOS Monterey.
brew --version..
brew install php..
command -v php..
/usr/local/bin/php..
/opt/homebrew/bin/php..
#!/usr/bin/env php..
b) You can include inline scripts in expansions. A simple inline script in PHP would be: {/PHP. echo "Hello"; }.

Can you download PHP on Mac?

Installation on macOS ¶ PHP is bundled with macOS since macOS X (10.0. 0) prior to macOS Monterey (12.0. 0). Compiling is similar to the Unix installation guide.

How do I manually download PHP?

Step 1: Visit https://www.php.net/ website using any web browser and click on Downloads..
Step 2: Click on the Windows “Downloads” button. ... .
Step 7: Now paste the copy folder in your windows drive in the Program files folder. ... .
Step 12: Now go to the “System variables” Path option and double click on Path..

How do I know if PHP is installed on Mac?

2 Answers.
Go to File > Preferences > User Settings > Settings.json..
Change the value of php. validate. executablePath according to the installed directory of php7. "php.validate.executablePath": "/Applications/MAMP/bin/php/php7.0.14/bin/php".
Relaunch VM Code..