Skip to content

Instantly share code, notes, and snippets.

@kitloong
Last active April 27, 2024 06:56
Show Gist options
  • Star 64 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save kitloong/5f66a140f38b9698e8c3ed13b968ff47 to your computer and use it in GitHub Desktop.
Save kitloong/5f66a140f38b9698e8c3ed13b968ff47 to your computer and use it in GitHub Desktop.
Mac - Install Apache, PHP, MySQL + phpMyAdmin with Homebrew

!!! This guide was created with Macbook Pro M1. Path may vary for different or even same machine.

Install Homebrew

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

Install PHP

brew install php
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

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

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /opt/homebrew/etc/php/8.1/

To restart php after an upgrade:
  brew services restart php
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/php/sbin/php-fpm --nodaemonize
brew services restart php
php --version
PHP 8.1.2 (cli) (built: Jan 21 2022 04:34:01) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

Update config

php -i | grep "additional .ini"
Scan this dir for additional .ini files => /opt/homebrew/etc/php/8.1/conf.d

# Instead of modify setting in default .ini file, /opt/homebrew/etc/php/8.1/php.ini
# You should vi new .ini file to /opt/homebrew/etc/php/8.1/conf.d
# All .ini files in conf.d will be scanned and loaded

# Example
echo "memory_limit = 512M" > /opt/homebrew/etc/php/8.1/conf.d/memory-limit.ini

php --ini
Configuration File (php.ini) Path: /opt/homebrew/etc/php/8.1
Loaded Configuration File:         /opt/homebrew/etc/php/8.1/php.ini
Scan for additional .ini files in: /opt/homebrew/etc/php/8.1/conf.d
Additional .ini files parsed:      /opt/homebrew/etc/php/8.1/conf.d/ext-opcache.ini,
/opt/homebrew/etc/php/8.1/conf.d/memory-limit.ini

php -i | grep "memory_limit"               
memory_limit => 512M => 512M

Install Apache

brew install httpd
DocumentRoot is /opt/homebrew/var/www.

The default ports have been set in /opt/homebrew/etc/httpd/httpd.conf to 8080 and in
/opt/homebrew/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.

To restart httpd after an upgrade:
  brew services restart httpd
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
brew services restart httpd
httpd -v
Server version: Apache/2.4.52 (Unix)
Server built:   Dec 20 2021 13:35:09

Create httpd-php.conf

vi /opt/homebrew/etc/httpd/extra/httpd-php.conf

# Insert following
DirectoryIndex index.php index.html # Add index.php

# php-fpm default port
<FilesMatch \.php$>
    SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>

Update Apache config

vi /opt/homebrew/etc/httpd/httpd.conf

# Change listen to port 8080
Listen 8080 # Port 80 common used by apps like Skype or Teams, change to other port such as 8080 for convenience.

# Enable following modules by uncomment
LoadModule proxy_module lib/httpd/modules/mod_proxy.so
LoadModule proxy_fcgi_module lib/httpd/modules/mod_proxy_fcgi.so
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

# Append at the end of the conf
# Load php fcgi configuration
Include /opt/homebrew/etc/httpd/extra/httpd-php.conf
brew services start httpd

Test

echo "<?php echo phpinfo();" > /opt/homebrew/var/www/info.php

curl -I http://localhost:8080/info.php

Install MySQL@5.7

brew install mysql@5.7
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

mysql@5.7 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have mysql@5.7 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql@5.7 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/mysql@5.7/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/mysql@5.7/include"


To restart mysql@5.7 after an upgrade:
  brew services restart mysql@5.7
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql@5.7/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql

# Backup existing mysql localtion if needed
ls -la $(which mysql)     
lrwxr-xr-x  1 liow.kitloong  admin  36 Mar 11  2019 /usr/local/bin/mysql -> ../Cellar/mysql@5.7/5.7.25/bin/mysql
# Link mysql to mysql@5.7
brew link mysql@5.7 --force

brew services restart mysql@5.7
mysql -V
mysql  Ver 14.14 Distrib 5.7.37, for osx10.16 (x86_64) using  EditLine wrapper

Install phpMyAdmin

brew install phpmyadmin
To enable phpMyAdmin in Apache, add the following to httpd.conf and
restart Apache:
    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>
Then open http://localhost/phpmyadmin
The configuration file is /opt/homebrew/etc/phpmyadmin.config.inc.php

Update config

vi /opt/homebrew/etc/phpmyadmin.config.inc.php
        
# Extend cookies lifetime
$cfg['LoginCookieValidity'] = 7*24*60*60;

Create phpMyAdmin.conf

vi /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>

Load phpMyAdmin.conf in Apache

vi /opt/homebrew/etc/httpd/httpd.conf
# Append at the end of the conf
# Load phpMyAdmin configuration
Include /opt/homebrew/etc/httpd/extra/phpmyadmin.conf

Restart httpd

brew services restart httpd

Validate

curl -I http://localhost:8080/phpmyadmin

Multi Hosts

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

# Add
$i++;
$cfg['Servers'][$i]['verbose'] = 'Server Name';
$cfg['Servers'][$i]['host'] = '128.0.0.2'; // New host
$cfg['Servers'][$i]['port'] = 3306;
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['AllowNoPassword'] = false;

Prefix credential in config (use at own risk!)

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

$cfg['Servers'][$i]['auth_type'] = 'config'; # Use config

$cfg['Servers'][$i]['user'] = 'username';
$cfg['Servers'][$i]['password'] = 'password';
@engineerdawood
Copy link

Beautiful!!!

@Leepian
Copy link

Leepian commented Jun 30, 2022

Most of the paths are incorrect

@TheodoreClemens
Copy link

on mac your path will be /usr/local/... not /opt/homebrew

@toloekka
Copy link

Most of the paths are incorrect

Its the right path for M1/Silicon Mac's with Homebrew :)

@toloekka
Copy link

on mac your path will be /usr/local/... not /opt/homebrew

Its the right path for M1/Silicon Mac's with Homebrew :)

@toloekka
Copy link

Thanks a lot. This solved the problem for loading phpmyadmin in my browser :)

@realmau5
Copy link

Works like a charm :) Thanks mate!

@1CISP
Copy link

1CISP commented Sep 5, 2022

curl: (7) Failed to connect to localhost port 8080 after 10 ms: Connection refused

@fahzafahmi
Copy link

fahzafahmi commented Sep 11, 2022

hello, any of people here have an idea for phpmyadmin shown up an error such as File not found. error, i'm on macOS Mojave with last patch, and installed everything with homebrew altogether with httpd and php default are replaced with homebrew version, i got my php running perfectly under php fast-cgi FPM

looking forward for the answers.

@ncmochacity
Copy link

Getting a 'site not found error' when pasting: localhost/phpmyadmin in the browser after the instructions on this page. Please advise.

@kitloong
Copy link
Author

@fahzafahmi @ncmochacity

I am not sure, but I would like to suggest checking your phpmyadmin path in your machine, it may use a different path from this manual.
You may browse your directory, or you could get the path hint after executing brew install phpmyadmin.
For instance:

To enable phpMyAdmin in Apache, add the following to httpd.conf and
restart Apache:
    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>
Then open http://localhost/phpmyadmin
The configuration file is /opt/homebrew/etc/phpmyadmin.config.inc.php

@parth391
Copy link

@kitloong, Nice information.

I am using intel processor macbook pro. I am having trouble with phpmyadmin.

my phpmyadmin.conf /private/etc/apache2/other/phpmyadmin.conf

Alias /phpmyadmin /usr/local/share/phpmyadmin
<Directory /usr/local/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>

But when i run curl -I http://localhost:8080/phpmyadmin i get

HTTP/1.1 404 Not Found
Date: Sat, 17 Sep 2022 09:40:43 GMT
Server: Apache/2.4.54 (Unix)
Content-Type: text/html; charset=iso-8859-1

@kitloong
Copy link
Author

Hi @parth391 ,

Just to confirm, have you included the phpmyadmin.conf into your http.conf?
The path may different in different machine.

vi /opt/homebrew/etc/httpd/httpd.conf
# Append at the end of the conf
# Load phpMyAdmin configuration
Include /opt/homebrew/etc/httpd/extra/phpmyadmin.conf
brew services restart httpd

@parth391
Copy link

@kitloong, Yes i have included the path in httpd.conf

@fahzafahmi
Copy link

fahzafahmi commented Sep 29, 2022

@fahzafahmi @ncmochacity

I am not sure, but I would like to suggest checking your phpmyadmin path in your machine, it may use a different path from this manual. You may browse your directory, or you could get the path hint after executing brew install phpmyadmin. For instance:

To enable phpMyAdmin in Apache, add the following to httpd.conf and
restart Apache:
    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>
Then open http://localhost/phpmyadmin
The configuration file is /opt/homebrew/etc/phpmyadmin.config.inc.php

@kitloong hi,

thank you for your explanation toward this matter, i wasn't that carefully to read the whole guided, because i was rearrange my own puzzle by watching someone elses

anyway, i have found a way to get rid 256 error while igniting httpd with homebrew, i was wasted my whole precious time of 6 hours to solve this, although the fixes was as simply as unlink and relink httpd

the 256 error was caused by httpd cannot read error_log file nor another log file, yet it has to be unlinked and linked those httpd again on homebrew version, in order to get it working again

yeah it is, i think that's all.

@Taufi
Copy link

Taufi commented Feb 4, 2023

Great. Thanks.

@gabrielfreirebraz
Copy link

Awesome! Thank you so much !!

@rynaberey
Copy link

läuft bis auf phpmyadmin .. perfect!

@wsmr
Copy link

wsmr commented May 31, 2023

php -v

PHP 7.4.33 (cli) (built: Apr 26 2023 18:36:16) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

httpd -v

Server version: Apache/2.4.57 (Unix)
Server built: Apr 6 2023 18:16:37

mysql -V

mysql Ver 8.0.33 for macos13 on arm64 (MySQL Community Server - GPL)

brew services info php@7.4

php@7.4 (homebrew.mxcl.php@7.4)
Running: ✔
Loaded: ✔
Schedulable: ✘
User: sahan
PID: 1693

brew services restart httpd

Stopping httpd... (might take a while)
==> Successfully stopped httpd (label: homebrew.mxcl.httpd)
==> Successfully started httpd (label: homebrew.mxcl.httpd)

brew services info httpd

httpd (homebrew.mxcl.httpd)
Running: ✘
Loaded: ✔
Schedulable: ✘

@kitloong please give me some advice to fixed this issue 🙏🙏🙏🙏

( If I replaced the default httpd.conf its running but phpmyadmin not working as follows
http://localhost:8080/phpmyadmin/

Not Found
The requested URL was not found on this server.

so what are the exact changers do I need to do )

Fixed

brew reinstall httpd
brew reinstall phpmyadmin 
brew cleanup httpd
brew cleanup phpmyadmin
  • Create httpd-php.conf
  • Update Apache config

@picasso091
Copy link

thanks. works perfectly for Mac M1.

@rajeshstarlite
Copy link

To the point, smooth. Thank you so much !!

@SmokerCreep
Copy link

Many thanks you mad my day! Works on M1! Needed a solution for MySql 8 that not comes witm MAMP.
Maybe a hint for newbees if you start phpmyadmin & MySQL in the basic configuration, you can not log in, because user root without password. therefore in the hpmyadmin.config.inc.php still enter:
$cfg['Servers'][$i]['AllowNoPassword'] = TRUE;

if (!empty($dbname)) {
// other configuration options
$cfg['Servers'][$i]['AllowNoPassword'] = TRUE;
// it should be placed before the following line
$i++;
}

Thanks a lot!!!

@wxx1764753522
Copy link

curl: (7) Failed to connect to localhost port 8080 after 10 ms: Connection refused

Append at the end of the conf

Load php fcgi configuration

Include /opt/homebrew/etc/httpd/extra/httpd-php.conf
brew services start httpd

If you use "brew services start httpd" it is not necessary

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