Tech

How to replace PHP on OS X Server 5.x with PHP via Homebrew

The version of  PHP on OS X Server 5.x is v.5.3.x. As of this writing, the current distro of PHP on Homebrew is v.7.4.12. You will lose the ability to control PHP loading from the GUI.

To prep, you should have prepared a phpinfo.php file on your website in an orphan directory so you can check all the modules that load with PHP. It is simply a text file with the name phpinfo.php and the content is:

phpinfo();

Don’t worry if PHP is currently running.

Install PHP via Homebrew:

brew install php

or if you already have a brew version:

brew upgrade php

Homebrew will download and update all dependencies and should install everything you need somewhere under /usr/local so it will not interfere with /etc, /sbin or /bin.

Now create a httpd_custom.conf file in your webserver’s config directory:

sudo nano /Library/Server/Web/Config/apache2/sites/httpd_custom.conf 

The content of httpd_custom.conf should include the location of the new PHP runtime and FileMatch parameter for PHP 7:

LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so
<FilesMatch \.php$>
  SetHandler application/x-httpd-php
</FilesMatch>

Now open up the Web setting in ServerAdmin.app and disable PHP for your websites.

If you have already upgraded to Server 5.7 or 5.9, then you may not have access to this control panel anymore. You will have to manually disable the old PHP runtime by editing /Library/Server/Web/Config/apache2/httpd_server_app.conf and commenting out

# LoadModule php7_module libexec/apache2/libphp7.so

Now restart Apache, the web server, via ServerAdmin.app if you still can. If you have lost that access, the Terminal.app command is:

sudo serveradmin stop web

then

sudo serveradmin start web

*Unfortunately, apachectl graceful does not work for OS X Server.

Now everytime your server starts up, the web service should load the version of PHP installed by Homebrew. 

You may tweak this version of PHP through the php.ini file at:

/usr/local/etc/php/7.4/php.ini

And that’s it, done with PHP.

**You may also want to install ImageMagick if you’re running any WordPress sites on this server.

***Check DirectoryIndex includes index.php

  DirectoryIndex index.php index.html