Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomaskanzig/5f4655004cee6ea11665d04ead9420e0 to your computer and use it in GitHub Desktop.
Save thomaskanzig/5f4655004cee6ea11665d04ead9420e0 to your computer and use it in GitHub Desktop.
How to install PHP + Apache + MySQL + phpMyAdmin on macOS Monterey using Homebrew

How to install PHP + Apache + MySQL + phpMyAdmin on macOS Monterey using Homebrew

This is a simple process with all instructions for the Development Environment on Mac OS X.
All steps will be launch in your Terminal (/Applications/Utilities/Terminal).

XCode Command Line Tools

If you don't already have XCode installed, it's best to first install the command line tools as these will be used by homebrew:

xcode-select --install

Homebrew Installation

This process relies heavily on the macOS package manager called Homebrew. Using the brew command you can easily add powerful functionality to your mac, but first we have to install it:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Just follow the terminal prompts and enter your password where required. This may take a few minutes.

If this is a fresh install and you don't have your path setup properly, you can follow the installation "next steps" which are already customized for you, or you can manually add the following paths to your .bashrc or .zshrc:

eval "$(/opt/homebrew/bin/brew shellenv)"

You should probably also run the following command to ensure everything is configured correctly:

brew doctor

It will instruct you if you need to correct anything.

Monterey Required Libraries

When installing fresh on Monterey, I ran into a few libraries that were missing when completing all the steps below. To make things easier, please simply run this now:

brew install openssl

Apache Installation

The latest macOS 12.0 Monterey comes with Apache 2.4 pre-installed, however, it is no longer a simple task to use this version with Homebrew because Apple has removed some required scripts in this release. However, the solution is to install Apache 2.4 via Homebrew and then configure it to run on the standard ports (80/443).

If you already have the built-in Apache running, it will need to be shutdown first, and any auto-loading scripts removed. It really doesn't hurt to just run all these commands in order - even if it's a fresh installation:

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Now we need to install the new version provided by Brew:

brew install httpd

Now we just need to configure things so that our new Apache server is auto-started

brew services start httpd

You now have installed Homebrew's Apache, and configured it to auto-start with a privileged account. It should already be running, so you can try to reach your server in a browser by pointing it at http://localhost:8080, you should see a simple header that says "It works!".

It works

Troubleshooting Tips

If you get a message that the browser can't connect to the server, first check to ensure the server is up.

ps -aef | grep httpd

You should see a few httpd processes if Apache is up and running, like this:

Apache is running

Try to restart Apache with:

brew services restart httpd

You can watch the Apache error log in a new Terminal tab/window during a restart to see if anything is invalid or causing a problem:

tail -f /opt/homebrew/var/log/httpd/error_log

Apache is controlled via the brew services command so some useful commands to use are:

brew services stop httpd
brew services start httpd
brew services restart httpd

Apache Configuration

Now that we have a working web server, we will want to do is make some configuration changes so it works better as a local development server.

In the latest version of Brew, you have to manually set the listen port from the default of 8080 to 80, so we will need to edit Apache's configuration file.

Open the configuration file:

vim /opt/homebrew/etc/httpd/httpd.conf

Find the line that says

Listen 8080

and change it to 80:

Listen 80

Listen 80

In the <Directory> block you will find an AllowOverride setting, this should be changed as follows:

#
# 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

Also we should now enable mod_rewrite which is commented out by default. Search for mod_rewrite.so and uncomment the line by removing the leading # by pushing ⌘ + / on the line (this is a quick way to uncomment and comment a single or multiple lines:

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

User & Group

Now we have the Apache configuration pointing to a Sites folder in our home directory. One problem still exists, however. By default, apache runs as the user daemon and group daemon. This will cause permission problems when trying to access files in our home directory. About a third of the way down the httpd.conf file there are two settings to set the User and Group Apache will run under. Change these to match your user account (replace your_user with your real username), with a group of staff:

User your_user
Group staff

Servername

Apache likes to have a server name in the configuration, but this is disabled by default, so search for:

#ServerName www.example.com:8080

and replace it with:

ServerName localhost

Restart apache to ensure your configuration changes have taken effect:

brew services stop httpd
brew services start httpd

PHP Installation

Up until the end of March 2018, all PHP related brews were handled by Homebrew/php tab, but that has been deprecated, so now we use what's available in the Homebrew/core package. This should be a better maintained, but is a much less complete, set of packages.

PHP 7.0, and PHP 7.1 have been deprecated and removed from Brew because they are out of support, and while it's not recommended for production, there are legitimate reasons to test these unsupported versions in a development environment. These versions also need to "built from source" in order to use the latest versions of icu4c and openssl.

Remember only PHP 7.2 through 8.1 are officially supported by Brew, but these also have to be built which is pretty slow. For the latest version of our guide we will use the new tap from @shivammahtur as there are many versions (including the latest PHP 8.1) pre-built.

brew tap shivammathur/php

We will proceed by installing various versions of PHP and using a simple script to switch between them as we need. Feel free to exclude any versions you don't want to install.

brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.0

For sprad, is mandatory to install PHP 7.4.

Also, you may have the need to tweak configuration settings of PHP to your needs. A common thing to change is the memory settings or the date.timezone configuration. The php.ini files for each version of PHP are located in the following directories:

/opt/homebrew/etc/php/7.4/php.ini
/opt/homebrew/etc/php/8.0/php.ini

We have installed but not linked these PHP versions. To switch to PHP 7.4 for example we can type:

brew link --overwrite --force php@7.4

Quick test that we're in the correct version:

php -v

And you will see something like this:

PHP 7.4.30 (cli) (built: Jun  7 2022 14:03:57) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

Apache PHP Setup

You have successfully installed your PHP versions, but we need to tell Apache to use them. You will again need to edit the /opt/homebrew/etc/httpd/httpd.conf file scroll to the bottom of the LoadModule entries.

Open the configuration file:

vim /opt/homebrew/etc/httpd/httpd.conf

Add the libphp module in the end of all modules:

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

Check again that the mode_rewrite package is not commented out either, as mentioned in this tutorial earlier:

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

This should be in the end something like this:

Modules

We can only have one module processing PHP at a time, so for now, so we have left our php@7.4 entry uncommented while all the others are commented out. This will tell Apache to use PHP 7.4 to handle PHP requests. (We will add the ability to switch PHP versions later).

Also you must set the Directory Indexes for PHP explicitly, so search for this block:

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

and replace it with this:

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

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

Save the file and stop Apache then start again, now that we have installed PHP:

brew services stop httpd
brew services start httpd

MySQL Installation

brew install mysql

Test connection

mysql -uroot

We've installed your MySQL database without a root password. To secure it run:

mysql_secure_installation

And then, initialize your MyQSL

mysql.server start

Troubleshooting Tips

When comes this typical problem ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'. Then try to re-install mysql again.

MySQL Configuration

To avoid any problems with import and export. Is needed to disable the strict mode.
Open your my.cnf file:

vim /opt/homebrew/etc/my.cnf

And add this:

innodb_strict_mode = 0

Restart mysql:

mysql.server restart

Install phpMyAdmin

brew install phpmyadmin

Create phpMyAdmin.conf

vim /opt/homebrew/etc/httpd/extra/phpmyadmin.conf

Insert following:

Alias /phpmyadmin /opt/homebrew/share/phpmyadmin
<Directory /opt/homebrew/share/phpmyadmin/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Allow from all
    </IfModule>
</Directory>

Open the phpMyAdmin.conf file

vim /opt/homebrew/etc/httpd/httpd.conf

Append at the end of the conf this script below:

# Load phpMyAdmin configuration
Include /opt/homebrew/etc/httpd/extra/phpmyadmin.conf

If you access your database without password, is necessary to enable AllowNoPassword in the config:

vim /opt/homebrew/etc/phpmyadmin.config.inc.php

Search for:

$cfg['Servers'][$i]['AllowNoPassword'] = false;

and replace it with:

$cfg['Servers'][$i]['AllowNoPassword'] = true;

Restart your apache

brew services restart httpd

You now have installed phpMyAdmin and is reachable via http://localhost/phpmyadmin/

Modules

Good job!

@zloadmin
Copy link

file /opt/homebrew/etc/httpd/httpd.conf not exists

@thomaskanzig
Copy link
Author

@zloadmin maybe if you do this in terminal:

ps ax | grep httpd

you can see where are running your Apache process and then you can go to the correct path.
In my case is in /opt/homebrew/opt/httpd.

  360   ??  Ss     0:05.45 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  664   ??  S      0:00.69 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
 2862   ??  S      0:02.50 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
 7159   ??  S      0:00.54 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
 7240   ??  S      0:01.49 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
 7361   ??  S      0:00.95 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
 7368   ??  S      0:00.46 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
10110   ??  S      0:00.49 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
11632   ??  S      0:00.31 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
11934   ??  S      0:00.06 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
13674   ??  S      0:00.03 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
18702 s001  S+     0:00.00 grep httpd

@johnstumbles
Copy link

file /opt/homebrew/etc/httpd/httpd.conf not exists

On my Monterey installation it appears to be at /usr/local/etc/httpd/httpd.conf

@medxpy
Copy link

medxpy commented Oct 29, 2023

i did all the step mentioned above till MySQL and I lost the server, when i try to access localhost on my browser i got "this site can't be reached"

@tinyamasisurum0
Copy link

tinyamasisurum0 commented Oct 31, 2023

i did all the step mentioned above till MySQL and I lost the server, when i try to access localhost on my browser i got "this site can't be reached"

same for me. developing shouldn't be this hard in 21st century. we make easier for everyone else but not ourselves (developers)

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