Set up PHP

Here you will install multiple versions of PHP and configure them to run in FPM mode.

If using macOS and Homebrew, out of the box it provides current PHP version in a package named php (version 7.4 at the time of writing), and other officially supported PHP versions in packages namespaced with version number, for example php@7.3. Officially unsupported PHP packages are not available in official formula repository (“tap” in brew lingo). However, they are still provided in some 3rd party taps, and it’s possible to install them from there.

1 Install

1.1 If using macOS with MacPorts

Execute on the command line:

sudo port install php84-fpm php84-mysql php84-imagick php84-gd php84-curl php84-opcache php84-mbstring php84-xsl php84-intl php84-sqlite php84-zip php84-openssl php84-iconv php84-sockets php84-exif php84-memcached php84-redis php84-sodium
sudo port install php83-fpm php83-mysql php83-imagick php83-gd php83-curl php83-opcache php83-mbstring php83-xsl php83-intl php83-sqlite php83-zip php83-openssl php83-iconv php83-sockets php83-exif php83-memcached php83-redis php83-sodium
sudo port install php82-fpm php82-mysql php82-imagick php82-gd php82-curl php82-opcache php82-mbstring php82-xsl php82-intl php82-sqlite php82-zip php82-openssl php82-iconv php82-sockets php82-exif php82-memcached php82-redis php82-sodium
sudo port install php81-fpm php81-mysql php81-imagick php81-gd php81-curl php81-opcache php81-mbstring php81-xsl php81-intl php81-sqlite php81-zip php81-openssl php81-iconv php81-sockets php81-exif php81-memcached php81-redis php81-sodium
sudo port install php80-fpm php80-mysql php80-imagick php80-gd php80-curl php80-opcache php80-mbstring php80-xsl php80-intl php80-sqlite php80-zip php80-openssl php80-iconv php80-sockets php80-exif php80-memcached php80-redis php80-sodium
sudo port install php74-fpm php74-mysql php74-imagick php74-gd php74-curl php74-opcache php74-mbstring php74-xsl php74-intl php74-sqlite php74-zip php74-openssl php74-iconv php74-sockets php74-exif php74-memcached php74-redis php74-sodium

Select desired default PHP version:

sudo port select php php84

This PHP version will be available from the command line as php.

1.2 If using macOS with Homebrew

Install PHP 7.4, 7.3 and 7.2 using Homebrew from the official tap:

brew install php php@7.3 php@7.2

PHP versions 7.1, 7.0 and 5.6 are available in exolnet/homebrew-deprecated tap. First register this tap with your local Homebrew installation with:

brew tap exolnet/homebrew-deprecated

Now you can install PHP 7.1, 7.0 and 5.6 with:

brew install exolnet/deprecated/php@7.1
brew install exolnet/deprecated/php@7.0
brew install exolnet/deprecated/php@5.6

2 Configure PHP-FPM pool definitions

For each PHP version edit PHP-FPM pool definition files (documented further below) and update it with the following configuration options:

user = brale
group = staff
listen = /var/run/php74-fpm.sock
listen.owner = brale
listen.group = staff
pm = ondemand
pm.max_children = 6
pm.process_idle_timeout = 15m
pm.max_requests = 128
pm.status_path = /status
ping.path = /ping
ping.response = "pong"

Make sure to use your own user and group instead of brale and staff, and name the socket file corresponding to the PHP version. Use configuration already existing in the file and do not create duplicate entries.

Note: Configuration files use ; character as a comment, so make sure you remove it as needed.

PHP-FPM resource consumption

Main PHP-FPM manager process takes up a small amount of memory, but spawned workers can take up to few hundred megabytes, depending on the application that was executed. To see how many worker processes are active and how much memory they use you can check the list of processes (ps or pstree on the command line), or open the PHP-FPM status page for the specific PHP version, for example https://home.php73/status?full&html.

2.1 Configure PHP-FPM pool definitions if installed using MacPorts

First, create PHP-FPM pool definitions by copying the default ones:

sudo cp /opt/local/etc/php84/php-fpm.d/www.conf.default /opt/local/etc/php84/php-fpm.d/www.conf
sudo cp /opt/local/etc/php83/php-fpm.d/www.conf.default /opt/local/etc/php83/php-fpm.d/www.conf
sudo cp /opt/local/etc/php82/php-fpm.d/www.conf.default /opt/local/etc/php82/php-fpm.d/www.conf
sudo cp /opt/local/etc/php81/php-fpm.d/www.conf.default /opt/local/etc/php81/php-fpm.d/www.conf
sudo cp /opt/local/etc/php80/php-fpm.d/www.conf.default /opt/local/etc/php80/php-fpm.d/www.conf
sudo cp /opt/local/etc/php74/php-fpm.d/www.conf.default /opt/local/etc/php74/php-fpm.d/www.conf

Update the created pool definition files as described above.

Next, create PHP-FPM configuration files by copying the default ones:

sudo cp /opt/local/etc/php84/php-fpm.conf.default /opt/local/etc/php84/php-fpm.conf
sudo cp /opt/local/etc/php83/php-fpm.conf.default /opt/local/etc/php83/php-fpm.conf
sudo cp /opt/local/etc/php82/php-fpm.conf.default /opt/local/etc/php82/php-fpm.conf
sudo cp /opt/local/etc/php81/php-fpm.conf.default /opt/local/etc/php81/php-fpm.conf
sudo cp /opt/local/etc/php80/php-fpm.conf.default /opt/local/etc/php80/php-fpm.conf
sudo cp /opt/local/etc/php74/php-fpm.conf.default /opt/local/etc/php74/php-fpm.conf

You don’t need to change the default configuration values.

2.2 Configure PHP-FPM pool definitions if installed using Homebrew

Find PHP-FPM pool definitions for your PHP versions in following files

/usr/local/etc/php/7.4/php-fpm.d/www.conf

Update these pool definition files as described above.

3 Configure PHP

For each PHP version find its configuration file (documented further below) and update it with the following configuration options:

date.timezone = Europe/Zagreb
session.gc_maxlifetime = 86400
memory_limit = 256M
error_log = /Users/brale/php73.log

Don’t forget to modify error log path to your user’s home directory, and set the correct PHP version depending on the ini file you’re modifying.

Note: Configuration files use ; character as a comment, so make sure you remove it as needed.

3.1 Configure PHP if using macOS and MacPorts

First create PHP configuration files by copying the default ones:

sudo cp /opt/local/etc/php84/php.ini-development /opt/local/etc/php84/php.ini
sudo cp /opt/local/etc/php83/php.ini-development /opt/local/etc/php83/php.ini
sudo cp /opt/local/etc/php82/php.ini-development /opt/local/etc/php82/php.ini
sudo cp /opt/local/etc/php81/php.ini-development /opt/local/etc/php81/php.ini
sudo cp /opt/local/etc/php80/php.ini-development /opt/local/etc/php80/php.ini
sudo cp /opt/local/etc/php74/php.ini-development /opt/local/etc/php74/php.ini

Update the created configuration files as described above and additionally with:

pdo_mysql.default_socket=/opt/local/var/run/mysql/mysqld.sock
mysqli.default_socket=/opt/local/var/run/mysql/mysqld.sock

This will enable using localhost as the database host from your application.

3.2 Configure PHP if using macOS and Homebrew

Find the configuration for your PHP versions in the following files:

/usr/local/etc/php/7.4/php.ini
/usr/local/etc/php/7.3/php.ini
/usr/local/etc/php/7.2/php.ini
/usr/local/etc/php/7.1/php.ini
/usr/local/etc/php/7.0/php.ini
/usr/local/etc/php/5.6/php.ini

Update these configuration files as described above.

5 Start PHP-FPM services

You can now start PHP services.

5.1 Start PHP-FPM services if installed using MacPorts

sudo port load php84-fpm
sudo port load php83-fpm
sudo port load php82-fpm
sudo port load php81-fpm
sudo port load php80-fpm
sudo port load php74-fpm

This will also ensure that PHP-FPM server starts automatically after a reboot.

Remember to restart them after changing PHP configuration in the future with:

sudo port reload phpxx-fpm

To stop the server and prevent it from running after a reboot, execute:

sudo port unload phpxx-fpm

5.2 Start PHP-FPM services if installed using Homebrew

sudo brew services start php@7.4
sudo brew services start php@7.3
sudo brew services start php@7.2
sudo brew services start php@7.1
sudo brew services start php@7.0
sudo brew services start php@5.6

This will also ensure that PHP-FPM server starts automatically after a reboot.

Remember to restart them after changing PHP configuration in the future with:

sudo brew services restart php@x.x

To stop the server and prevent it from running after a reboot, execute:

sudo brew services stop phpxx-fpm

6 Install PHP extensions

Installed PHP will come with built-in extension, but if your project requires additional extensions, these have to be installed separately.

6.1 Install PHP extensions using MacPorts

MacPorts already provides installable PHP extensions, find them for a particular PHP version by executing:

port list php56\*

Simply install the PHP extension you need, using MacPorts, for example:

sudo port install php56-memcached

6.2 If using Homebrew, compile the required PHP extensions manually

Install extensions using pecl for the specific version of php, for example:

/usr/local/Cellar/php@7.1/7.1.33/bin/pecl install imagick

Add the extension ini file to the conf.d folder for the specific version of php, for example:

echo "extension=imagick.so" > /usr/local/etc/php/7.1/conf.d/ext-imagick.ini

Make sure to replace the path to the pecl binary, the extension and the extension ini file to match your version of php and the extension you want to install.