Ok so this is aimed at being a very quick guide to installing a PHP (specifically PHP 5.4, but you can install PHP 5.3 or.PHP 5.5 if you wish!) development environment and MySQL server to enable you to develop (using the built in PHP development server) and MySQL on an MacOSX based machine, so to begin you should download and run the MacPorts .dmg file from the MacPorts website.
After you’ve ran and installed MacPorts, its time to bring up the terminal, next type the following at the terminal prompt:-
sudo port -d selfupdate
….it should now automatically check and update itself.
To install MySQL Server, simply run the following command:-
sudo port install mysql55 mysql55-server
Then we’ll set it to start automatically on system boot up:-
sudo /opt/local/lib/mysql55/bin/mysql_install_db --user=mysql
To be able to access MySQL using a desktop client (using TCP instead of a ‘socket’) I’d recommend you edit the mysql configuration file and comment out ‘skip networking’, you can do this like so:-
sudo nano /opt/local/etc/mysql55/macports-default.cnf
You can now start the MySQL server like so:-
sudo /opt/local/share/mysql55/support-files/mysql.server start
Now we’ll configure MySQL to launch at System Startup like so:
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql55-server.plist
Thats it, we can now move on to install PHP…
Now lets look at the PHP packages that we want to install…
You can use the ‘package search‘ feature on the MacPorts website to look at what packages you want to install, I’d recommend and will be installing the following packages for our ‘development setup’:-
- php54
- php54-gd
- php54-curl
- php54-imagick
- php54-intl
- php54-mbstring
- php54-mcrypt
- php54-iconv
- php54-ming
- php54-mysql
- php54-soap
- php5-sockets
- php54-sqlite
- php54-xdebug
- php54-zip
So we’ll now install them like so:-
sudo port install php54 php54-curl php54-imagick php54-intl php54-mbstring php54-mcrypt php54-iconv php54-ming php54-mysql php54-soap php54-sockets php54-sqlite php54-xdebug php54-zip
You may notice after the first package ‘php54’ has been installed you’ll see this message appear in the console:-
So now that everything is installed, we’ll make a copy of the ‘distribution’ copy so that we can make tweaks if needed in future, we’ll do this like so:-
sudo cp /opt/local/etc/php54/php.ini-development /opt/local/etc/php54/php.ini
So now and in future if you wish to edit this file, you can do so like (again, from the command line):-
sudo nano /opt/local/etc/php54/php.ini
Now, you can run PHP and check the version number from the terminal like so:-
php54 -v
Now we’ll symlink the php54 binary to ‘php’ so we can run PHP using the normal ‘php’ command, like so:-
sudo ln -s /opt/local/bin/php54 /usr/bin/php
So now, you should be able to run:-
php -v
Thats great! so now you can start your development PHP development server like so from the Terminal:-
sudo php -S localhost:8000
Hope you found this useful!
One reply on “Installing PHP and MySQL on MacOSX using MacPorts for development.”
[…] you prefer to stay with MacPorts: There is a blog post with a detailed howto for PHP 5.5. The respective uninstall commands for Fink are […]