Maksym Prokopov personal blog
Idea is a something worth sharing

Setting up a PHP development environment with Apache2 and php-fpm on macOS Catalina

09.06.2020

Reading time: 2 min.

At the moment using Docker for development on macOS © has its own flaws, and in case you need good performance, there is a more or less complicated way to set up a local development environment with native apache2 and php-fpm stack.

Keep in mind, PHP 7.3 comes with macOS Catalina preinstalled, so you might not need to install PHP separately.

Prerequisites

Installation

  1. Install PHP brew install php@7.4
  2. Install apache brew install apache2

At this point, you’ll have PHP v7.4 installed in /usr/local/opt/php@7.4/

In case you need to keep several PHP versions, it can be done using ENV vars in .zshrc

PHP_VERSION=7.4
export PATH="/usr/local/opt/php@${PHP_VERSION}/bin:/usr/local/opt/php@${PHP_VERSION}/sbin:$PATH"  
export LDFLAGS="-L/usr/local/opt/php@${PHP_VERSION}/lib"  
export CPPFLAGS="-I/usr/local/opt/php@${PHP_VERSION}/include"

PHP-FPM setup

Locate /usr/local/etc/php/7.4/php-fpm.d/www.conf and edit it to include environment variables. I also rebound default port 9000 to 9009 not to have conflicts with XDEBUG

clear_env = no 
listen = 127.0.0.1:9009

Apache setup

Locate /usr/local/etc/httpd/extra/httpd-vhosts.conf and edit it according to your project folder, here is my example


Listen 80  
<FilesMatch \\.php$> 
  SetHandler "proxy:fcgi://127.0.0.1:9009"  
</FilesMatch>
<VirtualHost *:80>  
        ServerAdmin webmaster@localhost  
        ServerName obo.local  
        DocumentRoot /Users/mprokopov/oxid/project/source  
        ErrorLog /Users/mprokopov/oxid/project/source/error.log  
        CustomLog /Users/mprokopov/oxid/project/source/access.log combined  
</VirtualHost>
<Directory "/Users/mprokopov/oxid/*/source">  
    Options Indexes FollowSymLinks  
    AllowOverride All
    Require all granted  
</Directory>

Project setup

For some reason php-fpm doesn’t play well with _ENV variables, so I had to change them to use getenv() function instead, like on the example below

config.inc.php of the project

$dbName = getenv('DB_NAME')  
$dbUser = getenv('DB_USER')  
...

Party time

When you decide to work on the project, go to the source folder of the project and run the command

DB_NAME=dbname DB_PASS=pass DB_USER=user php-fpm

and navigate in the browser to http://localhost address. It should work.

You can also save some efforts on typing and get env variables from the .env file by running eval $(cat .env) php-fpm instead.

Bonus: XDEBUG

In case you need XDEBUG, you can install it with command

1 pecl install xdebug

and edit /usr/local/etc/php/7.4/php.ini to include the following settings

zend\_extension="xdebug.so"  
xdebug.remote\_enable=on  
xdebug.remote\_host=127.0.0.1  
xdebug.remote\_handler=dbgp  
xdebug.idekey=geben  
xdebug.remote\_autostart=On  
xdebug.profiler\_enable\_trigger = 1  
xdebug.profiler\_enable=  
xdebug.profiler\_output\_dir = tmp  
xdebug.profiler\_output\_name = "callgrind.out.%p"