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 [email protected] 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 [email protected]
brew services start httpd

stop.sh:

#!/bin/zsh

brew services stop mysql
brew services stop [email protected]
brew services stop httpd

restart.sh:

#!/bin/zsh

brew services restart mysql
brew services restart [email protected]
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/[email protected]/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

@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