Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Setting up PHP & MySQL on OS X Yosemite

DZone's Guide to

Setting up PHP & MySQL on OS X Yosemite

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

It's that time again; Apple has shipped a new version of OS X, 10.10 Yosemite. Apple ships PHP 5.5.14 with Yosemite and this is how to set it up from a clean install.

However, if you don't want to use the built-in PHP or want to use version 5.6, then these are some alternatives:

Let's get started…

Homebrew

Homebrew is a package manager for OS X. Install it, as we'll need it later.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

You'll also need the Xcode command line tools – at least version 6.1. Usually you would install using
xcode-select --install, however, as of this writing Xcode 6.1 isn't available on the Mac App Store, so use this direct link. Don't forget to start up Xcode…

Note: if /usr/include doesn't exist, then you need to do:

sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include /usr/include

MySQL

  1. Download the "Mac OS X 10.9 (x86, 64-bit), DMG Archive" from mysql.com and install the pkg.
  2. Open the pref pane and start the MySQL Server.
  3. Update the path by editing ~/.bash_profile and add:
    export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:$PATH

    at top of file.

  4. Set up MySQL root password:
    mysqladmin -u root password {new-password}
    mysqladmin -u root -p{new-password} -h localhost password {new-password}
    mysqladmin -u root -p{new-password} reload

    Clear the history file by typing history -c so that {new-password} isn't in plain text on the disk.

  5. Now ensure that the mysql.sock file can be found by PHP:
    1. Ensure that MySQL is running
    2. sudo mkdir /var/mysql
    3. sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Apache

In Yosemite, Apache 2.4.9 is installed. We just have to use the command line to start and stop it.

One change compared to 2.2.x worth noting is that we now need the Require all granted directive in our virtual host definitions in place of Allow from all. Read the upgrading documentation for more information.

  1. cd /etc/apache2
  2. Edit /etc/apache2/httpd.conf
    1. To enable PHP and rewriting in Apache, remove the leading # from these two lines:
      #LoadModule rewrite_module libexec/apache2/mod_rewrite.so
      #LoadModule php5_module libexec/apache2/libphp5.so
    2. Find the <Directory "/Library/WebServer/Documents">section and change:
      AllowOverride None to AllowOverride All so that .htaccess files will work
      and Options FollowSymLinks Multiviews to Options FollowSymLinks Multiviews Indexes so that we can view directory listings.
  3. Restart Apache: sudo apachectl restart
  4. Give yourself permissions to the /Library/WebServer/Documents/ folder using the terminal commands:
    1. sudo chgrp staff /Library/WebServer/Documents
    2. sudo chmod g+rws /Library/WebServer/Documents
    3. sudo chmod g+rw /Library/WebServer/Documents/*
  5. Open Finder and navigate to /Library/WebServer/Documents/ using shift+cmd+g
  6. Create a new folder called "orig" and place all files currently in the Documents folder into it.
  7. Create a new file called info.php with <?php phpinfo(); inside it.
  8. Use Safari to navigate to http://localhost/info.php and check that the PHP version is displayed (5.5.14 at the time of writing).

php.ini

  1. cd /etc
  2. sudo cp php.ini.default php.ini
  3. sudo chmod ug+w php.ini
  4. sudo chgrp admin php.ini
  5. Edit php.ini and change settings appropriately.
    At a minimum, you should change:
    date.timezone = "Europe/London"
    error_reporting  =  E_ALL
    display_errors = On

Xdebug

Can't have a PHP development environment without xdebug! Fortunately, Yosemite ships with it ready to enable:

  1. Edit /etc/php.ini and add this line to the end:
    zend_extension = "xdebug.so"
  2. If you want to configure your xdebug settings, then add an [xdebug] section. I like these settings (use with caution…):
    xdebug.var_display_max_children = 999
    xdebug.var_display_max_data = 999
    xdebug.var_display_max_depth = 100
  3. Restart apache: sudo apachectl restart and check in the phpinfo that xdebug is now loaded.

Composer

Install Composer into /usr/local/bin:

  1. cd /usr/local/bin
  2. curl -sS https://getcomposer.org/installer | php
  3. mv composer.phar composer

PEAR

We need PEAR solely for installing PHP extensions using PECL. We just need to run the install phar file.

  1. cd /usr/lib/php
  2. sudo php install-pear-nozlib.phar
  3. sudo pear channel-update pear.php.net
  4. sudo pecl channel-update pecl.php.net
  5. sudo pear upgrade-all
  6. sudo pear config-set auto_discover 1

Compiling extensions

To compile extensions, you need the Xcode command line tools and autoconf.

brew install autoconf

Intl extension

If you need Locale:

  1. brew install icu4c
  2. sudo pecl install intl
    The path to the ICU libraries and headers is: /usr/local/opt/icu4c/
  3. Edit /etc/php.ini and add extension=intl.so to the end.

Mcrypt extension

Firstly, install mcrypt:

  1. brew install mcrypt

Now the PHP extension:

  1. brew tap homebrew/dupes
  2. brew tap homebrew/versions
  3. brew tap homebrew/php
  4. Edit /usr/local/Library/Taps/homebrew/homebrew-php/Abstract/abstract-php-extension.rb and patch as per this patch. (At least until this PR is accepted!)
  5. brew install php55-mcrypt --without-homebrew-php
  6. Enable:
    • sudo mkdir -p /Library/Server/Web/Config/php
    • sudo ln -s /usr/local/etc/php/5.5/conf.d/ext-mcrypt.ini /Library/Server/Web/Config/php/ext-mcrypt.ini

Finally, restart apache: sudo apachectl restart.

That's it! It all works on this machine, anyway :)

As I noted at the top, if you don't want to do all this yourself, there are otheroptionsavailable for PHP on OS X which may work better for you.


Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}