macOS: Install nginx, MySQL and PHP via brew

Since I was not satisfied with the performance of MAMP PRO and also used my local development environment mainly within the scope of localhost and not within multiple hosts, I decided to install a combination of nginx, MySQL and PHP via brew.

brew is a package manager, which allows to install many packages without a hassle.

Following I show you the fast installation of the packages and their basic configuration on macOS.


You can start the installation via the following command in your Terminal:

brew install nginxCode language: Bash (bash)

The initial configuration can be found in the directory /usr/local/etc/nginx (or in /opt/homebrew/etc/nginx for Macs with Apple Silicon). At first, edit the nginx.conf in this directory, e.g. with this command:

pico /usr/local/etc/nginx/nginx.confCode language: Bash (bash)

To use PHP later on you need to adjust this block:

        location / {
            root   html;
            index  index.html index.htm;
        }Code language: Nginx (nginx)

So that it looks like this:

	root /path/to/webroot;
	index  index.html index.htm index.php;

	location / {
		autoindex on;
		try_files $uri $uri/ /index.php?$args;

		proxy_buffer_size 128k;
		proxy_buffers 4 256k;
		proxy_busy_buffers_size 256k;
	}Code language: Nginx (nginx)

You need to set the path to your web root in the first line, which will be displayed after opening http://localhost in your browser. The line starting with index defines, which files will be accessed directly while opening a directory. Here you need to add the index.php at the end.

autoindex on; allows you to display the directory tree if there is no file specified in index. This is helpful for development, especially if you have multiply projects in sub-directories. Please never enable this function in a production environment.

The try_files directive declares how nginx tries to access a requested file. The given variant should fit the need of most applications. If you need a different, it should be found in the documentation of the particular application.

The different proxy buffer settings fix some issues with bigger websites (e.g. if they send large headers).

To be able to user PHP later on, you also need to add the following block below the one from above:

	location ~ \.php$ {
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
	}Code language: Nginx (nginx)

By default, nginx runs on port 8080 since port 80 and 443, which are usually used for HTTP and HTTPS, can only be used with administrator permissions on macOS. If you want to use port 80, you need to adjust the following line:

listen       8080;


listen       80;

Afterwards, you can start nginx with the following command:

brew services run nginxCode language: Bash (bash)

Or as administrator via sudo:

sudo brew services run nginxCode language: Bash (bash)

If you want to start the nginx server on system boot, you can use the following command instead:

brew services start nginxCode language: Bash (bash)

Note: If you need to start nginx with administrator permissions to use port 80, it’s not possible to automatically start it on system boot.

To stop the server, you can use the following command:

brew services stop nginxCode language: Bash (bash)

And to restart it, use the restart command:

brew services restart nginxCode language: Bash (bash)

This command chain can be used for all services of brew, so also for MySQL and PHP (you just need to replace the nginx at the end to mysql or php).


To install MySQL, you can use the following command in your Terminal:

brew install mysqlCode language: Bash (bash)

The configuration file can be found in /usr/local/etc/my.cnf (or in /opt/homebrew/etc/my.cnf for Macs with Apple Silicon) and can be opened similar to the nginx configuration:

pico /usr/local/etc/my.cnfCode language: Bash (bash)

As basis you should use at least the following configuration:

# Default Homebrew MySQL server config
# Only allow connections from localhost
bind-address            =
mysqlx-bind-address     =
socket                  = /tmp/mysql.sock

character-set-server    = utf8mb4
collation-server        = utf8mb4_unicode_ci
character-set-client-handshake = falseCode language: PHP (php)

If the service is already running after the installation, you should restart it now:

brew services restart mysqlCode language: Bash (bash)

To test if the MySQL server is running, you can enter the following command:

mysql -u root -p

The default password for user root is also root. I leave that value in my local test environment.

If everything works, the output should look like the following:

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 8.0.25 Homebrew

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>Code language: JavaScript (javascript)


Even PHP can be installed via a single command in the Terminal:

brew install phpCode language: Bash (bash)

Currently, PHP 8.0 will be installed using this command. If you want to install another version, you can do it like this:

First, install a new source – so called “tap” – to get different PHP versions:

brew tap shivammathur/phpCode language: Bash (bash)

Then, install your desired version like this:

brew install php@7.4Code language: Bash (bash)

This way, PHP 7.4 will also be installed if you already used the first command.

Similar to Linux, every PHP version has its own configuration directory in /usr/local/etc/php (or in /opt/homebrew/etc/php for Macs with Apple Silicon):

ls -l /usr/local/etc/php
total 0
drwxr-xr-x  10 matze  admin  320 Jul  3 15:55 7.4
drwxr-xr-x  10 matze  admin  320 Jul  1 18:23 8.0

The php.ini can be found in the directory of every version, e.g. in /usr/local/etc/php/8.0/php.ini.

Switch between PHP versions

To switch between PHP versions, you can use the unlink and link commands of brew.

To switch to PHP 7.4, use the following commands:

brew unlink php
brew link php@7.4 --force --overwriteCode language: Bash (bash)

To switch back, just replace the values of php and php@7.4 in the commands:

brew unlink php@7.4
brew link php --force --overwriteCode language: Bash (bash)

Before doing that, please make sure to stop any current running PHP version via brew services stop php or brew services stop php@7.4.

Afterwards, restart your Terminal session to apply the changes.

3 thoughts on “macOS: Install nginx, MySQL and PHP via brew

  1. Thanks, the post helped me a lot.
    Tho the command “brew install php@7.4” didn’t work on my Apple Silicon, I tried this “brew tap shivammathur/php” then “brew install php@7.4” and php 7.4 installed successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *