On this page:
Macintosh, Apache, MySQL, Perl/Python/PHP (MAMP) is doing my head in. I remember when it used to be simple and just worked. Now it seems it wants to be difficult.
I’m going to follow the instructions and see how I get on.
First things first: you’ll need Homebrew before you can use it to install anything; these instructions are perhaps clearer than Homebrew’s own:
The instructions above explain how to set up the native Apache; if I’m going to use Homebrew Apache, I’m going to need some different instructions – these instructions:
If you get any output from the following terminal command, Apache is already running:
sudo lsof -i :80
This is probably the built-in Apache, which you need to stop:
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
Re-running sudo lsof -i :80 should now produce no output.
Now you need to install the httpd package from Homebrew:
brew install httpd
This will install Apache, running as service httpd, and will listen to port 8080. You can start, restart and stop Apache by running the following commands:
brew services start httpdbrew services restart httpdbrew services stop httpd
Updating the system
You need to make sure your terminal realises the installation of Apache has changed. If which httpd gives /usr/sbin/httpd, then you’ll need to restart your terminal. The command will differ depending on your shell.
exec zshexec bash
If you don’t know which shell you’re using, use:
You should get:
If the output hasn’t updated, try quitting the terminal or restarting your computer.
Changing the port
You should change the port from 8080 to 80, which is the default port for Apache. Running:
sudo lsof -i :8080
should give you some output, meaning that Apache is listening to port 8080. You need to edit the httpd configuration file. I found it easier to open this file in a GUI text editor than in a terminal text editor. I used Sublime Text, which can be invoked from the command line:
Find Listen 8080 and change it to Listen 80.
Restart Apache to make it run with the new configuration.
Changing the document root
Apparently, there used to be a ~/Sites folder, but now there isn’t. This is where files are served to the web from. Create this folder either in Finder or in the terminal:
and check out its special icon.
Then go back to /usr/local/etc/httpd/httpd.conf, and make the following changes, where shortusername is your Mac username:
[…] DocumentRoot "/usr/local/var/www" […]
[…] DocumentRoot /Users/shortusername/Sites […]
[…] # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # AllowOverride FileInfo AuthConfig Limit # AllowOverride None […]
[…] # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # AllowOverride FileInfo AuthConfig Limit # AllowOverride All […]
[…] User _www Group _www […]
[…] User shortusername Group staff […]
[…] #ServerName www.example.com:8080 […]
[…] ServerName localhost […]
[…] #LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so […]
[…] LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so […]
Leave the file open – you’ll need it again soon.
Apache log files
Follow the error and access files as they update in a separate terminal each:
tail -f /usr/local/var/log/httpd/error_logtail -f /usr/local/var/log/httpd/access_log
Apple documented in the release notes for macOS Catalina 10.15 that certain scripting languages were only
included for compatibility with legacy software . PHP was deprecated in macOS 11.0 and is absent from macOS 12.0 onwards.
The most common versions of PHP to have installed seem to be 7.4, the last of 7.x, and 8.1, the most recent version, so I thought I would install both of these. I followed the instructions, but I could not get the result of php -v to be 8.1.x; rather, it insisted on being 7.4.x. This was frustrating until I found the answer on the GitHub site for the Homebrew package: the file paths for PHP 7.x have to be removed from the PATH variable. Perhaps this is why brew services list insists on listing an error for PHP 7.4.
Configuring PHP in Apache
There is another change to make in the /usr/local/etc/httpd/httpd.conf file. In the section with all the LoadModule entries, add:
[…] LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so […]
And at the end of the file, add this:
[…] # PHP settings Include /usr/local/etc/httpd/extra/httpd-php.conf
This includes another file with the PHP configuration; if you look in /usr/local/etc/httpd/extra/, you’ll see other configuration files that are included by /usr/local/etc/httpd/httpd.conf.
and add the following to it:
<IfModule php_module> <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch> <IfModule dir_module> DirectoryIndex index.php index.html </IfModule> </IfModule>
This was a bother. With the Homebrew installation, I kept getting authentication errors (e.g. access denied for ‘root’@’localhost’) no matter what I did. Going back to the original instructions for installing all the things, I saw that the MySQL instructions said to install the database directly from the MySQL website.
I installed the most recent DMG archive and ran the installer. The installer set up the database with a root user and password. The Homebrew version configured the database so that the root user had no password; I think the lack of password was causing the problems with authentication in the Homebrew version.
Once MySQL was up and running, I fixed the 2002 MySQL socket error that the instructions said to fix, then installed phpMyAdmin from the phpMyAdmin downloads page. This software provides an interface to the MySQL database. In phpMyAdmin, I created a new server as instructed with the root user I’d set up during installation of MySQL.
I tried to get virtual hosts working, but I couldn’t, despite following the instructions [4,5] I found very carefully. Something to try another time: it’s now time to get back to WordPress and my Panda-Puss theme!
To conclude, I followed the instructions and got there in the end, except for the virtual hosts thing, although I can’t seem to have any other files than index.php or index.html in the top level. Still, as long as I put each project in a new folder, it shouldn’t be a problem: files in folders seem to be ok.