macOS NGINX, PHP multiple version setup

configuration

Creating this guide for self reference so that every time I don't waste time on this stuff and do something productive.

Prerequisite: Before setting up nginx/php, you need xcode with CLI tools installed on your machine.

xcode-select --install

PHP

Don't use default homebrew core tap for PHP 🤷🏻‍♂️. Use shivammathur/php. Using this you can easily install or switch between multiple php versions.

brew tap shivammathur/php

Note: If you have packages from old homebrew/php tap, you can remove completly

Check this commands to cleanup old tap
# Remove PHP from Homebrew Cellar
rm -rf $(brew --cellar)/php

# Remove old PHP Launch Agents and daemons, if present:
rm -f ~/Library/LaunchAgents/homebrew.mxcl.php*
rm -f /Library/LaunchAgents/homebrew.mxcl.php*
rm -f /Library/LaunchDaemons/homebrew.mxcl.php*

# Remove homebrew/php tap
brew untap homebrew/php

# Cleanup
brew cleanup

Install php versions that you required. (Note, php gets install with php-fpm)

brew install shivammathur/php/php@7.2
brew install shivammathur/php/php@7.3
brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.0

Set PHP 7.4 as your default php CLI version

brew unlink php
brew link --overwrite --force php@7.4

To run php-fpm you will need a unique port. Change ports of each php-fpm. We will match php version with port to easily understand running process -

vim /usr/local/etc/php/7.4/php-fpm.d/www.conf
www.conf
# You may want to change `user` and `group` from `_www`
user = <your_username>
group = staff

# Find `listen` and replace with following:
listen = 127.0.0.1:9074

Before starting php-fpm, you may want to change some defaults from php.ini.

max_execution_time = 30
memory_limit = 1024M
post_max_size = 1024M
file_uploads = On
upload_max_filesize = 1024M
max_file_uploads = 200

Finally fire up php-fpm

sudo brew services start php@7.2
sudo brew services start php@7.3
sudo brew services start php@7.4
sudo brew services start php@8.0

Verify that you have processes running and validate your ports

sudo lsof -i -n -P | grep php-fpm

php-fpm-running-process

Optionally, add aliases and replace <your_version> with the version homebrew installs.

alias php72="/usr/local/Cellar/php@7.2/<your_version>/bin/php"
alias php73="/usr/local/Cellar/php@7.3/<your_version>/bin/php"
alias php74="/usr/local/Cellar/php@7.4/<your_version>/bin/php"
# The latest version of php in be located under
# the "php" folder not an "@" folder.
alias php80="/usr/local/Cellar/php/<your_version>/bin/php"
# Make switching versions easy
function phpv() {
    brew unlink php
    brew link --overwrite --force "php@$1"
}

If you want to change the default php CLI you can set it using brew or alias we added above

# brew
brew unlink php
brew link --overwrite --force php@7.4
# bash function
phpv 7.4

PHP Errors

As time passes Homebrew is bound to break you PHP installations. When this happens you can reinstall PHP version having the error. Keep in midn you may need to reconfigure that version of PHP but php.ini file remain same

brew reinstall shivammathur/php/php@7.4

Nginx

Install nginx

brew install nginx
sudo nginx

Test the installation by visiting http://localhost:8080.

Now, change default settings by going to /usr/local/etc/nginx/nginx.conf

listen 80;
server_name localhost test.x;
index index.html index.htm index.php;

Next, add FastCGI gateway to php-fpm on the default server -

location ~ \.php$ {
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
  fastcgi_pass 127.0.0.1:9074;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
}

Add some basic security to your default server -

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

Add charset,

charset utf-8;

Allow large file upload,

http {
    ...
    client_max_body_size 100M;
}

Next, create index.php file on /usr/local/var/www/index.php path and add phpinfo()

Reload nginx

sudo nginx -s reload

To test nginx config

nginx -t

To add more server you can go to the nginx servers dir, /usr/local/etc/nginx/servers and add individual files. Checkout this template

Checkout below resources:

Cheers!