Installing Apache on Mac OS X

Mac OS includes Apache and PHP, which is a bit handy for us developers. We all need a local environment and it helps if the local environment looks and feels a bit like the terminal ssh session we use to manage our production server. Configuration can be a little tricky however, and things change from version to version of the OS…

LAMP?

We’ll end up with a LAMP stack (more or less). LAMP is an acronym for:

  • Linux
  • Apache
  • MySQL (or MariaDB, MongoDB)
  • PHP (or Perl or Python)

Whilst Mac OS is not Linux, it’s Unix-based so it’ll do…

Steps

  • Check Apache
  • Enable PHP
  • Add virtual host configuration
  • Restart and check configuration
  • Install MySQL
  • Configure MySQL
  • Install additional tools

Check and Run Apache

Only run the start command if required…

$ ps aux | grep httpd

...
_www              431   0.0  0.0  2463684   1744   ??  S     8:23pm   0:00.00 /usr/sbin/httpd -D FOREGROUND
...

$ sudo apachectl start

Enable PHP

$ sudo vi /etc/apache2/httpd.conf

...
# Ensure this line is uncommented
LoadModule php5_module libexec/apache2/libphp5.so
...

Virtual Host Configuration

$ less /etc/apache2/users/myusername.conf

<VirtualHost *:80> 
        DocumentRoot "/Users/myusername/Sites" 
        ServerName myusername.local 
        ErrorLog "/private/var/log/apache2/myusername.local-error_log" 
        CustomLog "/private/var/log/apache2/myusername.local-access_log" common 

        <Directory "/Users/myusername/Sites"> 
                AllowOverride All 
                Order allow,deny 
                Allow from all 
        </Directory> 
</VirtualHost>

Check Your Work

Restarting Apache allows us to check this configuration is working. Create a directory “test” in /Users/myusername/Sites. Create a “hello world” index.html.

Restart Apache

$ sudo apachectl restart

Navigate to http://localhost/test/index.html. You should see your “hello world”.

403 Forbidden…

Argh! Pain! Apache runs under the _www user. This user needs to have 755 permissions to each directory in the heirarchy your site is located in. Chmod away…

$ chmod 755 /Users/myusername/Sites
$ chmod 755 /Users/myusername
$ etc...

Installing MySQL

MySQL is now distributed as a nice DMG package. Download it, install MySQL (open from the context menu if OSX complains about the legitimacy of the developer), and then install the preferences panel which adds MySQL to your preferences. Nip over to the shiny new settings pane and switch on MySQL. Check it out – it runs under a system user called mysql which has been available in MacOS installs for some time now.

$ ps aux | grep mysql
_mysql 1065 ... /usr/local/mysql/bin/mysql

Additional Tools

At least install a database admin tool locally. PHPMyAdmin does the job admirably – you’ll install it hosted in your new Apache PHP environment so there’s a nice circularity there… Alternatively use a MacOS database management application.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.