Setting up a Nginx
web server on macOS

15th December 2017
(initially published the 5th January 2016)

Seeking a satisfactory solution to create a local web server for programming in macOS with PHP and MySQL, I was disappointed that the turnkey solutions were far from equaling the WAMP that may exist on Windows.

But I forgot macOS is a Unix system, and unlike Windows, it’s perfectly possible to create a customized local server with some packages.

We will see how to install Nginx, PHP-FPM and MariaDB (MySQL) on macOS High Sierra thanks to Homebrew package manager.


HomeBrew is a package manager for macOS, that allows to easily install various Unix applications.

To install, simply execute the command shown on the official website:

/usr/bin/ruby -e "$(curl -fsSL"

If you do not already have them, macOS will prompt you to first install Xcode Command Line Tools.


Although Apache is natively included with macOS, we propose here to install Nginx, particularly lightweight and easily configurable.


To install and launch Nginx on startup, we use:

brew install nginx
sudo brew services start nginx

Although we musn’t use sudo with brew install, it is necessary to use it to launch Nginx if we want to use the the default port 80.


We want to store our web site in the folder of our choice, and access to the URL http://localhost/. To do this, edit the configuration file:

nano /usr/local/etc/nginx/nginx.conf

To begin, we will have to give to Nginx the permission to access our files and avoid a nasty 403 Forbidden error. To do so, we change the first line, where <user> is your username:

user <user> staff;

Then, to add a new website, we will add a new section inside the http directive:

server {
  listen 80;
  server_name localhost;
  root /Users/<user>/Documents/path/to/your/website;
  index index.html index.htm;

We then restart Nginx in order to take this changes into account:

sudo brew services restart nginx


In order to use PHP with Nginx we will use PHP-FPM. Here, we will use PHP 7.2, but you can easily choose any other version:

brew tap homebrew/homebrew-php
brew install php72

Then, we re-edit the configuration file:

nano /usr/local/etc/nginx/nginx.conf

We modify the line starting with index by:

index index.php index.html;

Finally, add in the section server the following lines to run PHP for all files with the extension .php:

location ~ \.php {
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;

To avoid a File not found. error, we also need to give the right permissions to PHP. In the following file:

nano /usr/local/etc/php/7.2/php-fpm.d/www.conf

Change the following parameter to:

user = <user>
group = staff

At last, we restart Nginx to activate the changes, and we don’t forget to launch PHP, to avoid a 502 Bad Gateway:

sudo brew services restart nginx
sudo brew services start homebrew/php/php72


We will now install and launch MariaDB:

brew install mariadb
brew services start mariadb

Finally, complete the installation by choosing a root password for MySQL:


Here is the perfect MAMP installation !

Published on the 15th December 2017 by Sylvain Durand.