Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GabLeRoux/5766354 to your computer and use it in GitHub Desktop.
Save GabLeRoux/5766354 to your computer and use it in GitHub Desktop.
Detailed steps to install PHP, MySQL, Apache and some usefull extensions such as xDebug on a mac running Mac Os X. Also included: PostgreSQL.

Install PHP, MySQL, PostgreSQL, Apache and xDebug on a Mac

You should have some tools installed like Homebrew, a text editor such as Sublime Text and it's subl command line app inside a bin folder and some basic terminal knowledge.

This gist is a more detailed version of niepi's gist with things that worked for me. It may help someone :)

Install PHP

$ brew install php --with-apache --with-mysql --with-pgsql --with-intl

Copy default php.ini file into your private configuration directory

$ sudo cp /private/etc/php.ini.default /private/etc/php.ini

Edit php.ini file with sublime text

$ subl /private/etc/php.ini

Set php timezone in php.ini

Add date.timezone = America/Montreal

See supported PHP time zones for your own timezone

Edit apache conf file with sublime text

$ subl /private/etc/apache2/httpd.conf

Load php module in apache

Add LoadModule php5_module libexec/apache2/libphp5.so in your httpd.conf file

Fix pear permissions and set php.ini location

$ chmod -R ug+w /usr/local/Cellar/php53/5.3.25/lib/php
$ pear config-set php_ini /private/etc/php.ini

Start Apache Server

$ sudo apachectl start

or

$ sudo apachectl restart

Now visit localhost and see if it works. You may need to specify your documentroot directory and other parameters to make it work properly. See Apache documentation

Install mysql

$ brew install mysql

Install mysql default tables

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

Note the datadir /usr/local/var/mysql, you'll need to make sure it's correctly set in your my.cnf file.

Edit my.cnf

Edit my.cnf with sublime (if it doesn't exist, just create it)

$sudo subl /private/etc/my.cnf

my.cnf

[mysqld]
datadir=/usr/local/var/mysql
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/usr/local/Cellar/mysql/5.6.10

[client]
socket=/var/lib/mysql/mysql.sock

Set mysql up to start automatically on system boot:

$ mkdir -p ~/Library/LaunchAgents
$ sudo cp /usr/local/Cellar/mysql/5.6.10/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

ln won't work if you try to launchctl load on a symbolic link. You may use hardlink for osx instead of copying the mysql.plist file.

$ sudo hardlink /usr/local/Cellar/mysql/5.6.10/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Note: At this point, mysql should already be started by launchctl after launchctl load.

You can also verify other launchtl loaded agents by going into ~/Library/LaunchAgents and/or /Library/LaunchAgents folder and by listing files

$ cd ~/Library/LaunchAgents
$ ls -la

Start mysql:

$ mysql.server start

Make sure you don't start mysql deamon before running mysql_install_db

Change MySQL Password

1st time

$ mysqladmin -u root password newpassword

If a password already exists

$ mysqladmin -u root -p'oldpassword' password newpassword

Test if your password works

$ mysql -u root -p'yourpassword' -e 'show databases;'

Change socket dir usage for Apache

Edit php.ini file and locate

pdo_mysql.default_socket=/var/mysql/mysql.sock
mysqli.default_socket=/var/mysql/mysql.sock
mysql.default_socket=/var/mysql/mysql.sock

and replace their path with the socket directory we used /var/lib/mysql/mysql.sock.

Note: You can find the mysql.sock file used by your server by looking at the mysql configuration variables (see bellow) or you can define your own in the my.cnf file.

See mysql configuration variables

Usefull if you want to know your datadir or pid file location, etc.

$ mysql -u root -p
SHOW VARIABLES;

Install xdebug & apc

If you don't have autoconf (Xcode 4.3) install autoconf

$ brew install autoconf

Install xdebug & apc

$ pecl install xdebug apc

xdebug setup

Open php.ini file and set this

[xdebug]
zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.remote_enable = On
xdebug.remote_autostart = 1

You should now see xDebug enabled using phpinfo(), make sure you use zend_extension and not extension or you will get a “XDEBUG NOT LOADED AS ZEND EXTENSION” warning and it will not work as expected with breakpoints. ;)

Optional parts

Install postgresql

$ brew install postgresql

Initialize the DB

$ initdb /usr/local/var/postgres

Add startup items

$ cp /usr/local/Cellar/postgresql/9.2.4/org.postgresql.postgres.plist ~/Library/LaunchAgents

or

$ sudo hardlink /usr/local/Cellar/postgresql/9.2.4/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

then

$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Start Postgres

$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Install pear packages

PHP q/a tools

$ pear config-set auto_discover 1
$ pear install pear.phpqatools.org/phpqatools pear.netpirates.net/phpDox

This will install:

  • PHP_Depend
  • PHP_CodeSniffer
  • File_Iterator Text_Template
  • PHP_Timer
  • YAML
  • Console_CommandLine
  • Log
  • PHP_TokenStream
  • Base
  • PHP_PMD
  • PHP_CodeBrowser
  • PHP_CodeCoverage
  • PHPUnit_MockObject
  • ConsoleTools
  • PHPUnit
  • phpcpd
  • phploc
  • phpqatools

Install phing

$ pear channel-discover pear.phing.info
$ pear config-set preferred_state beta
$ pear install phing/phing
$ pear config-set preferred_state stable

Resources

@karawitan
Copy link

Hello thanks for sharing this. On Catalina, with homebrew 2.2.14;

brew install --with-mysql 

gives

Error: invalid option: --with-mysql

The same for all options ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment