Install CakePHP on Ubuntu 10.10 and 11.10

CakePHP is a framework that allows for rapid PHP development. I recently installed it on one of my home test servers and there were a few steps specific to an Ubuntu install that were required but that were not clearly outlined in the official documentation.

The version of the Apache web server that gets installed via the package manager in Ubuntu (I prefer apt-get personally) compartmentalizes many of the Apache configuration files, so rather than editing a single httpd.conf file, we unfortunately have to hunt and peck for individual configuration files and modify them as needed.

This install guide begins immediately after a base install of Ubuntu has been completed. It takes us through installing the fundamentals, and ends with a working install of CakePHP, ready for development work. It assumes our server is dedicated to a single CakePHP-based website. Since this is an internal, home-based test server setup, I don’t put as much emphasis on securing the server, so bear that in mind as you work through this. You or your organization may have some additional security measures to take after getting CakePHP up and running.

Phase I: Basics

Assuming we’ve just completed a base install of Ubuntu (Server or Desktop, it doesn’t really matter) with a static IP or reservation via DHCP, we now have a few basic packages to install. They are:

  • Apache: our web server. This is important. Without it, nothing else will work.
  • PHP 5: another foundational piece of software. Without PHP installed, CakePHP won’t work.
  • MySQL: our database management client and engine.
  • Vim: our text editor.
  • Git: the tool we use to copy (or clone) CakePHP from github.com.
  • OpenSSH Server: for convenient remote shell access.

Note: MySQL and OpenSSH Server are not used in this tutorial, but they’re good to have installed. The reality is, eventually you might need a database for your CakePHP-based website, and at some point, you’re probably going to want secure access to your web server from a remote terminal.

To install the aforementioned packages, all we need to do is access our terminal and type:

regularuser@smalleycreative:~$ sudo apt-get install apache2 php5 php5-mysql mysql-server vim git openssh-server

During the install, we’re asked what we want our MySQL root password to be. We can set it to whatever we want, and make a note of it for later. Please resist the urge to sticky it to your monitor.

Now is a good time to do a quick restart of the Apache service, in case it doesn’t see that PHP is present. If Apache can’t see PHP, PHP can’t be used, and if PHP can’t be used, then CakePHP won’t work. To restart Apache, we type:

regularuser@smalleycreative:~$ sudo service apache2 restart

Phase II: Acquire Cake

First, some background information: There are two official ways of getting and working with CakePHP. We can either manually download CakePHP and copy it into our web root (this is where we store our websites on a web server. On Ubuntu this is located at /var/www), or we can use git to clone CakePHP from github.com into /var/www.

Note: Git is a tool that we use for something called version control or revision control. Put very simply, version control is a concept that allows developers to maintain different versions of their code in a more organized fashion. It provides a level of protection against errors and allows for a more robust method of managing changes to a code base. That’s all you need to know for the purpose of this guide. We are going to clone CakePHP from github.org. This downloads a copy (or clone, get it?) of the CakePHP code repository to a directory of our choosing on our server. Also, an added bonus is that no decompression is needed.

We can do this completely from the Ubuntu terminal by typing:

regularuser@smalleycreative:~$ cd /var/www
regularuser@smalleycreative:~$ sudo git clone https://github.com/cakephp/cakephp.git

Within a few seconds, the most current version of the CakePHP code should be downloaded from github.com to our /var/www directory. Cool, huh?

Phase III: Configuration

The final step in getting CakePHP up and running is to make a few configuration changes to our server. The first thing we need to do is modify permissions on tmp, a directory in the CakePHP source code repository that should now be located at /var/www/cakephp/app/tmp. More specifically, we need to ensure that this directory and all directories within it are writable by the web server user. A quick and dirty way to do this is:

regularuser@smalleycreative:~$ sudo chmod 0777 -R /var/www/cakephp/app/tmp

Once that is out of the way, we need to modify a configuration file located at /etc/apache2/sites-available/default. Most tutorials on how to install CakePHP tell us to modify our httpd.conf file, but in our case, we’re not interested in httpd.conf, we’re interested in /etc/apache2/sites-available/default. As I stated earlier, Apache under Ubuntu is set up a bit differently, and configuration files are more compartmentalized. There are two main changes we need to make to our default file. To make these changes, we can open default in vim by typing:

regularuser@smalleycreative:~$ sudo vim /etc/apache2/sites-available/default

Once in vim, we need to modify two lines in our default configuration file. First, we modify something called our DocumentRoot. The DocumentRoot is the directory where our main URL redirects. By default, it points to /var/www. CakePHP documentation states that our DocumentRoot must point to /var/www/cakephp/app/webroot. To achieve this:

DocumentRoot /var/www

should be changed to

DocumentRoot /var/www/cakephp/app/webroot

Before we continue, a brief explanation is required. In Apache, a file called .htaccess can be placed in any website. Unsurprisingly, the original purpose of the .htaccess file was access control, but now it controls a lot more than just access. In our case, CakePHP uses .htaccess to rewrite and shorten what would otherwise be long, complex URLs. Most servers have a master (or global) .htaccess file, and many have individual (or local) .htaccess files located in individual directories in the /var/www directory tree that control access on a per website basis. This allows for more a more fine-grained configuration on servers that run more than one website, and though it takes a little bit more work, having multiple .htaccess files is the preferred method of configuration because of the level of control it affords the server administrator. Before Apache will allow local .htaccess files to take precedence over the global .htaccess file, a configuration setting called AllowOverride must be changed. This setting is located in our default configuration file as well. We need to change AllowOverride from None to All. This instructs Apache to allow local .htaccess files to override the global .htaccess file. To do this:

       <Directory />
               Options FollowSymLinks
               AllowOverride None
       </Directory>

must be changed to

       <Directory />
               Options FollowSymLinks
               AllowOverride All
       </Directory>

Once this change is made, we’re done with the default file, so we can save and quit vim using :wq.

Now that our configuration file is set up, we need to enable mod_rewrite. mod_rewrite is the Apache module that allows Apache to shorten URLs. Apache will not allow this behavior by default, it’s something we need to explicitly configure. Whenever we hear someone say, “Enable mod_rewrite”, what they’re telling us is to turn on this module so that Apache can make changes to URLs. To do this, we type:

regularuser@smalleycreative:~$ sudo a2enmod rewrite

And we get some output telling us mod_rewrite is being enabled and that we should restart Apache to apply the change:

Enabling module rewrite.
Run '/etc/init.d/apache2 restart' to activate new configuration!

We finish our configuration phase with a restart of Apache to apply all of the configuration changes we’ve made:

regularuser@smalleycreative:~$ sudo service apache2 restart

Phase IV: Test!

To test, we open a web browser and type in the IP address or hostname of our server in the URL bar. Assuming everything is properly set up, we should see a CakePHP template page complete with CSS styling and a few small images. There may be some notices or warnings on this page, but we should still be in good shape to begin development using CakePHP.

Comments

35 responses to “Install CakePHP on Ubuntu 10.10 and 11.10”

  1. paresh Avatar
    paresh

    This is a really nice guide. More so because, u have explained each step so one can understand things and not do them blindly.

    Thanks a lot.

    1. Michael Smalley Avatar

      Thank you Paresh. I’m glad you enjoyed it.

  2. jemparing Avatar
    jemparing

    Thank you so much. Useful tutorial for begineer like me.

    1. Michael Smalley Avatar

      Thank you for reading the guide. I’m glad you found it useful as well.

  3. Miro Avatar
    Miro

    I agree with Paresh, it’s a very useful guide, good job!!!
    Greetings from Switzerland!

    1. Michael Smalley Avatar

      A sincere “thank you” to you as well, and likewise, greetings from Philadelphia.

  4. snake_892 Avatar
    snake_892

    Dude thanks… !!!the best tutorial ever!!!.

    1. Michael Smalley Avatar

      I’m glad you found it useful!

  5. Dolgion Avatar

    Thanks for the guide. It’s very understandable. I successfully got it up and running in Ubuntu 11.10, so maybe you should update the title of your post?

    1. Michael Smalley Avatar

      Good to hear! I just tested and updated the title.

  6. mrzee Avatar

    Thank you easy guide but i there is issues with the color font despite that is crazy guide thanks again

    1. Michael Smalley Avatar

      Thank you for your suggestion regarding the font color. As a result, I have edited the site so that code and CLI examples are easier to read. Enjoy!

  7. Michael Smalley Avatar

    I appreciate that! As long as I’m around, this site will stay online. Thanks!

  8. Alex Avatar
    Alex

    Hi !!
    Nice job Michael , I completely agree with asd !
    This the best tutorial I’ve ever read on the net about Cakephp ! Thank you for such a nice job and I strongly encouraged you to go on sharing your knowledges this way !

  9. Varun Avatar
    Varun

    Hi Michael really nice tutorial . Real easy to understand.

    1. Michael Smalley Avatar

      Thank you for the kind words.

  10. Billy Moon Avatar
    Billy Moon

    Great tutorial, I especially appreciate the effort gone into explaining each command in detail – and the site is clean and easy to read also. Good job. Thanks.

    1. Michael Smalley Avatar

      Thanks for the kind words. I put a lot of time and focus into making sure my guides are easy on the eyes. As a stickler for good design and readability, I’ve learned that I am quite the rarity in my field of systems administration.

  11. Rad Avatar
    Rad

    An effective way to get everything going plus know what is going on!

    1. Michael Smalley Avatar

      That’s what I’m shooting for!

  12. ashish Avatar
    ashish

    when i am trying to load ” localhost/cakephp ” i got an error.. that is “Missing Controller”. and it’s say “Notice: If you want to customize this error message, create app/View/Errors/missing_controller.ctp”
    how to deal with this error.. pls help me ..

    1. Michael Smalley Avatar

      My advice is to double-check and make sure you set AllowOverride to All in your /etc/apache2/sites-available/default file. This is detailed further in this tutorial.

  13. nick Avatar

    Excellent guide, i found it very useful thanks.

  14. ayush Avatar
    ayush

    thanks man ….a lot………

  15. ayush Avatar
    ayush

    thanks bro……

  16. Vignesh Avatar
    Vignesh

    Michael u have given a usefull tutorial which is not propery available in officeal sites…. This is good…
    Thank you…

  17. Ashish Paliwal Avatar
    Ashish Paliwal

    Great Work!

  18. Andrey Avatar
    Andrey

    Very long time I could not install CakePHP on my Ubuntu and this post very helped me. Big thanks 🙂
    From Russia with love 🙂

  19. Lucas Avatar
    Lucas

    Thanks!

  20. Vijay Avatar
    Vijay

    Its a best explanation i have see for cakephp installtion

  21. Shivdhwaj Avatar
    Shivdhwaj

    Nice tutorial

  22. Maxi Avatar
    Maxi

    It also works for Ubuntu 12.04, thanks !

    1. Michael Smalley Avatar

      No problem, the pleasure is mine.

  23. […] Apache for the changes to take effect: sudo service apache2 restart Cakephp installation on ubuntu other ref for cake php installation on ubuntu one more […]

Leave a Reply

Your email address will not be published. Required fields are marked *