Installing nginx on Mac OSX

Install MacPorts: See InstallingMacPorts, then:

$ sudo port install nginx +ssl

Root folder is treated as /opt/local so configuration files are under /opt/local/etc/nginx, with the default website under /opt/local/share/nginx/html.

Set nginx to start during boot

$ sudo port load nginx

or

$ sudo launchctl load /Library/LaunchDaemons/org.macports.nginx.plist

Disable nginx automatic startup at boot

$ sudo port unload nginx

or

$ sudo launchctl unload /Library/LaunchDaemons/org.macports.nginx.plist

Reload Configuration

$ sudo nginx -s reload

Installing PHP

These instructions relate to installing PHP version 7.3.13 on macOS 10.15.3 (Catalina) using MacPorts

  1. Install the php and php-fpm packages:

    $ sudo port install php73 php73-fpm

  2. Enable the Unix Socket in `php-fpm.conf’

    $ cd /opt/local/etc/php73/ $ cp php-fpm.conf.default php-fpm.conf

  3. Edit php-fpm.conf and uncomment the following line by removing the semi-colon prefix:

    pid = run/php73/php-fpm.pid

  4. Create a configuration section in /opt/local/etc/nginx/nginx.conf

    E.g.

    http { # … other configuration directives …

      server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        root share/nginx/html;
    
        index index.html index.htm index.nginx-debian.html index.php;
    
        server_name _;
    
        location / {
          # First attempt to serve request as file, then
          # as directory, then fall back to displaying a 404.
          try_files $uri $uri/ =404;
        }
    
        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
          fastcgi_index index.php;
          include fastcgi.conf;
    
          fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    
          # Check that the PHP script exists before passing it
          try_files $fastcgi_script_name =404;
    
          # Bypass the fact that try_files resets 
          # see: http://trac.nginx.org/nginx/ticket/321
          set $path_info $fastcgi_path_info;
          fastcgi_param PATH_INFO $path_info;
    
          # With php-fpm (or other unix sockets):
          fastcgi_pass unix:/opt/local/var/run/php73/php7.3-fpm.sock;
        #    # With php-cgi (or other tcp sockets):
        #    fastcgi_pass 127.0.0.1:9000;
        }
      }

    }

  5. Start the server with:

    $ sudo port load nginx php73-fpm

  6. Stop the server with:

    $ sudo port unload nginx php73-fpm

  7. See VikiHosting for some instructions on creating a PHP test page.

See Also:

– Frank Dean - 14 Feb 2020


– Frank Dean - 10 Mar 2017

Related Topics NginxTips, InstallingMacPorts, MacOSXTips, VikiHosting