Using MAMP on a MAC

When learning PHP you will want to test PHP files on your computer; this is called running the PHP pages locally. To do this you need to install some software on your computer.

Testing files locally is much faster than trying to create and edit files on a remote server run by a hosting company. This page shows you how to install that software and the sample code for our book.

Why Use MAMP

To view PHP pages that are stored on your Mac in your web browser, your Mac needs to be running a piece of software called a web server that supports PHP.

The web server's job is to find the page you want, run the PHP code, then send an HTML page to your web browser. We recommend that you use a web server called Apache.

To create database-driven websites on your Mac, you also need database software such as MySQL or MariaDB installed on your computer.

Rather than installing and configuring this software yourself, we recommend that Mac users get a program called MAMP.

  • MAMP is free.
  • It installs and configures Apache and MySQL for you.
  • And it provides an interface to help you use them.

The MAMP interface is really helpful because neither Apache nor MySQL have a graphical user interface of their own.

How to Install MAMP on a Mac

To install MAMP on your Mac go to: https://mamp.info and click on the Free download button.

Screenshot showing home page of MAMP website

Next, click on the Apple logo to start the download.

Screenshot of MAMP site where you select your operating system.

Once the download has completed, go to your Downloads folder and double-click on the package file to start the installation.

Screen shot of Finder displaying the downloaded package file

Follow the instructions to install MAMP.

Screen shot of the MAMP installer running

In your Applications folder you should see:

  • A folder called MAMP, which contains a free version of MAMP
  • A trial version of MAMP Pro (which you have to pay for)

Open the MAMP folder and click on the MAMP icon to start MAMP.

MAMP needs to be running when you try to view any PHP page that is stored on your computer, so you might like to keep it in the Dock.

Screen shot of Finder displaying the MAMP folders and app in the Applications directory

The MAMP Interface

  • Screen shot of MAMP interface
  • MAMP interface with start button highligted in top right corner
  • MAMP interface with the name of the server highlighted
  • MAMP interface with document path to the root folder highlighted
  • MAMP interface with the options of web server highlighted - Apache is selected
  • MAMP interface with drop down selector for version of PHP set to 8

Where to Save PHP Files

The MAMP interface shows you the path to the folder called the document root folder. When you are first learning to create websites using PHP, you should save all of your PHP pages in this folder. Also put any other files that the browser might request in this folder (such as images, CSS and JavaScript files).

When using MAMP, the default location of the document root folder is Applications/MAMP/htdocs (although you can select another folder using the Preferences option in the top-left hand corner of the MAMP interface, and selecting the Server tab).

Running the Sample Code

1. Click here to download the code files for the entire book.

2. Move the phpbook folder into your document root folder (the default location is Applications/MAMP/htdocs).

3. Enter the following URL into your browser's address bar: http://localhost:8888/phpbook/

Once you have loaded this page, you can click on the links to move between the example files.

NOTE: You cannot open a PHP file in your browser using the browser's File > Open menu option to open a file, or by dragging it onto a browser like you would with an HTML page.

Understanding Local URLs

The URL is made up of the following parts:

  • http://
  • localhost
    host name
  • :
  • 8888
  • /
  • phpbook/index.php