Setting up cakePHP on Mac OS X
It’s actually quite simple to set up cakePHP with Mac OS X because most of the functionality you need comes already shipped with your system. Therefore I’m showing here how to use as much of the built in software such as Apache as possible to get cake up and running. Of course, alternatively you could use a local development environment such as MAMP. But why bother with an additional piece of software when Apache & PHP are already integrated in your Mac? And also you have greater flexibility when it comes to using virtual hosts (unless you buy the Pro version of MAMP). I recommend to give it a try - it’s not that hard: just follow these steps:
Prepare your Mac with Apache, PHP and MySQL
- Activate your built-in apache webserver by going to the system settings -> Sharing -> Web-Sharing and checking the box to enable it
- In the web-sharing options it should now give you an URL of the website of your computer and your personal website. Try accessing both by clicking on the links. The first link should give you an “Apache it works screen”. The second link should show you an intro page in Mac design.
- If you get the error message “Forbidden” on the second like (I did…) follow these steps to fix this. One note: Open finder and use the “Go to” feature to get to the directory /etc/apache2/users/
- Now our webserver is running and we can display static html webpages. The path where your website is stored is: /Users/yourusername/Sites/
- In order to run cakePHP we need to activate PHP. This guide basically gives you all the info you need to succeed with this task. In this article you will also find useful information on working with virtual hosting (ie having multiple websites on your computer).
- Next we want to install MySQL. This is described in the article above as well, but since this is already about a year old life is actually much easier. Just download the latest MySQL Package and start the installer. Then start the MySQL server from the command line or control panel and give your root user a new password.
- What is always nice to have is phpMyAdmin - this is a great web interface to control your MySQL database. Download it and put it in a directory of your Websites folder. From there you can access it using the URL: http://localhost/~yourusername/thedirectory/
Installing cakePHP
- Download the latest version of cakePHP (use the .dmg File).
- Mount the file, copy its content to a directory in your Websites folder and unmount the file again.
- If you point your browser to this directory (as shown above) you should be able to see the first output of cakePHP - and quite some errors. So let’s fix those:
Warning (512): /.../app/tmp/cache/ is not writable
To fix this you have to make the cache directory writeable. In fact you should make the whole tmp directory writeable. Go to your app/tmp directory and change the read/write permissions for “everyone” to read & write (do this for all subfolders as well using the option just below the permissions field).
Notice (1024): Please change the value of 'Security.salt'...
For this just go to your app/config/core.php file and change the value of the string where it says:
Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');
and change the value of the string to something else. The length doesn’t matter, but I recommend to keep it this length and also in this form (random).
Your database configuration file is NOT present.
To configure the database rename the file database.php.default to database.php and set the parameters in it according to your setup. E.g. you can add a new user with a new database using phpMyAdmin and set those values in the database.php
Now all error messages should be gone. But there is still one thing to do - the design is not working as it is plain text only and the CSS does not seem to load. To fix this we need to set the DocumentRoot of Apache to the /app/webroot directory. To do this we have to define a virtual host and the corresponding DocumentRoot. How to do this is explained here again. Just extend the path of the virtual server with /app/webroot in your username.conf as shown in the guide and things should work. Don’t forget to restart your Apache (by deactivating and activating the Web-Sharing in your control panel) for the changes to take effect!
And then… there is actually one more last thing. Apache on Mac OS X is by default configured not to work with .htaccess files! That’s something we have to change in order to get cakePHP to work. And because we have a virtual server environment set up we can do this in the username.conf file again. Add the following bold lines of code for every virtual host definition:
<virtualhost *:80> DocumentRoot /Users/yourusername/Sites/pathttocake/app/webroot ServerName pathtocake <Directory "/Users/yourusername/Sites/pathtocake"> Options Indexes FollowSymLinks AllowOverride All </Directory> </virtualhost>
Now everything should be set up! If anything was unclear please comment on this post - I’ll try to answer your questions and/or improve the describtions!