Installing Nginx, PHP and Mysql on Apple M1, MacOS 11 Big Sur
This guide will walk you through installing and configuring nginx, PHP and Mysql optimized for MacOS Big Sur on Apple Silicon - M1 Arm processors.
Note: Most of this content originated from this post. Here, I’ve pared it down and fixed the relevant content. Credit goes to the original author: Kevin Dees.
Setup
Before starting you need to install XCode for the command line tools. Open a Terminal and run:
1 | xcode-select --install |
Next, we need to install Homebrew. Homebrew is a package manager that every MacOS user should have. Installing it won’t hurt anything and you’ll definitely need it for this guide. The newest version of Homebrew has been built natively for the M1 Architecture. When you run this, you should see arm
files getting downloaded. This is how you’ll know if a package is compiled natively for M1 / ARM Apple Silicon.
This Homebrew installation method is automated, but if you’re security minded (and should be), you can download the installer script and examine it before running it.
1 | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" |
OpenSSL
We’ll need to install OpenSSL to use SSL certificates in nginx. This doesn’t break any existing SSL functionality when installed via Homebrew.
1 | brew install openssl |
MySQL
Install MySQL.
1 | brew install mysql |
Next, update your my.cnf
1 | vi /opt/homebrew/etc/my.cnf |
Add this content:
1 | # Default Homebrew MySQL server config |
Now, secure using the password password and then restart.
1 | mysql_secure_installation |
Next, MySQL 8 authentication needs to be updated per user to mysql_native_password
. Note that there is no space after the -p
. If you add the password on your command line, it will go in your bash history which is a security risk. If you don’t want that, simply use -p
and type the password when prompted.
1 | mysql -u root -ppassword |
Postgres SQL DB
Install postgresql
1 | brew install postgresql |
Now, you can check your user list.
1 | postgres-# \du |
PHP
Don’t use the default homebrew core tap for PHP. Use shivammathur/php.
1 | brew tap shivammathur/php |
You can verify the new binary is M1 native by running file
. The important thing to note is that it is arm64
.
1 | hostname:~ username$ file /opt/homebrew/bin/php |
Next, set PHP 7.4 as your default php CLI version.
1 | brew unlink php |
Now, for each version update the php-fpm you will need a unique port. Change the ports of each php-fpm to match its php version number. For example, php@7.4 I use port 9074.
Also, you will want php-fpm to run with your user account
and not _www
.
1 | vi /opt/homebrew/etc/php/7.4/php-fpm.d/www.conf |
1 | # default |
Optionally, before starting php-fpm, if you want to make edits to a php.ini file now is the time. For example, you might want to increase the upload_max_filesize and post_max_size to 10M.
1 | /opt/homebrew/etc/php/7.2/php.ini |
Once you are ready, start up php-fpm for each version.
1 | sudo brew services start php@7.4 |
Check that you have processes running and validate your ports are correct.
1 | sudo lsof -Pn | grep php-fpm |
Nginx
Install nginx.
1 | brew install nginx |
Now, test the install is working.
1 | curl http://localhost:8080 |
Next let’s change the default settings.
1 | vi /opt/homebrew/etc/nginx/nginx.conf |
From
1 | listen 8080; |
To
1 | listen 80; |
Next, add a FastCGI gateway to php-fpm on the default server. The latest version of php installed is best. For other servers, you can set the version of PHP to the project requirement.
1 | location ~ \.php$ { |
Optionally, add the Universal Coded Character Set for Unicode ( ‘Bəɹʤəɹɑn for example )
1 | charset utf-8; |
Next, we edit the real index.html file used by nginx. So, replace the index.html with an index.php file. Then, and some php code to make sure everything is working.
mv /opt/homebrew/var/www/index.html /opt/homebrew/var/www/index.php
vi /opt/homebrew/var/www/index.php
1 |
|
Now reload nginx:
1 | sudo nginx -s reload |
1 | curl http://localhost |
To add more servers you can go to the nginx servers directory, /opt/homebrew/etc/nginx/servers, and add them there as individual files. Here is a basic template.
1 | server { |
To add SSL for your nginx server check out this post. I use these bash functions to make the process faster. To add a server I use the command nginxcreate my.test.x but you might want to modify the files to match your setup.
1 | alias nginxreload="sudo nginx -s reload" |
Redis
Install Redis. This will install Redis Server v6.
1 | brew install redis |
Optionally, you can update your default dump.rdb file name in the redis.conf if you want.
1 | vi /opt/homebrew/etc/redis.conf |
The filename where to dump the DB
1 | dbfilename dump.rdb |
Resources
Installing Nginx, PHP and Mysql on Apple M1, MacOS 11 Big Sur
https://chrisbergeron.com/2021/03/17/MacOS-11-Big-Sur-Nginx-PHP-and-Mysql/