Brew, nginx and PHP on Mac

Docroot is: /opt/homebrew/var/www

The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /opt/homebrew/etc/nginx/servers/.

To start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/nginx/bin/nginx -g daemon off;
==> Summary
/opt/homebrew/Cellar/nginx/1.25.1: 26 files, 2.4MB

Starting and stopping

brew services start nginx brew services start php

Check it's running

brew services

Where are service files installed

brew list php brew list nginx

Other commands

  • brew list list packages
  • brew list --versions list with versions
  • brew list --cask list cask packages
  • brew outdated upgradeable packages
  • brew outdated --cask upgradeable cask packages
  • brew leaves packages without dependencies
  • brew deps --tree --installed show dependencies

Where are nginx configs

look for something like

/opt/homebrew/etc/nginx/servers/ fastcgi_pass u

Where is PHP

look for something like /opt/homebrew/Cellar/php/8.2.7_1/.bottle/etc/php/8.2/php-fpm.d/www.conf and see listen = 127.0.0.1:9000

 Sample nginx config file

server {
    listen 80;
    server_name dome-laravel-10.local;
    root "/Users/Firstname.lastname/mywww/domelaravel-10/public";
    index index.html index.htm index.php;
    charset utf-8;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    access_log off;
    sendfile off;
    client_max_body_size 100m;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_intercept_errors off;
        include fastcgi_params;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }
    location ~ /\.ht {
        deny all;
    }
}

Troubleshooting

You may see some errors like:

Error: Failure while executing;/bin/launchctl bootstrap gui/502 /Users/Martin.Hearn/Library/LaunchAgents/homebrew.mxcl.nginx.plistexited with 5.

It happens when a previous postgres process doesn't terminate properly (maybe because you killed a psql process in activity monitor or otherwise). Restarting appears to properly unlock/delete the postmaster.pid file where "start" doesn't. Source