MacOS, Apache, MySQL, PHP – without MAMP

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’ve found an article that shows you how to install Apache, MySQL and PHP on macOS using Homebrew, which I’ve found to be an easier way of doing things, except for when it isn’t.

Install Apache, MySQL, PHP on macOS 11 Big Sur and earlier macOS

I’m going to follow the instructions and see how I get on.

Installing Homebrew

First things first: you’ll need Homebrew before you can use it to install anything; these instructions are perhaps clearer than Homebrew’s own:

Installing Homebrew on macOS Monterey 12, Package Manager for Linux Apps

Installing Apache

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:

Installing & Configuring Apache on macOS using Homebrew and use Sites folder

If you get any output from the following terminal command, Apache might already be already running:

sudo lsof -i :80

If you get output like this, Apache is running:

COMMAND     PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
httpd     64133  root    4u  IPv6 0x3890e40d10e5cfcf      0t0  TCP *:http (LISTEN)
httpd     64150  _www    4u  IPv6 0x3890e40d10e5cfcf      0t0  TCP *:http (LISTEN)

This is probably the built-in Apache. If you get any output with your own username (as opposed to root and _www as above), that’s not Apache, and you can ignore it.

You need to stop the built-in Apache if it’s running:

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:

exec "$SHELL"

You should get:

which httpd/usr/local/bin/httpd

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:

subl /usr/local/etc/httpd/httpd.conf

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:

mkdir ~/Sites

and check out its special icon.

The ~./Sites directory gets its own 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"
<Directory "/usr/local/var/www">
[…]

[…]
DocumentRoot /Users/shortusername/Sites
<Directory "/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

Installing PHP

Apple documented in the release notes for macOS Catalina 10.15 that certain scripting languages were only included for compatibility with legacy software [1]. PHP was deprecated in macOS 11.0 and is absent from macOS 12.0 onwards.

Perhaps it’s because the version supplied was always out of date [2], so people installed it from Homebrew anyway; perhaps it’s because all the cool kids use some sort of Javascript framework [3]. So, even if you do have PHP installed on your system, it’s best to get a maintained installation from Homebrew. Installing PHP is necessary if you want to do WordPress development – perhaps the kids there aren’t cool enough to use JavaScript or perhaps the codebase is too big to change easily.

Updating to PHP versions 7.4 and 8 on macOS 11 Big Sur and Catalina

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 (edit: now 8.2), 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.

Create /usr/local/etc/httpd/extra/httpd-php.conf:

nano /usr/local/etc/httpd/extra/httpd-php.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>

Installing MySQL

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:

sudo mkdir /var/mysqlsudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Installing phpMyAdmin

I 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.

Virtual hosts

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.

References

  1. https://developer.apple.com/documentation/macos-release-notes/macos-catalina-10_15-release-notes
  2. https://developer.apple.com/forums/thread/681907
  3. https://discussions.apple.com/docs/DOC-250004361
  4. https://wpbeaches.com/set-up-virtual-hosts-on-macos-big-sur-11-in-apache/
  5. https://jasonmccreary.me/articles/configure-apache-virtualhost-mac-os-x/

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.