Mac OS X Apache MySQL PHP PhpMyAdmin Set Up

Published: April 2, 2013

I recently started a new job and was given the opportunity to choose my work computer. The rest of the development team was using Mac and as of this writing we’ll be using OS X 10.8 Mountain Lion. The problem for me is that I’ve always used Windows. However, I’ve heard great things about Apple, plus I have an iPhone, so I thought… why not? The rest of this post details how I set up my new MacBook Pro for my development stack including Apache, MySQL, PHP, phpMyAdmin, etc.

NOTE: This article was updated on December 12th, 2013 after upgrading my MacBook Pro to Mavericks

I’ve learned a little bit about MacPorts and Homebrew and I understand that these can be very helpful package managers. Unfortunately, when I tried using them it was hard for me to understand where everything was going and how things “magically” worked. Therefore, I decided to do all of the installation myself manually. I felt more in control and had better knowledge of how everything was hooked up and where things were.

There are a few articles that I found online that were very helpful and I’ll list them here for reference:

The rest of this post basically goes through the 4 links above with some modifications.

/usr/local

Ensure that the following directories exist. If not, create them. See man mkdir for the -p flag on the last command in this list for more info.

  • sudo mkdir /usr/local/include
  • sudo mkdir /usr/local/bin
  • sudo mkdir /usr/local/lib
  • sudo mkdir -p /usr/local/man/man1

MySQL

  1. Download the “Mac OS X ver. 10.7 (x86, 64-bit), DMG Archive” for MySQL 5.6.x from mysql.com and install the pkg, the MySQL.StartupItem, and the MySQL.prefPane.
  2. Add /usr/local/mysql/bin to the path: nano ~/.bash_profile and add:
    export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:$PATH

    at the top of the file.

  3. source ~/.bash_profile and/or quit terminal and reopen
  4. At this point you should be able to reopen terminal and type mysql -v which will list the mysql version and open the mysql prompt. Type \q to quit out of mysql and return to your home terminal.
  5. Set up MySQL root password
    mysqladmin -u root password 'new-password'

    where new-password is your password of choice. Use single ‘quotes’ surrounding your new-password. Clear the history by typing history -c so that ‘new-password’ isn’t in plain text on the disk.

  6. Now ensure that the mysql.sock file can be found by PHP (fixes the 2002 socket error):
    1. Ensure that MySQL is running: mysql -v then quit \q
    2. sudo mkdir /var/mysql
    3. sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Apache

Apple has Apache is installed by default. We just have to use the command line now to start and stop it as well as do a little bit of configuration to fit our needs.

  1. cd /etc/apache2
  2. Give write permission the config file to root: sudo chmod u+w httpd.conf
  3. sudo nano httpd.conf
  4. Find #LoadModule php5_module libexec/apache2/libphp5.so and remove the leading #
  5. Find AllowOverride None within the <Directory “/Library/WebServer/Documents”>section and change toAllowOverride All so that .htaccess files will work.
  6. Change all instances of Document Root from /Library/WebServer/Documents/ to /Users/yourusername/Sites/
  7. Change permissions back: sudo chmod u-w httpd.conf
  8. Restart Apache: sudo apachectl restart
  9. Open Finder and navigate to /Users/yourusername/Sites/ Note: You may have to create this directory if it doesn’t already exist.
  10. In your Sites directory, create a new file called phpinfo.php with <?php phpinfo(); inside it.
  11. Use Firefox to navigate to http://localhost/phpinfo.php and check that the PHP version is displayed (5.3.15 at the time of writing).
  12. Ensure that Apache will start after a reboot:
    sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

php.ini

  1. cd /etc
  2. Copy the default php.ini file to one that you continue to modify to suit your own needs now and in the future: sudo cp php.ini.default php.ini
  3. Change write permissions on this file: sudo chmod ug+w php.ini
  4. Begin editing your php.ini file and change the following: sudo nano php.ini
    1. error_reporting = E_ALL | E_STRICT
    2. display_errors = On
    3. html_errors = On
    4. extension_dir = “/usr/lib/php/extensions/no-debug-non-zts-20100525”
    5. Change all instances of/var/mysql/mysql.sock to /tmp/mysql.sock
    6. date.timezone = UTC or whatever timezone you prefer
    7. post_max_size = 64M
    8. upload_max_filesize = 64M
  5. Save the php.ini file in nano with “Ctrl + x”, “Y” to save changes, and “Enter” to write to same filename.
  6. Restart Apache: sudo apachectl restart

Xdebug

The four stages of Xdebug are 1) What is xdebug? 2) How do I install it? 3) Huh, this is neat 4) OMG I CAN’T LIVE WITHOUT THIS! Apple must agree because Mountain Lion ships with it.

  1. sudo nano /etc/php.ini
  2. Find the line:
    ;zend_extension=”/usr/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so”
    and uncomment it by removing the semicolon at the start. If that line is not already in your php.ini file then add it to the end of the file.
  3. If you want to configure your Xdebug settings then scroll to the end of the file and look for the [xdebug] section. I like these settings but you can configure it however you want:

    xdebug.idekey=”phpstorm-xdebug”
    xdebug.remote_enable=1
    xdebug.remote_port=”9000″
    xdebug.var_display_max_children = 999
    xdebug.var_display_max_data = 99999
    xdebug.var_display_max_depth = 100
  4. Restart Apache: sudo apachectl restart and check your phpinfo page that we created when setting up Apache to verify that Xdebug is now loaded

See also my post on using Xdebug with PhpStorm.

PEAR

NOTE: If you installed PEAR with previous version of OS X then you may only need to update php.ini in step #8.
However, it never hurts to upgrade all.
  1. cd /usr/lib/php
  2. sudo php install-pear-nozlib.phar
  3. pear config-set php_ini /private/etc/php.ini
  4. pecl config-set php_ini /private/etc/php.ini
  5. sudo pear channel-update pear.php.net
  6. sudo pecl channel-update pecl.php.net
  7. sudo pear upgrade-all
  8. Edit /etc/php.ini, search for include_path, uncomment it by removing the semicolon, and add /usr/lib/php/pear so that the line looks like include_path = “.:/php/includes:/usr/lib/php/pear”

phpMyAdmin

NOTE: If you installed phpMyAdmin with previous version of OS X then no need to do this again.
  1. Download phpMyAdmin, the all-languages.tar.gz package, uncompress and move the folder with its contents into your document root renaming folder to ‘phpmyadmin’. /Users/yourusername/Sites/phpmyadmin/
  2. cd ~/Sites/phpmyadmin
  3. mkdir config
  4. chmod o+w config
  5. Run setup in your browser: http://localhost/phpmyadmin/setup/
  6. You need to create a new localhost MySQL server connection. Click New Server

    New Server

  7. Switch to the Authentication tab and set the local MySQL root user and the password. Add in the username “root” and the password that you set up earlier for the MySQL root user set up. Click on save and you are returned to the previous screen. NOTE: This is not the OS X Admin or root password, it is the MySQL root user.

    Set New Server MySQL Password

  8. Make sure you click on “Save.” A config.inc.php is now in the /config directory of your phpmyadmin directory. Move this file to the root level of /phpmyadmin and then remove the now empty /config directory.

    Save Config

  9. Now going to http://localhost/phpmyadmin/ will now allow you to interact with your MySQL databases.
  10. To upgrade phpmyadmin just download the latest version and copy the older config.inc.php from the existing directory into the new folder and replace. Be sure to keep a backup the older one just in case.

mcrypt

After installing phpMyAdmin you may get the following warning… “The mcrypt extension is missing.” Complete the following procedures to fix this warning.

  1. Xcode
    1. Get the free download version of Xcode from the Apple app store. Install it to your /Applications folder. It will be called ‘Install Xcode’ or just Xcode.app. You need this to use the compilers to compile the source code that you will download.
    2. Go to Xcode preferences and then look in the ‘Downloads’ button.
    3. Install the command line tools from the preferences of Xcode.

      Xcode Command Line Tools

  2. Getting mcrypt on OS X Mountain Lion
    1. Open a Terminal window to work in
    2. Change to home account directory: cd ~
    3. Make a directory that you will work in: mkdir mycrypt
    4. Change to the directory that you just created: cd mcrypt
    5. Download libmcrypt 2.5.8 from Sourceforge
    6. Get the PHP code in a tar.gz or .bz2 format for the version of PHP that you are currently running. See your phpinfo.php file to be sure or you can type php -v at the command line of your Terminal.
    7. Move both of these files that you downloaded into your working directory (mcrypt in this instance) and go back to Terminal
    8. Expand the first file: tar -zxvf libmcrypt-2.5.8.tar.gz
    9. Expand the second file: tar -zxvf php-5.4.17.tar.gz
    10. Remove the compressed archives: rm *.gz
    11. Any errors on the command line including C++ and g++ mostly are due to Xcode not being installed or the command line tools missing. If you have Xcode but still get the errors, launch Xcode > Preferences > Downloads > Install ‘Command Line Tools’
  3. Configuring libmcrypt
    1. Change directory into libmcrypt: cd libmcrypt-2.5.8
    2. Libmcrypt needs to be configured. Enter: ./configure
    3. make
    4. sudo make install
  4. Autoconf errors
    1. If the below error occurs after you try the following compile of mcrypt, then autoconf is not installed.
      Cannot find autoconf. Please check your autoconf installation and the
      $PHP_AUTOCONF environment variable. Then, rerun this script.
      

      Autoconf is not installed with the latest Xcode but may be present on some OS X from an upgraded older Xcode.

      If you need autoconf – some more Terminal heavy lifting:

    2. cd ~/mcrypt
    3. curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
    4. tar xvfz autoconf-latest.tar.gz
    5. cd autoconf-2.69/
    6. ./configure
    7. make
    8. sudo make install
  5. Compile mcrypt PHP extension
    1. cd ../php-5.4.17/ext/mcrypt/
    2. /usr/bin/phpize
    3. Output should be:
      Configuring for:
      PHP Api Version: 20100412
      Zend Module Api No: 20100525
      Zend Extension Api No: 220100525
    4. ./configure
    5. make
    6. sudo make install
    7. The result of this should be:
      Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20100525/
  6. Enabling mcrypt.so PHP extension
    1. Open /etc/php.ini and add the line below at the end: extension=mcrypt.so
  7. Restart Apache
    1. sudo apachectl restart
    2. That’s it! Check your phpMyAdmin login page or browse to your phpinfo.php to see if it loaded correctly. If it didn’t load then you may need to declare the extensions directory in /etc/php.ini
      extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20100525/"

Wrapping Up

Well, that’s about it. We’ve covered a lot and hopefully you have your local web development stack up and running. There’s lots left to do including setting up your IDE, version control, etc. See my other post, recovering from the computer crash, for other items on the list to set up when starting with a new computer or a fresh OS install.

Did I miss anything? What else do you do when configuring your local web server stack?

One Response to “Mac OS X Apache MySQL PHP PhpMyAdmin Set Up”

  1. Grateful

    I just want to say thank you so much. This is exactly what I’ve been searching for for two days now :). I still have one problem though. After downloading and installing the crypt files the phpmyadmin is still saying that the extension is missing. I also cannot find the mcrypt.so file or module or whatever it’s supposed to be (sigh i’m not a great programmer yet). Suffice it to say that everything else seems to work properly so far. Is mcrypt necessary? Also, i get another message from phpmyadmin saying that the configuration storage is deactivated. I’ve uncommented the code lines but i get no change.

    Thanks again anyway. I will figure it all out soon enough. This was fun.

Leave a Reply

Home · About · Domains · Hosting · Development · Portfolio · Blog · Contact · Legal Disclaimer · Privacy Policy

Copyright © 2018 · Lively Web Design