This post is next in a series of posts about installing PHP web development on Mac. In the last post I showed you how to install and configure Apache web server. This time I will show you step by step instructions on how to install and configure PHP 5.5 on your Mac.
Before you start installing PHP 5.5 make sure you have:
Each Mac OS X comes with PHP preinstalled but it’s usually outdated. Installing PHP from MacPorts gives you a way to have most recent version of PHP. Before we start we will check the current version of PHP. Open Terminal and do:
$ php -v PHP 5.4.17 (cli) (built: Aug 25 2013 02:03:38) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologie
On my system (OS X 10.9.1 – Mavericks) it shows PHP version as 5.4.17. Now lets find out what is the most up to date version of PHP in MacPorts:
$ port search php php55 @5.5.8 (lang, www) PHP: Hypertext Preprocessor
I have removed most of the lines but this one was the most current one when I was writing this post and this is the one we will install. To do that issue command:
$ sudo port install php55
After a short while you should get something similar to below image on your screen:
Great! PHP has been installed. The next step is to set the configuration file PHP will use. To do that we change directory to where PHP configuration files have been installed and list them:
$ cd /opt/local/etc/php55 $ ls -al total 288 drwxr-xr-x 5 root admin 170 Jan 25 15:39 . drwxr-xr-x 11 root admin 374 Jan 25 16:14 .. -rw-r--r-- 1 root admin 70977 Jan 10 17:46 php.ini-development -rw-r--r-- 1 root admin 71009 Jan 10 17:46 php.ini-production
By default MacPorts gives us two example ini files. One for development (php.ini-development) and one for production (php.ini-production). Since this is going to be a development environment we are going to use the first one. The only thing we have to do now is to create a copy:
$ sudo cp php.ini-development php.ini
We won’t be doing any changes to this file at this time.
Now we have PHP installed and configured but when you check the version of PHP you will still get 5.4.17. This is because we have to activate the version we have installed from MacPorts. Before we do that lets check what versions we do have available:
$ port select php Available versions for php: none (active) php55
OK the 5.5 version is not active. To activate it do:
$ sudo port select php php55 Selecting 'php55' for 'php' succeeded. 'php55' is now active.
When you check the PHP version right now you will still get 5.4.17 that’s because you have to close the Terminal and open it again. After doing that you should be greeted with something like this:
$ php -v PHP 5.5.8 (cli) (built: Jan 10 2014 17:45:35) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
Alright we have PHP 5.5.8 installed. Now it’s time to install Apache handler for PHP.
Configuring Apache PHP handler
Apache handler is another install from MacPorts:
$ port search php | grep apache php53-apache2handler @5.3.28 (lang, www) php54-apache2handler @5.4.24 (lang, www) php55-apache2handler @5.5.8 (lang, www)
You have to install handler for the PHP version we have installed.
$ sudo port install php55-apache2handler
This will take a while to finish… When it’s done port will let us know how to enable PHP Apache module in one of the last log messages on the screen.
Lets do it.
$ cd /opt/local/apache2/modules $ sudo /opt/local/apache2/bin/apxs -a -e -n php5 mod_php55.so [activating module `php5' in /opt/local/apache2/conf/httpd.conf]
Unfortunately this is not all we have to do. To finish Apache configuration we have to open its main configuration file and do two changes to it.
$ sudo nano /opt/local/apache2/conf/httpd.conf
In the file find (ctrl-w) lines:
<IfModule dir_module> DirectoryIndex index.html </IfModule>
and change the
DirectoryIndex so it looks like this:
<IfModule dir_module> DirectoryIndex index.html index.php </IfModule>
Then scroll to the end of the file and find the line we have added in the previous blog postl about installing and configuring Apache:
# Virtual hosts Include conf/extra/httpd-vhosts.conf
below that line add two lines:
# PHP configuration Include conf/extra/mod_php55.conf
Now save the file and restart apache:
$ sudo /opt/local/apache2/bin/apachectl restart
If you get errors make sure they are solved before you go to the next step.
In the previous blog post we have set up Apache with virtual host for testing (
http://site1.dev). We will use it to test is PHP and Apache have been configured properly. Change directory to our test site and rename file
$ cd ~/ws/site1 $ mv index.html index.php
now open the
index.php file using nano and make it look like this:
<?php phpinfo(); ?> <html> <body> <h1>Hello from site1!</h1> </body> </html>
Save and exit the file. Now go to your favorite browser and view the site
http://site1.dev. You should see something similar to this:
Great! PHP and Apache work together the way we wanted. Before closing the browser check if php info line
Loaded Configuration File points to
/opt/local/etc/php55/php.ini if it doesn’t that means you did something wrong when coping the file
php.ini little bit earlier.
Installing additional PHP packages
Now when everything works properly we will install the rest of the most important packages:
$ sudo port install php55-xdebug php55-sqlite php55-pear php55-openssl php55-mysql php55-memcached php55-mcrypt php55-mbstring php55-imagick php55-iconv php55-gd php55-curl
When the installation is done restart Apache and go to
http://site1.dev in your browser and check if all the modules are listed.
That’s it! Now you know how to do basic basic Apache web server configuration. If you like the post / video tutorial make sure to give me thumbs up on YouTube!