MAMP is Mac OS X package consisting of an Apache HTTP server, MySQL database, PHP interpreter, and some libraries. The word "MAMP" is an acronym, where "M" stands for "Macintosh", meaning it's designed for Mac OS X, and the other letters stand for the package components.
Mac OS X comes with pre-installed PHP interpreter and Apache server, so it can be configured to work well together following the tutorial published by php.net. PhpStorm-related configuration is similar to the one described in this tutorial. Installing additional packages and configuring the system environment on your own can be tricky, so this guide describes how to get everything set up at once easily in a separate environment with complete MAMP package.
MAMP is a reliable and fast way to set up an environment for PHP programming. It provides all the components required for developing, running, debugging, and unit testing of PHP applications. MAMP is a good alternative to installing and configuring a Web server, a PHP engine, a database server, and a debug engine separately. All you need to do to start developing is download MAMP, extract the archive, and start the components using the MAMP control panel.
- Choosing the right MAMP edition
- MAMP Control Panel
- Integrating MAMP with the IDE
Before starting, the following tools should be available on your system:
- Either PhpStorm or IntelliJ IDEA Ultimate Edition
- When working with IntelliJ IDEA:
Choosing the right MAMP edition
From the MAMP web site, we can download and install the MAMP edition which matches the Mac OS X version. The following versions are available:
- MAMP & MAMP PRO 2.* (Latest version is 2.1.4) is suitable for Mac OS X 10.6.6+ (including Mac OS X 10.8).
- MAMP & MAMP PRO 1.* (Latest version is 188.8.131.52) is suitable for Mac OS X 10.4+.
It's also possible to download additional PHP versions for MAMP PRO 2.1.4.
MAMP is free software distributed under GNU license. MAMP PRO is a paid version which includes additional features. MAMP can be used stand-alone without MAMP PRO.
In this tutorial, we will use the latest MAMP 2.1.4 version.
Downloading the MAMP installer
From the MAMP downloads page, we can find a list of components included in the package. Download link for the last stable version is on top of the page.
We will be using the pkg version (in zip archive). There is also source code available in zip archive and svn repository.
Once the download of the package is completed, we can run it.
Installing the MAMP package
Once the MAMP archive is downloaded, unpack it and run the installer (MAMP_2.1.4.pkg in our case).
The MAMP installation wizard is pretty straightforward. Using the Continue button, we can navigate through all steps in the installation.
Click the Close button to complete installation.
MAMP Control Panel
After installation, we can use the MAMP Control Panel to perform several actions such as starting / stopping servers and changing configurations. MAMP Control Panel Application can be found in Applications/MAMP folder.
Starting and stopping servers
To start Apache and MySQL servers, click the Start Servers button. To stop running servers, we can use the Stop Servers button.
You can configure MAMP clicking Preferences... button. There are various options available:
- PHP version and caching options on PHP tab
- Start/Stop Servers configuration on Start/Stop tab
- Apache and MySQL ports on Ports tab
- Apache document root on Apache tab
Let's start servers by clicking the Start Servers button. Icons on the left will get green and Start Page will be opened (http://localhost:8888/MAMP/ by default). The default page contains information about your environment and useful links.
Integrating MAMP with the IDE
Now that we have installed MAMP and launched its components, we need to tell our IDE where these components are stored and how they are configured.
Integrating the PHP executable
Let's start by registering the PHP executable from MAMP in PhpStorm. When in a PHP project, we can do this using the File | Settings menu (Cmd+Shift+S), navigating to the Project Settings | PHP.
With IntelliJ IDEA and PhpStorm, we can have several separate PHP interpreters registered in the IDE, depending on the PHP version and/or PHP configuration needed for the project. All currently registered installations of PHP interpreters are displayed in the Interpreter drop-down list:
We can add the one we've installed with MAMP by clicking the Browse button (…). This will bring up the Interpreters dialog box.
- In the left-hand pane, click the Add (+) button on the toolbar.
- In the Name text box, type the name to identify the current installation, for example PHP from XAMPP.
- In the PHP Home text box, specify the folder where the PHP executable file php is stored. We can enter the path manually or use the Browse button to find the path on our system.
- The IDE checks whether the specified folder contains a PHP executable file, detects the PHP version, and displays it in the PHP Info read-only field. Starting with PhpStorm v7.0, the IDE also validates that the debugger is configured correctly.
We can now save these settings and select the newly added PHP interpreter for our project:
Our IntelliJ IDEA or PhpStorm IDE will now make use of the PHP installation that was bundled with MAMP.
Integrating the debugger
PhpStorm offers a powerful debugger in which breakpoints can be set and variables can be inspected at runtime. For more information on using PhpStorm's debugger, have a look at any of the following articles:
- Zero-configuration Web Application Debugging with Xdebug and PhpStorm
- Xdebug Installation Guide
Once you have installed and configured the debugger, you can validate your debugging configuration by using the tutorial.
Integrating the Apache server
Interaction between IntelliJ IDEA or PhpStorm with Web, FTP, and other servers is supported through the Remote Hosts Access plugin, which is enabled by default for IntelliJ IDEA and PhpStorm. The IDE accesses servers using connection settings specified in the registered server access configurations. These configurations are created and managed in the Project Settings | Deployment page of the Settings dialog box.
Choose File | Settings on the main menu again to open the Settings. Click Deployment under the Project Settings node to go to the Deployment page.
From the toolbar, click the Add button to add a new server. From the Add Server dialog, we can specify a name for our MAMP server and select the deployment type. For MAMP Apache server, select the Local or mounted folder type.
After clicking OK, we can specify the folder to Apache's web root (/Applications/MAMP/htdocs) and the URL to the web server, http://localhost:8888. Note that these settings may be different depending on how you have configured the MAMP Apache server in the MAMP configuration or Apache configuration files.
Using the Open button, we can verify that settings are OK. Once done, we should see the MAMP htdocs folder.
Integrating the MySQL server
IntelliJ IDEA and PhpStorm allow us to connect to the MAMP MySQL database server using the Database plugin (installed and enabled by default). First of all, make sure the MySQL server is running from the MAMP Control Panel. When using MAMP PRO, also make sure that the "Allow local access only" setting is disabled from the MAMP PRO Control Panel.
Next, from the right-hand side of our IDE, we can open the database tool window.
Opening the database pane, we can create a new connection or Data Source by using the context menu. We'll have to specify the JDBC database driver to be used to connect to our database. For MySQL, we can use the MySQL Connector driver available in the list of drivers. PhpStorm doesn't ship these drivers but a simple click (on Click here) fetches the correct JDBC driver from the Internet.
Next, we'll have to enter our connection details. As the JDBC driver class, select the com.mysql.jdbc.Driver driver. The Database URL should be a connection string to our MySQL Database, for example jdbc:mysql://localhost:8889/ or jdbc:mysql://localhost:8889/foodb to connect to the foodb database. We can also specify the username and password for MySQL, root for both username and password by default in MAMP.
When using UTF-8 or unicode characters in the username, password or database name, two additional parameters must be added to the connection string: characterEncoding and useUnicode. An example connection string would be jdbc:mysql://localhost:8889?characterEncoding=utf8&useUnicode=true. It may also be required to select a different JDBC driver from the list, com.mysql.jdbc.NonRegisteringDriver.
Using the Test Connection button, we can verify the settings and make sure PhpStorm can connect to MySQL.
We can now create tables, run queries, update data and so on. See this blog post for more information.