Portable MAMP (Apache2 + PHP5 +MySQL5)

The aim of that quick and dirty tutorial is a fully functional web server running from any usb-drive on mac OS X (10.4) and maybe on other unix / linux distributions. For sure the following descripts seem to be very comlicated and confusing but it’s a step by step guid to build your own MAMP (MacApacheMysqlPhp) which will be a lot more flexible because it’s independet of any local settings. I call it portable MAMP or just pMAMP.
As MAMP it’ not made to run a public web server because of a whole lot of outstanding security holes.
Also its a kind of unflexible because of the static path names (‘COMPSPINE’) but it’s just a question of the right script (perl seems to be a good for me).

Status: Apache and PHP working perfect. MySQL not really.

0. Requirements

– A USB Stick
– Mac OS 10.4 (10.3 not tested)
– Installation of Apple Developer Toolkit, available at: http://developer.apple.com/tools/download/

Folder structure on the USB-drive / USB stick
COMPSPINE
-data
-mac
—-apache-2.0.58

—-mysql-5.0.45
—-php-5.2.5
—-logs

1. Preparations

Get an USB-Stick, plug it into your Mac and open Disk Utility. It can be started in Finder in the folders Applications and then Utilities or simply by a Terminal command like that:

open -a /Applications/Utilities/Disk\ Utility.app

We want to partition a MS DOS drive which can be read by either win32 and Unix plattforms. Within the Disk Utility you should see a orange colored drive named like your USB-stick inside Finder. Select it and chose the ‘Partition’ tab on the right hand side. Select MS DOS form the drop down menu and change the partition size if you want to. Finaly press ‘partition’ and confirm it by clicking OK. Take care that you backup all your important data form your stick in advance because partitioning delets everything. As long as any application accesses the USB drive Disk Utility can’t unmount the drive which is necessarry before starting the partitioning process.

2. Install Apache on USB drive

Download Apache-2.0.58 on your Desktop from
http://archive.apache.org/dist/httpd/httpd-2.0.58.tar.gz

Open your Terminal and type:

cd /Volumes/COMPSPINE/mac/apache-2.0.58
sudo ./configure –prefix=/Volumes/COMPSPINE/mac/apache-2.0.58 –enable-mods-shared=most
make
make install
open -e conf/httpd.conf

Your favorite editor should appear with the httpd configuration file. Search for the line “Listen 80” and change it to “Listen 8080” (use an other port if that on is used by an other application like tomcat e.g.). Replace line:
DocumentRoot “/Volumes/COMPSPINE/mac/apache-2.0.58/htdocs”
with
DocumentRoot “/Volumes/COMPSPINE/data”.
As well replace

with
Save and close the file.Open now bin/apachectl:

open -e /Volumes/COMPSPINE/mac/apache-2.0.58/bin/apachectl

Replace the line:
HTTPD=’/Volumes/COMPSPINE/mac/apache-2.0.58/bin/httpd’
by
HTTPD=’/Volumes/COMPSPINE/mac/apache-2.0.58/httpd’Back in Terminal type:

sudo /Volumes/COMPSPINE/mac/apache-2.0.58/bin/apachectl start
open -a Safari 127.0.0.1:8080

If the URL ‘127.0.0.1:8080’ appears within the adress line in your Safari your Apache2 is running.

3. Install PHP

Try to download PHP on your Desktop from (URL may change after a while): http://de3.php.net/get/php-5.2.5.tar.bz2/from/a/mirror

Extract the file on your Desktop and:

cp ~/Desktop/php-5.2.5 /Volumes/COMPSPINE/mac

Stop the Apache to avoid conflicts

cd /Volumes/COMPSPINE/mac/apache-2.0.58/bin
sudo apachectl stop

Do some hard coding as a work around (I could figure out which option flag of the following ./configure comand would be a better solution):

open -e /Volumes/COMPSPINE/mac/apache-2.0.58/bin/apxs

Replace
my $httpd = get_vars(“sbindir”) . “/” . get_vars(“progname”);
with
my $httpd = “/Volumes/COMPSPINE/mac/apache-2.0.58” . “/” . get_varsNow we are going to configure a Makefile to compile PHP:

/Volumes/COMPSPINE/mac/php-5.2.5/configure –prefix=/Volumes/COMPSPINE/mac/php-5.2.5 –with-apxs2=/Volumes/COMPSPINE/mac/apache-2.0.58/bin/apxs –with-config-file-path=/Volumes/COMPSPINE/mac/php-5.2.5

Compile your PHP

make
make install

(I got a warning after ‘make’)Now we need a php.ini file

cp php.ini-dist php.in

Configure Apache2

open -e /Volumes/COMPSPINE/mac/apache-2.0.58/conf/httpd.conf

Look for somethin similar like “LoadModule php5_module modules/” an add the line
LoadModule php5_module modules/libphp5.soSearch for anything like “AddType application/” and add the line
AddType application/x-httpd-php .php .phtml
(you can add other file endings as well)Now we have to sheet a little bit. Go to http://www.mamp.info/en/index.php and download a fresh copy of MAMP (don’t take the pro version). After mounting the .dmg file we going to borrow just one file:

cp /Volumens/MAMP/MAMP/Library/modules/libphp5.so /Volumes/COMPSPINE/mac/apache-2.0.58/modules/

After doing that you can unmount and delete MAMP

Start Apache again:

sudo /Volumes/COMPSPINE/mac/apache-2.0.58/bin/apachectl start

Finaly lets see if PHP is working. Therefor we need to have a test file.

vi /Volumes/COMPSPINE/data/index.php

An editor called ‘vi’ appears within the terminal windos. Even if you don’t see anything type:

“”

Now press escape once and type (again in the command mode of ‘vi’) ZZ
Vi should closes and you will see all your terminal stuff again.

Install MySQL

sudo chown -R mysql .
sudo chgrp -R mysql .
scripts/mysql_install_db –user=mysql
open -e ../bin/mysqlaccess

Replace the line:
$MYSQL = ‘/usr/local/mysql/bin/mysql’; # path to mysql
with
$MYSQL = ‘/Volumes/COMPSPINE/mac/mysql-5.0.45/bin/mysql’; # path to mysql

Safe and close the file.

sudo bash
cd sudo chgrp -R mysql /Volumes/COMPSPINE/mac/mysql-5.0.45/data
sudo chown -R mysql /Volumes/COMPSPINE/mac/mysql-5.0.45/data
sudo -u mysql -b /Volumes/COMPSPINE/mac/mysql-5.0.45/bin/safe_mysqld
./bin/mysqladmin -u root password root

Instead of ‘password root’ you should be creative to chose your own password

sudo ./bin/mysqld –user root –pid-file=/Volumes/COMPSPINE/mac/mysql-5.0.45/data/mysql.pid –basedir=/Volumes/COMPSPINE/mac/mysql-5.0.45 –lower_case_table_names=2

For me this is not teh perfect way to start the mysql server but it should work. It would be better to use a different –socket path then /tmp. That means that we are accessing the local hard disc. The option –socket=/Volumes/COMPSPINE/mac/mysql-5.0.45/tmp/mysql.sock results in the following error:

071209 17:51:12 InnoDB: Started; log sequence number 0 43655
071209 17:51:12 [ERROR] Can’t start server : Bind on unix socket: Invalid argument
071209 17:51:12 [ERROR] Do you already have another mysqld server running on socket: /Volumes/COMPSPINE/mac/mysql-5.0.45/tmp/mysql.sock ?
071209 17:51:12 [ERROR] Aborting

I have multiple mysql servers on my system but none of them accesses that tmp folder. Its allmost caused by wrong priveleges but my tmp folder is owned by either mysql user and group. Also the bin/mysqld is owned by mysql and his group.

It’s often good to know how kill that particular process. Use:

sudo bash && killall mysqld

Any suggestions to fix that?