Turn Mac OS X Lion Into a LAMP Web Server (Without MAMP)

|

Lots of developers use MAMP for local PHP development. There’s nothing wrong with MAMP. It works. But I wanted to find out how to use the pre-installed Apache and PHP in Lion.

Prerequisites

Ensure these directories exist:

$ sudo mkdir /usr/local/include
$ sudo mkdir /usr/local/bin
$ sudo mkdir /usr/local/lib
$ sudo mkdir -p /usr/local/man/man1

Setup Apache2

$ sudo chmod u w /etc/apache2/httpd.conf
$ sudo vim httpd.conf

Uncomment #LoadModule php5_module libexec/apache2/libphp5.so.

To enable virtual hosts, uncomment #Include /private/etc/apache2/extra/httpd-vhosts.conf.

Notice that Directory "/Library/WebServer/Documents" has AllowOverride None. So if you want to use .htaccess files, you can specify AllowOverride All for individual directory blocks within VirtualHosts.

Save and quit the file and restore original file permissions:

sudo chmod u-w /etc/apache2/httpd.conf

Restart Apache1:

$ sudo apachectl restart

Check Apache is running with

$ ps aux | grep httpd

Install and Setup MySQL

Homebrew is awesome. So use it to install MySQL:

$ brew install mysql

Have MySQL run as your system user:

$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

Start MySQL:

$ mysql.server start

If MySQL started successfully, follow the instructions given by running this command:

$ /usr/local/Cellar/mysql/5.5.10/bin/mysql_secure_installation

If you run into problems logging in as root or setting up other user accounts, try this.

Check MySQL is running with

$ ps aux | grep mysql

Setup PHP

$ cd /etc
$ sudo cp php.ini.default php.ini
$ sudo chmod ug w php.ini
$ sudo chgrp admin php.ini

Edit php.ini to have:

error_reporting  =  E_ALL | E_STRICT
display_errors = On
html_errors = On
extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20090626"

Change all instances of /var/mysql/mysql.sock to /tmp/mysql.sock.

Check PHP is setup correctly by having info.php in Apache’s default document root /Library/WebServer/Documents and having this inside it:

Install phpMyAdmin and Enable with All Functionality That You May Not Even Need Just for Kicks

Use Homebrew to tap this PHP formulae repo:

$ brew tap josegonzalez/php

Create the file /private/etc/apache2/extra/phpmyadmin.conf and add this:

Alias /phpmyadmin /usr/local/share/phpmyadmin

    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all

Edit the phpMyAdmin config file in /usr/local/Cellar/phpmyadmin/3.4.10.1/share/phpmyadmin by creating a copy of config.sample.inc.php as config.inc.php. If you have $cfg['Servers'][$i]['auth_type'] = 'cookie'; then you must have a $cfg['blowfish_secret']. This website generates one for you. Also make sure to have $cfg['Servers'][$i]['host'] = '127.0.0.1'; instead of localhost (explanation of why network socket works but not Unix socket). My full Apache configuration.

You should now be able to log into phpMyAdmin.

Import /usr/local/Cellar/phpmyadmin/3.4.10.1/share/phpmyadmin/scripts/create_tables.sql using phpMyAdmin. This will create some tables necessary to turn on all the extra functionalities.

Create user “pma” with password “pmapass” (or another of your choosing, just be sure to update config.inc.php) if one hasn’t been created from the import already and grant it all privileges for the phpMyAdmin database. Uncomment the lines under “User used to manipulate with storage” and “Storage database and tables.”

mcrypt Support

Dependencies: Xcode >= v4.1

$ brew install mcrypt mcrypt-php

Add this line to /etc/php.ini (make a copy of php.ini.default if it doesn’t exist): extension="/usr/local/Cellar/mcrypt-php/5.3.10/mcrypt.so"

Restart Apache and check to see the warning disappeared in phpMyAdmin.

Setup Pear

Pear isn’t setup on Lion, but the install phar file is here, so we just need to run it:

$ cd /usr/lib/php
$ sudo php install-pear-nozlib.phar

Edit /etc/php.ini and find the line ;include_path = ".:/php/includes" and change it to include_path = ".:/usr/lib/php/pear".

$ sudo pear channel-update pear.php.net
$ sudo pecl channel-update pecl.php.net
$ sudo pear upgrade-all

Install MDB2, MDB2′s mysqli Driver

If you’re using PHP ≥ 5.3, then you need to install this beta version of MDB2.

$ pear install MDB2-2.5.0b3
$ pear install MDB2_Driver_mysqli-1.5.0b3

Enable memcache Support for PHP

$ brew install memcached

Start memcached and use PECL to install the PHP memcache extension:

$ /usr/local/bin/memcached
$ pecl install memcache

Put this file in a document root that’s served by Apache and check it works: