site en fran├žais

2012, May 19

Config MAMP : php/mysql/phpmyadmin and Mac OS X

Edit 01/12/2013 : Think about it twice before installing MAMP. The website migrated from php/mysql to Python/Jinja2 with free hosting from Google AppEngine. Taking a step back from this blog post, I realize how difficult it is to work with Apache/MySQL. It is with a great relief that we moved out. If you just need phpMyAdmin to read local MySQL databases, use a dedicated free software, no need of MAMP. SQLite is even better if you have the time to slightly convert your data. And if you like Python and you have veryl ittle php code, give up on php.

If you are on Mac OS Lion and you try to get MAMP fully functional, you are at the right place, either because you don't know much and just want to have a local php/MySQL local website or because you messed up a lot of config files trying to migrate from 10.6 to 10.7. This is the reason of the existence of this tutorial. I couldn't get MAMP working properly, I didn't want to move my websites from ~/Sites to MAMP/htdoc and symlinks were failing. After reading hundreds of good and bad tutorials, here is a (hopefully) good way to get everything set up on your machine.

Summary of what we are going to do, before getting into the nitty gritty

  • Forget about MAMP. It is not the core of php/mysql. It is a mere wrapper that turns them on/off. It was really convenient before 10.7, but now they have no widget, we have to move our websites to their htdoc directory, ...
  • Apache is already installed on your Mac. Let's take advantage of it !
  • We will just install few missing modules to Apache, MySQL and phpMyAdmin.
  • Before doing anything, export your current databases as text files (SQL commands) and keep them in a safe place.


Install Homebrew : /usr/bin/ruby -e "$(/usr/bin/curl -fksSL". This is a utility similar to MacPorts. Many other tutorials to solve MAMP problems ask you to use MacPorts but it always broke somewhere for me and I always had a few dependencies missing.

Installation of MySQL

brew install mysql
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Initialisation of MySQL with username "root" and password "root" (very common).

mysql.server start mysqladmin -u root password root
mysqladmin -u root -h localhost
mysql.server start

Installation of Apache/php

This is optional, do it only if nothing works fine and you changed apache config files without knowing what you were doing (don't be ashamed, anyone does that, there is no need to try to understand what's really happening when typing all these commands).

rm -f ~/.pearrc
rm -rf ~/.pear
brew unlink php
brew install autoconf
sudo chmod a+w /usr/local/share/emacs/
sudo chmod a+w /usr/local/share/emacs/site-lisp/
brew link autoconf
brew tap josegonzalez/homebrew-php
brew install php --with-mysql --with-apache
open ~/.bashrc
Ajoutez ces deux lignes puis sauvegardez le fichier
alias pear="php /usr/lib/php/pear/pearcmd.php"
alias pecl="php /usr/lib/php/pear/peclcmd.php"
source ~/.bashrc
pear config-set auto_discover 1
pear update-channels
pear upgrade
sudo pecl install apc-beta mongo memcachepear install
sudo pear install PHP_CodeSniffer-beta
sudo pear install
brew link php

Modification of Apache's config

This is not optional.

Now that lots of stuff are installed, we need to tell Apache that our home directory for websites is ~/Sites, tell him to accept php5 and few other things.

sudo chmod a+w v /etc/apache2/httpd.conf
open /etc/apache2/httpd.conf

This opens a file. I advise that every time you change a line in a config file, you make a copy of the original line that you are modifying and comment it out just above your modification.

Same thing with /etc/php.ini :
sudo chmod a+w /etc/php.ini open /etc/php.ini

The 2 last changes will enable you to use phpMyAdmin with databases larger than 2 or 8 Mo.

Installation of phpMyAdmin

phpMyAdmin is like a little local website that helps you create, modify, ... your MySQL databases.

brew install phpmyadmin
mkdir /usr/local/share/phpmyadmin/config
sudo chmod o+w /usr/local/share/phpmyadmin/config

Create file :
open /etc/apache2/other/mod_phpmyadmin.conf

# changeThomas: empty file before
AliasMatch ^/phpmyadmin(?:/)?(/.*)?$ "/usr/local/share/phpmyadmin$1"

  Options -Indexes
  AllowOverride None
  Order allow,deny
  Allow from all

  LanguagePriority fr en es de ja ko pt-br ru
  ForceLanguagePriority Prefer Fallback

phpMyAdmin needs libmcrypt (a php library) to run. Sorry for this fastidious part, even though almost optional, if you really can't make this part work move on things are gonna be ok but you will have ugly warnings in phpMyAdmin.

DownloadLibmcrypt and the last version of php.

Make a directory called SourceCache on your desktop and put them the 2 zip files and unzip them there.

Install Xcode 4.1 tools from the App Store.

cd ~/Desktop/SourceCache
MACOSX_DEPLOYMENT_TARGET=10.7 CFLAGS='-O3 -fno-common -arch i386 -arch x86_64' LDFLAGS='-O3 -arch i386 -arch x86_64' CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64' ./configure --disable-dependency-tracking
cd libmcrypt-2.5.8
MACOSX_DEPLOYMENT_TARGET=10.7 CFLAGS='-O3 -fno-common -arch i386 -arch x86_64' LDFLAGS='-O3 -arch i386 -arch x86_64' CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64' ./configure --disable-dependency-tracking
make -j6
sudo make install
cd ../php-5.3.6/
cd ext/mcrypt/
MACOSX_DEPLOYMENT_TARGET=10.7 CFLAGS='-O3 -fno-common -arch i386 -arch x86_64' LDFLAGS='-O3 -arch i386 -arch x86_64' CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64' ./configure --with-php-config=/Developer/SDKs/MacOSX10.7.sdk/usr/bin/php-config
MACOSX_DEPLOYMENT_TARGET=10.7 CFLAGS='-O3 -fno-common -arch i386 -arch x86_64' LDFLAGS='-O3 -arch i386 -arch x86_64' CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64' ./configure --with-php-config=/usr/bin/php-config
make -j6
sudo make install

Configure phpMyAdmin

phpMyAdmin has just one little drawback : it asks for your password everytime you turn it on and then every 10 minutes. If you are the only one using your computer the best is to deactivate this.

Open /usr/local/share/phpmyadmin/ and be sure you have these lines:

$cfg['Servers'][$i]['verbose'] = '';
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['port'] = '';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Servers'][$i]['AllowNoPasswordRoot'] = true;

Time to play

You should be (almost) all set. Let's run everything now. For your website to run locally, you need to 1) start the apache server and 2) run a mysql server (if your website needs).

Apache can be started via Preferences Pane -> Sharing -> tick "web sharing", or via command line : sudo /usr/sbin/apachectl start (or restart or stop)

MySQL can be run with mysqld &

You can of course ask your system to fire them at start up time but your computer will be slightly slower to boot. If you want, "open /System/Library/LaunchDaemons/org.apache.httpd.plist" and throw this config in:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

Watchout, mysqld fails to start when you didn't stop it before turning off your computer. You must open the activity monitor and quit it, or "ps | grep mysqld | grep -v 'grep' | cut -d" " -f3 | xargs kill {}; mysqld &" to kill and re-run it. Time to make an alias in your .bashrc !

Once this is all done, you should be able to access http://localhost/phpmyadmin/index.php and your websites !

If something is not working / I forgot a step, please don't hesitate to post a message here.

BleezWorld can perform missions for you. Consult us.