wiki:howto/MAMP

<- Back to the HOWTO section

Install Apache, MySQL, PHP 5.6 and phpMyAdmin on macOS

These installation instructions are written for the following versions:

  • Apache 2.4.* - See: howto/Apache2 for instructions on installation of Apache2
  • MySQL 8.* - See: howto/MySQL for instructions on installation of MySQL
  • PHP 5.6.* - See: howto/PHP for instructions on installation of PHP

Simplified sequence:

  • Install Apache
  • Install MySQL
  • Install PHP
  • Modify Apache Configuration to support MySQL and PHP.

Integrate PHP with Apache

If this is your first install, you need to enable php56-apache2handler in your web server.

To enable the php56-apache2handler, run:

Register PHP with Apache

$ cd /opt/local/lib/apache2/modules
$ sudo /opt/local/bin/apxs -a -e -n php5 mod_php56.so
$ # or, if you're using php7...
$ sudo /opt/local/bin/apxs -a -e -n php7 mod_php74.so
$ #  and for php8... it's slightly different:
$ sudo /opt/local/bin/apxs -a -e -n php mod_php82.so

This should return a message like:

[activating module `php5' in /opt/local/etc/apache2/httpd.conf]

Update Apache’s httpd.conf - /opt/local/etc/apache2/httpd.conf - file to enhance the "DirectoryIndex" directive to include additional "index" files. Search for:

DirectoryIndex index.html

and change it this way:

DirectoryIndex index.php index.html

Verify that at the end of the httpd.conf file the following lines exist so that Apache includes the mod_php "AddType" configurations

# Include PHP configurations
Include etc/apache2/extra/mod_php56.conf
# Or...
Include etc/apache2/extra/mod_php74.conf
# Or...
Include etc/apache2/extra/mod_php82.conf

Verify that in the Dynamic Shared Object (DSO) Support section the following have been added.

# Load the PHP module
LoadModule php5_module modules/mod_php56.so
# Or...
LoadModule php7_module modules/mod_php74.so
#
# And again notice the difference for php82:
LoadModule php_module  modules/mod_php82.so

Note: either of the above two edits are only required if the lines are not present in the httpd.conf file, as the apxs command (executed above) will add those for you.

Step 3: MySQL setup for PHP

Setup the MySQL default socket to use the MacPorts configuration (/opt/local/var/run/mysql8/mysqld.sock)

$ sudo -i
# cd /opt/local/etc/php56
# cp php.ini php.ini.bak
# defSock=$(/opt/local/bin/mysql_config --socket)
# cat php.ini | sed \
  -e "s#pdo_mysql\.default_socket.*#pdo_mysql\.default_socket=${defSock}#" \
  -e "s#mysql\.default_socket.*#mysql\.default_socket=${defSock}#" \
  -e "s#mysqli\.default_socket.*#mysqli\.default_socket=${defSock}#" > tmp.ini
# grep default_socket tmp.ini  # Check it!
# mv tmp.ini php.ini
# exit # OR rm php.ini.bak && exit

If you are unfamiliar with multiline Terminal commands like the cat command above: Each line ends with a backslash. Give Enter after the backslash and you will enter the next line.
To make things easy: Copy the first line cat php.ini | sed \ with Command C and past it in the Terminal with Command V. Hit Enter and copy and paste the next line, etc...

The "grep" check should return:

pdo_mysql.default_socket=/opt/local/var/run/mysql8/mysqld.sock
mysql.default_socket=/opt/local/var/run/mysql8/mysqld.sock
mysqli.default_socket=/opt/local/var/run/mysql8/mysqld.sock

For proper setup of MySQL for PHP all three lines must be present. If there is one missing, for example mysql.default_socket=/opt/local/var/run/mysql8/mysqld.sock,
open the php.ini file in your editor and add somewhere the line 'mysql.default_socket='. Next repeat the cat command above but only for the missing part.
'Grep' should show now the three required lines.

Restart Apache so that your changes take effect

$ sudo port unload apache2
$ sudo port load apache2

Step 4: Create phpinfo

This is used to test the configuration after you have integrated it with Apache and MySQL.

Create in Apache's DocumentRoot a file named phpinfo.php that contains the following line

<?php phpinfo(); ?>

In case you don't remember, your Apache "DocumentRoot" is either the default directory "/opt/local/www/apache2/html" or your own user "Sites" directory if you activated user directories as specified above. Point your browser to http://localhost/phpinfo.php (or http://localhost/~username/phpinfo.php as applicable) and verify that the correct version of PHP is active and that MySQL support is active (you may want to search the page for "mysql").
Note that this file needs to be readable and executable.

  • Remember to return to howto/PHP to complete the PHP installation.
Last modified 9 months ago Last modified on Jun 13, 2023, 2:50:59 PM