Setting Up PHP 7.1 macOS High Sierra


By default, macOS High Sierra comes with PHP 7.1 installed and Apache Apache/2.4.27 (Unix). But to get it to show up, we have to edit a few files.

There are also 3 different ways to do this, and after doing all three, I prefer the Homebrew method.

#1. Default Mac Method

First, let's start our Apache Server.

sudo apachectl start

Editing httpd.conf

Head to your apache2 httpd config path.

sudo vim /etc/apache2/httpd.conf

Then you will need to uncomment these two file extensions, which are for some reason not uncommented upon installing.

#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
#LoadModule php7_module libexec/apache2/libphp7.so
#LoadModule proxy_module libexec/apache2/mod_proxy.so
#LoadModule proxy_fcgi_module libexec/apache2/mod_proxy_fcgi.so

Then restart Apache.

sudo apachectl restart

Once you are finished with that cd into your documents folder. And then create your php info file. If everything shows up as it should be in the GUI page for it, then you are all set!

cd /Library/WebServer/Documents/
touch phpinfo.php && echo "<?php phpinfo();?>" >> phpinfo.php

NOTE: You may have to give yourself permissions to edit in this folder. What I did was just right clicked on the documents folder, clicked get info, then added my current user to the list of permissions at the bottom on the pop up window (you have to click the lock icon to unlock it and add additional users).

Installing MySQL

To install MySQL, simple use the following command.

brew install mysql

Adding Root User

I already had this setup with my current Ruby setup. Please see my MySQL user guide for changing roots password and adding additional users.

Moving Files To WebServer/Documents

I ran into permission issues while transferring my project to this folder. I overcame it by setting the right permissions.

sudo chmod -R 755 FOLDER

MySQL Connection issues

Before I had the connection as localhost instead of 127.0.0.1. Depending on which way you install MySQL, it could be looking for the sockets in the wrong location. I fixed the following error I show below with the following code.

# Error => Warning: mysqli_connect(): (HY000/2002): No such file or directory

$link = mysqli_connect("127.0.0.1", "user", "password", "db")

#2 Alternative One Liner PHP 7.1 Installation

This is an alternative to the above steps. If you don't have any php on your machine, you can also do a one liner by using the following command.

curl -s https://php-osx.liip.ch/install.sh | bash -s 7.1

System Shows Old Version

If your php -v in your console still shows the old version, try doing the following quick fix. Php-osx doesn't overwrite the php binaries installed by Apple, but installs everything in /usr/local/php5. The new php binary is therefore in /usr/local/php5/bin/php. We will have to adjust the path it looks for in our .profile. Open it up in your main directory as follows.

vim ~/.profile

Then paste the following command in it to tell your profile to look for the new php version here.

export PATH=/usr/local/php5/bin:$PATH

#3 Via Homebrew

Use 'brew doctor' after installing all of this to get feedback on issues. Original script is here, but I had to make adjustments in order to get what I needed accomplished.

# 1. Install brew  --> http://brew.sh/
# 2. run the following commands in your Terminal
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php

brew services start homebrew/php/php71

# Stop Macs Apache and unload it
sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
brew install httpd
sudo brew services start httpd

# brew unlink *any previous php version*
brew install --with-openssl curl
brew install --with-homebrew-curl --with-apache php71
brew install php71-mcrypt php71-imagick

# 3. Follow these instructions to make Apache and php-cli use the newer php executable and make the change persist after reboot.
brew info php71

# restart apache
#sudo apachectl restart

# apply PATH changes without relogging
source ~/.profile

For some reason I did not have a /usr/local/sbin, so I had to create one to link it.

sudo mkdir /usr/local/sbin
sudo chown -R `whoami`:admin /usr/local/sbin
brew link php71

Put this in your .bash'src.

export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/sbin:$PATH"
export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"

Put this in your /usr/local/etc/php/7.1/php.ini.

extension=/usr/local/Cellar/php71-imagick/3.4.3_4/imagick.so

By default brew's httpd uses localhost:8080 for it's web server.

You will also need to enable the mods you need within brew's httpd.conf.

vim /usr/local/etc/httpd/httpd.conf

I also changed the following in it so I could use my mod_rewrite.

<Directory />
   AllowOverride All
   Require all denied
</Directory>

<Directory "/usr/local/var/www/my_folder">
  Options Indexes MultiViews FollowSymLinks ExecCGI
  AllowOverride none
  Require all granted
</Directory>

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

After doing this, you'll need to set permissions in the directory for a folder you bring in (in my case) because ImageMagick couldn't write to it.

sudo chown -R `whoami`:admin directory/
sudo chmod –R 777  directory/

# Not sure if this helped me at all with issues on jpeg and png
brew install imagemagick --build-from-source