Working with PHP on OS-X: Tips and Tricks
Working with PHP on OS-X requires tips and tricks if you want to maintain your sanity. Tarn Aeluin has a similar post for the world of Java on OS-X. I was recently playing around with Magento 2, an open-source eCommerce solution, and had enough trouble that I wanted to be sure that everything I learned would be handy for myself and anyone else that thought they had PHP working ok, but had a few things to enable. As with my Java post – I’m going to keep adding to/updating this over time.
Note: I’m specifically writing about PHP on OS-X El Capitan (10.11.3). Tips and tricks below may or may not work on other versions of OS-X.
Updated 10/12/2016: PHP IDEs!
Development Machine Setup
The machine I’m using is a MBP (MacBookPro11,3) that I use for most of my development work. On this machine I have Apache httpd 2.4 with a fairly generic configuration. I’m using the version installed with OS-X. My laptop has 16GB of RAM and plenty of SSD space for disk. Apache is still running under the OS-X security configuration (I have to sudo to restart it for example), but I have mapped the webroot to a directory under my user’s home directory.
Basic Tools for PHP on OS-X
For starters I recommend installing (or upgrading) to the latest XCode and then install the command line tools. To do this execute this command from a terminal after installing XCode:
xcode-select --install
There are two tools that are essential with PHP on OS-X: Homebrew and Composer. Homebrew is a general tool that helps with installing and updating just about anything on OS-X whereas Composer is a PHP dependency management tool. On El Capitan and above there are some additional steps to take when using brew – the most important is to make sure that /usr/local is writeable, but see the homebrew page for all the details. Support for PHP is quite good. It is helpful to first consider what you are doing with PHP. There are a lot of options that can be installed. Take a look at the options with this command:
brew options php56
Since I’m working with apache and web apps I’m going to go with these options:
brew install php56 --with-apache --with-homebrew-curl --v
Speaking of curl install that and these other things with the following commands:
brew install curl --with-libssh2 --with-openssl brew update php56 --with-homebrew-openssl
Then we need to install a bunch of other PHP extensions. What extensions are available? Use this command:
brew search php56
That command will show you what PHP extensions are available and highlight what is actually installed. You can use brew list to see just what is currently installed by brew (not just PHP stuff, but everything you have installed with brew). So with that information now I can install a set of extensions that I will need for working with Magento 2 (my current project I’m working on):
brew install php56-opcache
brew install php56-tidy brew install php56-uploadprogress brew install php56-oauth brew install php56-mailparse brew install php56-igbinary
So where is all this stuff getting installed? Homebrew will handle creating all the symlinks for you so that when you issue PHP commands you will be all set, but it is helpful to know where everything is. There are commands to help. This command: php –ini will tell you what php.ini files are being loaded. You can then go look in there for default locations of stuff:
Now you know where to go change the main php.ini configuration. Homebrew keeps things pretty tidy as well by managing the .ini files for all your extensions. I hinted above at the location where homebrew places things. Check /usr/local/Cellar and you will see everything there. Neat and tidy unlike my actual cellar.
PHP on OS-X: Configuration with a Brew Install
PHP is now installed and you should be able to interact with it on the command line. Use “php -v” to check the version number matches you expectation. The next step is to get Apache httpd 2.4 configured – 2.4 is the default version installed with OS-X El Capitan. The first question is of course – where is the main conf file? Of course it is in /etc/apache2. To edit the file you will need to copy it, make your changes, and then copy it back (or you could change the permissions, but I’m not going to do that.). I’m going to enable rewrites and php with these lines:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
#LoadModule php5_module libexec/apache2/libphp5.so LoadModule php5_module /usr/local/Cellar/php56/5.6.19/libexec/apache2/libphp5.so
I find it a good idea to rem out an original line and add a new one if you are making a change to the line itself. With that done if you plan to control re-writes with .htaccess files you need to allow that in your directory blocks. Since this is a my dev laptop (not even a dev server) I’m going to do something you should never do in production:
<Directory "/Users/beren/www"> ... AllowOverride All ... </Directory>
This setting will basically allow .htaccess files to be used in any sub-directory of my webroot. Handy when you are installing and playing with many tools like Drupal, WP, Magento 2, etc. Not so secure however. It is important to also look at how many PHP parameters including redirect settings and OpenSSL parameters can be set in various files including .htaccess files. Parameters can be set only in php.ini, or in a combination of php.ini .htaccess files, as well as other locations.
PHP on OS-X: Dependency Management with Composer
There are a lot of dependency management tools – I suppose the oldest is PEAR – but I’ve come to think Composer is the best of them, whether you are using PHP on Linux/Windows or PHP on OS-X. Many tools that I’ve been using recently use Composer. Symfony2 in particular makes heavy use of it and now I’m also using Magento 2 that also uses it. You can use the installer from the Composer page, but I’ve started to install everything with Homebrew – and yes they have support for Composer! Why use Brew? Two reasons. Brew will setup all the links so that you can run composer globally. Brew will update Composer if there is an update when you run “brew ugrade composer”.
Either way – once you have it installed it is simply a matter of getting some code and then running “composer install” in a source directory that has a composer.json file. When you run the install you will see that composer reads the composer.json file and grabs all the packages listed in there and downloads them. Inside the .json file the versions for each product are specified. Once installed then you can use “composer update” to check for any updates. Each time the .json file is updated in the code you can then easily update everything.
I was getting a bunch of errors from composer related to https connections in my composer.json file. Composer (rightly so) by default will only connect to https url’s. You can turn this off with the following command:
composer config -g -- secure-http true
but that won’t do you any good if the project repository you are trying to connect to is only available via https, which is the case with Magento 2. There’s not a ton of information about how to fix this issue on the internet. Here’s a gallery of the various errors I was getting with captions explaining what to do or what was causing each of them.
That was a bit of a pain – especially that one about the cert being “missing” when it was really that the folder it was trying to write to did not exist. Why that odd directory? That was what I set as my temp directory in php.ini while I was debugging all this stuff.
PHP on OS-X: Integrated Development Environment (IDE)
Ok – so any serious developer needs an IDE. I’ve used them all, but Jet Brains PHPStorm is the best for PHP on OS-X. Sorry Eclipse, Sublime, Emacs, and vi fans…I know you can use these tools, but PHPStorm is awesome. Great tools still need tricks so I’m going to document PHPStorm tricks here so I have a good list (for when I forget):
- To get CSS minimization working do this.
- More to come….
I do also use Sublime quite a bit for quick edits and changes or when just poking at code in an open source tool like WordPress or Drupal.