Setting up LAMP stack for web development on OS X can be done with 3rd party software like MAMP but as Mac OS X comes with pre-installed Apache and PHP it’s easy to use the native setup. You just need to configure Apache, PHP and install MySQL.
Setup Apache2
Set up the Server Name to localhost to suppress the warning about fully qualified domain name and enable PHP module.
$ sudo vim /etc/apache2/httpd.conf
ServerName localhost:80
LoadModule php5_module libexec/apache2/libphp5.so
Create “virtual hosts” under your Sites. Change the username to your account’s username.
~$ sudo vim /etc/apache2/users/username.conf
ServerName dev
DocumentRoot /Users/username/Sites
VirtualDocumentRoot /Users/username/Sites/%-2/htdocs
UseCanonicalName Off
AllowOverride All
Order allow,deny
Allow from all
Now Apache serves your projects from your home directory’s Sites folder. Apache will serve files from the htdocs folder like “~/Sites/projectname/htdocs”.
Now just restart Apache and check that it’s running.
$ sudo apachectl restart
$ ps aux | grep httpd
Setup PHP
$ sudo cp /etc/php.ini.default /etc/php.ini
Edit php.ini for easier debugging:
error_reporting = E_ALL | E_STRICT
display_errors = On
html_errors = On
Setup MySQL
MySQL can be installed directly from Oracle’s MySQL packages or by using Homebrew.
Install Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install MySQL using Homebrew
$ brew install mysql
Install the MySQL system tables and have it 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 and check that it’s running
$ mysql.server start
$ ps aux | grep mysql
Reset the root password. Change the “5.5.27” to your installed version number.
$ /usr/local/Cellar/mysql/5.5.27/bin/mysqladmin -u root password 'YOUR_NEW_PASSWORD'
As we are using the Homebrew package for MySQL and the default php.ini file then PHP is trying to connect to MySQL through the default_socket at /var/mysql/mysql.sock which doesn’t exist as MySQL is using /tmp/mysql.sock. Just change all instances of /var/mysql/mysql.sock to /tmp/mysql.sock.
$ sudo sed -i "" "s:/var/mysql/mysql.sock:/tmp/mysql.sock:g" /etc/php.ini
And you’re done.
Leave a Reply