Skip to content

Instantly share code, notes, and snippets.

@lennardv2
Last active October 3, 2024 20:29
Show Gist options
  • Save lennardv2/538d93ae35297b35e8b56db95b49469c to your computer and use it in GitHub Desktop.
Save lennardv2/538d93ae35297b35e8b56db95b49469c to your computer and use it in GitHub Desktop.
Native PHP development / MAMP stack on Apple silicon M1

Building the MAMP stack (php, apache & mysql) on Apple Silicon ARM (native)

Update! This tutorial is outdated. Nowadays brew installes m1 binaries just fine. Also use valet: https://laravel.com/docs/9.x/valet. It's 10x easier.

In this tutorial, we'll build the the nescessary packages for ARM via homebrew. After that we'll configure apache2 for using virtual hosts. The native php is ofcourse way faster, see the results of this benchmark below.

TEST NAME SECONDS OP/SEC
Rosetta2 191.654 sec 1.96 MOp/s
Intel i7-4790K (imac 2014) 156.791 sec 2.39 MOp/s
Intel i5-8500B (mini 2018) 141.381 sec 2.65 MOp/s
ARM m1 43.745 sec 8.58 MOp/s

Installing homebrew

cd ~
mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
sudo mv homebrew /opt/homebrew

This way homebrew will be installed to the /opt/homebrew folder. We can then initiate homebrew by cd to that directory and run brew in the bin folder.

cd /opt/homebrew/bin
./brew update

If not installed already, the system will ask to install the command-line tools for developers for you. Go ahead.

Now add the homebrew/bin directory to your path by editing/creating a .zshrc file in your home directory.

nano ~/.zshrc
export PATH="/opt/homebrew/bin:$PATH"

Build the packages

Note: checkout this comment if you have problems building python.

Update: Building is probably not necessary anymore, you can omit the -s flag.

brew install -s mysql php@7.4 httpd

This might take some time (30mins) or so. When everything is done it will list some next steps like adding the php module to apache.

Configuration

Make life a bit easier by symlinking the config directories to a rememberable path:

mkdir ~/MAMP
mkdir ~/MAMP/www
ln -s /opt/homebrew/etc/httpd ~/MAMP/httpd
ln -s /opt/homebrew/etc/php/7.4 ~/MAMP/php

Now, let's add some scripts to this directory

nano ~/MAMP/start.sh

start.sh:

#!/bin/zsh

brew services start mysql
brew services start php@7.4
brew services start httpd

stop.sh:

#!/bin/zsh

brew services stop mysql
brew services stop php@7.4
brew services stop httpd

restart.sh:

#!/bin/zsh

brew services restart mysql
brew services restart php@7.4
brew services restart httpd

Make 'em writable:

chmod +x ~/MAMP/start.sh
chmod +x ~/MAMP/stop.sh
chmod +x ~/MAMP/restart.sh

Oke, lets configure apache/httpd for usage with php and enable mod_rewrite along the way:

nano ~/MAMP/httpd/httpd.conf

Uncomment:

# LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

Change the DocumentRoot:

DocumentRoot "/Users/{username}/MAMP/www"
<Directory "/Users/{username}/MAMP/www">

And add the following to the bottom:


LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Include /opt/homebrew/etc/httpd/vhosts/*.conf

Now create a directory named vhosts

mkdir ~/MAMP/httpd/vhosts

Addding a website / virtual host

mkdir ~/MAMP/www/dev.example.com
mkdir ~/MAMP/www/dev.example.com/public_html
mkdir ~/MAMP/www/dev.example.com/logs
nano ~/MAMP/httpd/vhosts/dev.example.com.conf

Add:

<VirtualHost *:8080>
    DocumentRoot "/Users/{username}/MAMP/www/dev.example.com/public_html"
    ServerName dev.example.com
    ErrorLog "/Users/{username}/MAMP/www/dev.example.com/logs/error.log"
    CustomLog "/Users/{username}/MAMP/www/dev.example.com/logs/custom.log" common
</VirtualHost>

<Directory /Users/{username}/MAMP/www/dev.example.com/public_html>
        Options FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all
</Directory>

Add dev.example.com to your hosts file

echo '127.0.0.1 dev.example.com' | sudo tee -a /etc/hosts

Now restart:

~/MAMP/restart.sh

Troubleshooting

Check the output of the following commands:

sudo apachectl start 

If you have see a message saying something like Address already in use: AH00072: make_sock: could not bind to address, try changing the Listen config in httpd.conf to:

Listen 0.0.0.0:8080

Check if your config is valid:

apachectl configtest

Vist http://dev.example.com:8080

Hits

Copy link

ghost commented Jul 8, 2021

i Have a error after sudo apachectl restart
AH00557: httpd: apr_sockaddr_info_get() failed for Zennns-MacBook-Pro.local
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
(48)Address already in use: AH00072: make_sock: could not bind to address [::]:8080
(48)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:8080
no listening sockets available, shutting down
AH00015: Unable to open logs
zennn@Zennns-MacBook-Pro ~ % nano ~/MAMP/httpd/httpd.conf
zennn@Zennns-MacBook-Pro ~ % apachectl configtest
AH00557: httpd: apr_sockaddr_info_get() failed for Zennns-MacBook-Pro.local
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK

run in MBP M1

@Iznaur
Copy link

Iznaur commented Jul 15, 2021

did anyone have errors when trying install any pecl extension(i tried ssh2, gearman)? Homebrew/homebrew-core#67768 something like described there, how fix this?

Copy link

ghost commented Jul 16, 2021 via email

@pom2fer
Copy link

pom2fer commented Apr 3, 2022

Hello!!! You can make your life 10 times easier by using Valet!
//same than in this tuto
brew install php
brew install mysql
brew services start php
brew services start mysql
//To manage your hosts use Valet https://laravel.com/docs/9.x/valet
composer global require laravel/valet
valet install
cd YOUR_WWW_DIRECTORY
//the command below will do that each subfolder will become a website hosted on http://subfolder.test it's magical :-)
valet park
//Use PHPMonitor to get PHP versions on top bar and Valet integrations
brew tap nicoverbruggen/homebrew-cask
brew install --cask phpmon

I hope it helps :-)

@sr2ds
Copy link

sr2ds commented May 20, 2022

Hello! Someone with PHP 8 and phpunit very very very very slow on M1 too?
I thinked the problem was because running in docker or volumes but without docker with pure PHP on mac, the same performance.
So Bad 😔

@tonypartridge
Copy link

Ohhhh Amazing :-)

@lennardv2
Copy link
Author

lennardv2 commented Oct 26, 2022

Hello! Someone with PHP 8 and phpunit very very very very slow on M1 too? I thinked the problem was because running in docker or volumes but without docker with pure PHP on mac, the same performance. So Bad 😔

I seem to have slowness problems with 8.1/8.2
8.0 runs fine though..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment