Archive

Posts Tagged ‘PHP’

Setting up cakePHP on Mac OS X

November 13th, 2008

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

  1. Activate your built-in apache webserver by going to the system settings -> Sharing -> Web-Sharing and checking the box to enable it
  2. 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.
  3. 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/
  4. Now our webserver is running and we can display static html webpages. The path where your website is stored is: /Users/yourusername/Sites/
  5. 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).
  6. 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.
  7. 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

  1. Download the latest version of cakePHP (use the .dmg File).
  2. Mount the file, copy its content to a directory in your Websites folder and unmount the file again.
  3. 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!

cakePHP , , , ,

Web Development Frameworks - cakePHP vs. ZEND

November 13th, 2008

If you want to create websites fast, and I mean really fast you sooner or later will look into using a framework. Frameworks are a great way to save time and nerves because they have a lot of often used functionality (e.g. user login etc.) already built in and also help you by providing a certain structure for your coding.

There are a lot of web development frameworks out there. One of the best known is probably Ruby on Rails. What I’ve seen this framework is really great, so if you happen to have no programming experience at all and want to learn a new programming language from scratch RoR is definitely going to be a good choice. I, for my part, have been coding PHP since 5 years or so and didn’t feel like learning a new language. But that’s not a big problem because there are several frameworks available for PHP as well. But which one to choose? I first had a look at the ZEND framework which is very popular. But I wasn’t really satisfied with it and kept looking. I finally found cakePHP and have never thought about switching to something else since then…

CakePHP

cakePHP is sometimes refered to as a PHP clone of RoR. I cannot comment on this really, because I have never used RoR myself but some features (e.g. Scaffolding) seem to be very similiar indeed. What is so great about the cakePHP framework is that you basically build your website based entirely on the framework. With ZEND you have far more freedom (e.g. with the database model) and that’s why it doesn’t support the development process as much. Another great thing about cakePHP is the community - although documentation is sometimes out of date, there are great tutorials and discussion groups to help you learning cakePHP and with problems that might occur.

cakePHP , , ,