| Getting CakePHP to play nice with Ubuntu |
|
| Friday, 13 November 2009 15:29 |
|
I'm going to be doing some CakePHP development shortly, this time under Linux instead of Windows in a virtual machine, since the target server this time will be a Linux box. Previously, I'd used the excellent XAMP to get a LAMP server up and running for web development, but since I can now use my trusty Ubuntu box, I'll be planning to work with the default development setup I already have, which is currently used for my Joomla work. While writing the article that explained the process I use for moving the Apache document root under Ubuntu, I encountered a mildly annoying problem: Apache's rewrite module didn't seem to be working. At the time, it wasn't such a big deal, so I just disabled "pretty URLs" on my test Joomla site and went about my day. The problem resurfaced today when I had to get CakePHP working in the same way it would on the final production server, and that needed mod_rewrite. CakePHPInstalling CakePHP is a piece of cake. Extract the CakePHP archive into a folder called "cake" under the web development directory you use in your home folder. The article previously mentioned describes how to setup Apache to run web applications wherever you'd like to put them on your system. Next, create an Apache configuration file in the scripts folder, again under home. The first line of the file creates an alias called "cake" which can be used to access the CakePHP development environment from a browser, simply by typing "http://127.0.0.1/cake". Change the path to suit your user account. Alias /cake /home/neilb/www/cake/ Next, you'll want to symlink the new script into the /etc/apache2/conf.d folder where Apache can find it. sudo ln -s ~/scripts/cake.conf /etc/apache2/conf.d/cake.conf Then, make sure that mod_rewrite is enabled and restart Apache. sudo a2enmod rewrite The last thing to do is to add the /cake alias into the .htaccess files that were part of the CakePHP archive. For example, the .htaccess file in the root folder should look something like this: <IfModule mod_rewrite.c> Add the RewriteBase directive after RewriteEngine, so it looks like this: <IfModule mod_rewrite.c> Then do the same to the two other .htaccess files, under app/ and app/webroot. Adding the RewriteBase directive eliminates the need to mess around with the Apache DocumentRoot directive. Apache's Rewrite moduleThis is where the fun started. Once I'd made the above changes, I could access CakePHP's start page with no problems, but attempting to access a View or Controller that I knew existed would just produce a 404. It took a little while to realise that the reason this was happening is that CakePHP's .htaccess files were being ignored by Apache. This turned out to be because Ubuntu's default Apache install includes the directive "AllowOverride none", effectively disabling mod_rewrite. Opened the "000-default" site configuration file for editing. gksudo gedit /etc/apache2/sites-enabled/000-default Then, change the "AllowOverride none" to "AllowOverride all" under the first two sections, "<Directory />" and "<Directory /var/www/>". Restart Apache and you should be good to go. sudo service apache2 restartblog comments powered by Disqus |
