Year: 2012
In one of my old posts, I noted short documentation on how to get started with the AMP stack on Mac OS X Lion. In that setup, I was using the default PHP and downloaded MySQL from the mysql website. In this post, I am going to describe how I used homebrew to setup my AMP environment with ease!
The Homebrew Magic
Homebrew is a cool tool for Mac OS users. Written in Ruby, this beautiful tool works as a package manager for Mac OS. Unlike Mac Ports or Fink, homebrew takes less space, uses the default os x tools to compile everything to a custom location. This makes life suck less and clutter free!
To install PHP 5.4 branch using homebrew, just issue this command:
1 2 3 |
brew tap josegonzalez/php brew update brew install php54 |
PS: If you want to customize the installation (like installing the mysql module by default) add necessary configuration options. Using the “bash-completion” formula from brew, you can hit tabs to see available installation options.
To view information on your PHP 5.4 installation via brew, type in:
1 |
brew info php54 |
Seperate MySQL Installation: Where is the ctl?
I have installed MySQL separately. I added the mysql and mysqldmin to my bash_profile like this:
1 2 |
alias mysql=/usr/local/mysql/bin/mysql alias mysqladmin=/usr/local/mysql/bin/mysqladmin |
And to start/stop/restart mysql server, I added:
1 |
export mysqlctl=/Library/StartupItems/MySQLCOM/MySQLCOM |
That allows me to do:
1 |
sudo $mysqlctl restart |
Using PEAR
Homebrew installs all the PEAR packages to “/usr/local/Cellar/php54/5.4.3/lib/php”. So, I added it to my php include_path.
For PEAR to work correctly, we need sudo. If there are tools involved in the pear setup which can be run from command line (eg. phpunit), the binary files are stored in: “/usr/local/Cellar/php54/5.4.3/bin”.
I added the above path to my .bash_profile for convenience 🙂
The other day, I got myself a Macbook Pro which runs OS X Lion. Coming from the Linux world where everything is available at finger tips via package managers, I was at first lost. I tried MAMP and Zend Server CE. I had some issues with Zend Server CE and custom virtual hosts – mostly because I couldn’t figure out which bits to be changed. MAMP worked out pretty well. But I wanted to use the native builds of the AMP stack. I know about macports and homebrew but wanted to do things myself!
(1) Mac OS X ships with built in apache. I enabled “Web Sharing” from System Preference > Sharing. It turned on viewing of my ~/Sites directory at http://localhost/~masnun/. It works but not pretty helpful.
(2) I edited /etc/apache2/httpd.conf to change the document root to ~/Sites. Now I could see my home page on localhost.
(3) I tried creating virtual hosts at ~/VirtualHosts – this worked with MAMP but with the built in Apache, it can not load the virtualhosts from that location because of permission issues. So instead, I reinstated the default location for vhosts inside the “extra” directory. I appended all my virtualhosts in one file. It works!
(4) “NameVirtualHosts *” and a separate virtual host for localhost is must.
(5) Mac OS X has a built in PHP. I just created the default php configuration file at /etc/php.ini
(6) I downloaded MySQL from their website. Installed the packages and imported my existing databases.
(7) PHP by default expects the mysql unix socket to be in /var/mysql/mysql.sock but the package from mysql creates the socket at /tmp/mysql.sock. So I had to edit the socket for mysql, pdo-mysql and mysqli. I needed to change them all because WP uses mysql, phpmyadmin uses mysqli and ZF uses pdo.
(8) I ran the pear installer (as a phar file inside /usr/lib/php) to install pear and pecl.
(9) To install mcrypt extension, I downloaded libmcrypt and source of PHP 5.3.10. I changed directory to PHP’s mcrypt extension directory. Ran “phpize”, “make” and “sudo make install”. Copied the generated extension to my custom extensions directory.
(10) Restarted apache at every step. It’s now working fine 🙂