If you find this post useful, please consider donating in the form of a
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: The 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 — important 🙂
- PHP 5: without PHP, there’s just Cake, which is pretty good on its own, but we want CakePHP.
- 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. It can be whatever we want. Ah, the possibilities…
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: Get CakePHP
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 to /var/www
. git
is a tool that we use for something called version control or revision control. Put very simply, revision 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 codebase. For the purposes of this guide, we can leave it at that. What we’re going to do is clone CakePHP from github.org. This downloads a copy (or clone, get it?) of CakePHP to a directory of our choosing on our server. Also, an added bonus is that no extraction (or unzipping) 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 CakePHP should copy (or clone) down 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 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 do 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. The original purpose of .htaccess was access control, but now .htaccess 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 site, and though it represents a little bit more work, having multiple .htaccess files is a 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 must be changed. This is the AllowOverride setting, and it 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>
needs to 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 enable. 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: Testing
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.
36 comments: On Install CakePHP on Ubuntu 10.10 and 11.10
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.
Thank you Paresh. I’m glad you enjoyed it.
Thank you so much. Useful tutorial for begineer like me.
Thank you for reading the guide. I’m glad you found it useful as well.
I agree with Paresh, it’s a very useful guide, good job!!!
Greetings from Switzerland!
A sincere “thank you” to you as well, and likewise, greetings from Philadelphia.
Dude thanks… !!!the best tutorial ever!!!.
I’m glad you found it useful!
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?
Good to hear! I just tested and updated the title.
Thank you easy guide but i there is issues with the color font despite that is crazy guide thanks again
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!
Well, I think this is the best tutorial about cake’s installation on the net. I’ve read many and none of them worked as good as this one – quick, easy and with no errors.
Hope this site will never go offline!
I appreciate that! As long as I’m around, this site will stay online. Thanks!
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 !
Hi Michael really nice tutorial . Real easy to understand.
Thank you for the kind words.
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.
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.
An effective way to get everything going plus know what is going on!
That’s what I’m shooting for!
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 ..
My advice is to double-check and make sure you set
AllowOverride
toAll
in your/etc/apache2/sites-available/default
file. This is detailed further in this tutorial.Excellent guide, i found it very useful thanks.
thanks man ….a lot………
thanks bro……
Michael u have given a usefull tutorial which is not propery available in officeal sites…. This is good…
Thank you…
Great Work!
Thank you!
Very long time I could not install CakePHP on my Ubuntu and this post very helped me. Big thanks 🙂
From Russia with love 🙂
Thanks!
Its a best explanation i have see for cakephp installtion
Nice tutorial
It also works for Ubuntu 12.04, thanks !
No problem, the pleasure is mine.
Pingback: How To Install CakePHP On An Ubuntu 12.04 VPS | radheyipec ()