Setting up PHP & MySQL on OS X Yosemite
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:
- PHP 5.3/5.4/5.5 for OS X 10.6/10.7/10.8/10.9/10.10 as binary package by Liip
- Homebrew has PHP.
- Zend Server 7.x (Paid for)
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
- Download the “Mac OS X 10.9 (x86, 64-bit), DMG Archive” from mysql.com and install the pkg.
- Open the pref pane and start the MySQL Server.
- Update the path by editing ~/.bash_profile and add:
export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:$PATH
at top of file.
- 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.
- Now ensure that the mysql.sock file can be found by PHP:
- Ensure that MySQL is running
- sudo mkdir /var/mysql
- 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.
- cd /etc/apache2
- Edit /etc/apache2/httpd.conf
- 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
- 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.
- To enable PHP and rewriting in Apache, remove the leading # from these two lines:
- Restart Apache: sudo apachectl restart
- Give yourself permissions to the /Library/WebServer/Documents/ folder using the terminal commands:
- sudo chgrp staff /Library/WebServer/Documents
- sudo chmod g+rws /Library/WebServer/Documents
- sudo chmod g+rw /Library/WebServer/Documents/*
- Open Finder and navigate to /Library/WebServer/Documents/ using shift+cmd+g
- Create a new folder called “orig” and place all files currently in the Documents folder into it.
- Create a new file called info.php with <?php phpinfo(); inside it.
- 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
- cd /etc
- sudo cp php.ini.default php.ini
- sudo chmod ug+w php.ini
- sudo chgrp admin php.ini
- 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:
- Edit /etc/php.ini and add this line to the end:
zend_extension = "xdebug.so"
- 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 xdebug.max_nesting_level = 200
- Restart apache: sudo apachectl restart and check in the phpinfo that xdebug is now loaded.
Composer
Install Composer into /usr/local/bin:
- cd /usr/local/bin
- curl -sS https://getcomposer.org/installer | php
- mv composer.phar composer
PEAR
We need PEAR solely for installing PHP extensions using PECL. We just need to run the install phar file.
- cd /usr/lib/php
- sudo php install-pear-nozlib.phar
- sudo pear channel-update pear.php.net
- sudo pecl channel-update pecl.php.net
- sudo pear upgrade-all
- 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:
- brew install icu4c
- sudo pecl install intl
The path to the ICU libraries and headers is: /usr/local/opt/icu4c/ - Edit /etc/php.ini and add extension=intl.so to the end.
Mcrypt extension
Firstly, install mcrypt:
- brew install mcrypt
Now the PHP extension:
- brew tap homebrew/dupes
- brew tap homebrew/versions
- brew tap homebrew/php
- brew install php55-mcrypt --without-homebrew-php
- 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 other options available for PHP on OS X which may work better for you.
As an alternative to your php and mysql packages you mentioned and as you've installed brew anyway, you could just grab the packages from that tool.
php
brew install php56 --without-pear
There are several versions of php you can install (i.e. php53, php54, etc.)
As with all brews you can get a full list of options with:
brew info php56
mysql
brew install mysql
You wouldn't need to redefine your path environment var then, as it should already have been defined by homebrew.
Also, to help with hardening your mysql install, post installation you can make use of mysql_secure_installation
An even easier route is to use Phundamental (https://github.com/skl/phundamental) which will download, compile and configure PHP, Nginx and MariaDB in one command.
Now I get your "Don't forget to start up Xcode…" warning. It's easy to forget about doing that and then loosing half an hour to find a fix.
I use Homebrew-PHP
https://github.com/Homebrew/homebrew-php
1. I followed the instructions for Local intl creation and after I did php-v I got "PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20121212/intl.so'".
2. So I searched for intl.so and found it in "/usr/local/Cellar/php55/5.5.18/lib/php/extensions/no-debug-non-zts-20121212"
3. I then changed the line in /etc/php.in to "extension=/usr/local/Cellar/php55/5.5.18/lib/php/extensions/no-debug-non-zts-20121212/intl.so", now php -v is OK and returns PHP v 5.5.14
Any idea why in in php 5.5.18 directory? BTW I installed mySQL with brew mysql.
Thanks for this walkthrough, it worked.
However in addition to wojtek: don't start with upgrading until you have upgraded xcode and xcode command line tools to the latest version.
I took me 3 hours before I noticed.
Whoops, the Homebrew installer has moved! Please instead run:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Also, please ask wherever you got this link from to update it to the above.
Thanks!
Thanks Rob,
I could not get my .htaccess working until I came across your blog.
Its really informative and thanks for sharing.
Dude, you made my day! Today I started working, and everything was messed up! but your tutorial totally helped me! Saludos desde México!
Thnx, very helpful! Just a question with respect to the mysql installation: I already had a mysql server/db setup when I upgraded from maverick to yosemity osx, therefore I dont want to lose the database information of the various webapplications. However, the 'old' mysql server doesnt work anymore under yosemity. How to deal with this?: Should I still install mysql accoding to your instructions and somehow import the old db information into the new db system? Appreciate your comments…
Thank you for the post, it helped me a lot.
I have the following error when installing intl :
downloading intl-3.0.0.tgz …
Starting to download intl-3.0.0.tgz (248,200 bytes)
…………………………………………….done: 248,200 bytes
could not extract the package.xml file from "/tmp/pear/install/intl-3.0.0.tgz"
Download of "pecl/intl" succeeded, but it is not a valid package archive
Error: cannot download "pecl/intl"
Download failed
install failed
any idea ?
I had the same issue, but after a bit of reading around, I don't believe that we're the only one. How to fix it "properly", I'm not sure, as I've not done contributed to extensions, yet. However, here's two links which helped me out:
– http://php.net/manual/de/install.pecl.phpize.php
– https://bugs.php.net/bug.php?id=65834&edit=1
I believe we're experiencing the same issue as in the scrypt bug. I manually extracted the intl archive and it did exactly that. However, cutting a long story short; I successfully installed the extension as follows:
– extract the archive
– run phpize inside the archive directory
– run configure, make, make install
– add extension=intl.so to /etc/php.ini
All the best with it.
Really helpful! Cheers, Rob!
Awesome – thanks!
Any notes on what's needed for running Eclipse instead of XCode?
Thanks,
CtrlJ
CtrlJ,
Running Eclipse is completely independent of needing Xcode to compile C-based libraries on OS X.
Just want to say thanks and congrats !!!
It worked like a charm !!!
Thank you.
Boris.
How do you do step 3? Update the path by editing ~/.bash_profile and add and the terminal code that follows…
Do you just put that in the terminal and hit enter or is there something else?
GP: use a text editor.
Thanks very very much! This is a great help to my fledgling career.
I've successfully followed this but for some reason I'm unable to get away from this version of php. I cannot unlink it and go to another brew install?
Thank you!
Spend several hours trying to figure out a problem and it turned out I just needed to run "sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock"
Ste,
Thank you very much for the useful information!