OS X Lion comes with most of the tools you would need to do “MAMP” (Mac OS X, Apache, MySQL/MariaDB, PHP) development, as outlined in my previousposts once you add a database. So then why would you want to use MacPorts? Setting your development environment up in MacPorts isolates the binaries, libraries, and configuration files, completely separate from the existing OS X install (with the exception of startup scripts). You can also tweak the configuration files on your own, apply your own patches, and apply updates that MacPorts may get before Apple pushes them. It will take more time because you’ll be compiling everything, but you have all of the control. Read on for how to get things set up.
We’ll going to be compiling a lot of source code so you’ll need to have Xcode 4 installed. I initially tried with the OS X GCC Installer but eventually ran into a problem where a portfile was expecting an Xcode binary to check to a prerequisite. I’m sure some hacking could’ve resolved it to avoid having to install the very large Xcode package, but at the end of the day it’s better to just know that it’ll work as expected.
Once you have Xcode 4 installed, you’ll need to install MacPorts. It’s a simple click-through installer. Come back to this guide once MacPorts is ready.
Note that for all commands before that are starting with a $, the dollar sign is showing a command-line prompt in Terminal, and you should not actually type it as part of the commands.
Update the ports tree to get the latest from the MacPorts server in case the installer had an older tree:
sudo /opt/local/bin/port selfupdate
All of the packages that we need can be installed with a single command:
Start by setting up PHP to work with Apache properly:
$ sudo cp -av /opt/local/apache2/conf/httpd.conf /opt/local/apache2/conf/httpd.conf-default
$ sudo /opt/local/apache2/bin/apxs -a -e -n "php5" /opt/local/apache2/modules/libphp5.so
$ sudo bash -c "cat >> /opt/local/apache2/conf/httpd.conf <<'EOF'## Add mod_php informationInclude conf/extra/mod_php.conf# Add index.php to the list of files that will be served as directory indexes.<IfModule dir_module> DirectoryIndex index.php index.html</IfModule>EOF"
Now we’ll move httpd-vhosts.conf to ~/Sites for easy editing of new virtual hosts, as well as create a ~/Sites/logs folder:
$ sudo sh -c "cat >> /opt/local/apache2/conf/httpd.conf <<'EOF'# phpMyAdminInclude conf/extra/phpmyadmin.confEOF"$ sudo sh -c "cat >> /opt/local/apache2/conf/extra/phpmyadmin.conf <<'EOF'AliasMatch ^/phpmyadmin(?:/)?(/.*)?$ /opt/local/www/phpmyadmin$1AliasMatch ^/phpMyAdmin(?:/)?(/.*)?$ /opt/local/www/phpmyadmin$1<Directory "/opt/local/www/phpmyadmin"> Options -Indexes AllowOverride None Order allow,deny Allow from all LanguagePriority en de es fr ja ko pt-br ru ForceLanguagePriority Prefer Fallback</Directory>EOF"$ sudo /opt/local/bin/port unload apache2
$ sudo /opt/local/bin/port load apache2
Basic set up of phpMyAdmin:
sudo sed -i """s/blowfish_secret\'\] = \'/blowfish_secret\'\] = \'`cat /dev/urandom | strings | grep -o '[[:alnum:]]' | head -n 50 | tr -d '\n'`/" /opt/local/www/phpmyadmin/config.inc.php
sudo /opt/local/bin/mysql5 -uroot -proot < /opt/local/www/phpmyadmin/scripts/create_tables.sql
sudo /opt/local/bin/mysql5 -uroot -proot -e"CREATE USER 'pma'@'localhost' IDENTIFIED BY 'pmapass'; GRANT SELECT, INSERT, DELETE, UPDATE ON phpmyadmin.* TO pma@localhost;"
Now you can edit ~/Sites/httpd-vhosts.conf and add a new virtual host. You’d need to reload Apache after doing so by running sudo port unload apache2 && sudo port load apache2. phpMyAdmin or the mysql5 binary should provide you a way to create new databases and database users and you can then set up a local site.
If you find MacPorts too heavy a separation from OSX, or too slow to compile, you should check out my previous blog posts on setting up a MAMP environment with as many built-in tools as possible only by adding a MySQL installer or MySQL/MariaDB via Homebrew.