Friday, July 31, 2009

Quick guide on setting up a CakePHP project

Read the updated version for CakePHP 1.3 here

Create the database

First we create a database called authcake. Rename it as needed, but be sure to change it in the examples below.

user@host:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 385
Server version: 5.0.67-0ubuntu6 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database authcake;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER
-> ON authcake.*
-> TO 'authcake'@'localhost' IDENTIFIED BY 's3cr3t';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

Download CakePHP

Next we download the latest CakePHP from http://cakephp.org/. The version used in this example is 1.2.3.8166 Stable and the file cake_1.2.3.8166.tar.gz is placed in the homedir.

Extract download

CakePHP will be installed in the ~/public_html/authcake directory, which is served using mod_userdir. The directory will be available at the URL http://yourhost/~username/authcake . Of course it is possible to install it to another location, but be sure to change the locations in the commands below accordingly.

Make sure mod_rewrite is working and that the AllowOverride property for ~/public_html is set to All.

user@host:~$ cd ~/public_html
user@host:~/public_html$ tar xvzf ~/cake_1.2.3.8166.tar.gz
cake_1.2.3.8166/
..........
.  SNAP  .
..........
user@host:~/public_html$ mv cake_1.2.3.8166/ authcake
user@host:~/public_html$ cd authcake/

Configure Database

Rename the default database config and change the values to match those on your system.

user@host:~/public_html/authcake$ mv app/config/database.php.default app/config/database.php
user@host:~/public_html/authcake$ vi app/config/database.php

Update Configuration

Change the setting Security.salt in the core.php file

user@host:~/public_html/authcake$ vi app/config/core.php

Set Permissions

The tmp dir needs write permissions. Use chmod -R 777 only in development environments. In production environments the owner of the files need to be the user running the webserver.

user@host:~/public_html/authcake$ cd app/
user@host:~/public_html/authcake/app$ chmod -R 777 tmp/
user@host:~/public_html/authcake/app$ cd ..

The CakePHP installation should now ready and available.

The result in the browser should look like this:


Friday, July 10, 2009

Easily create a RAM-disk in Linux

This command creates a 2GB RAM-disk and mounts it to /tmp/space.
$ mkdir /tmp/space
$ sudo mount -t tmpfs tmpfs /tmp/space -o size=2G,nr_inodes=200k,mode=01777
You should now have an extra 2GB storage device.
 $ df -h | grep space
$ tmpfs                 2.0G     0  2.0G   0% /tmp/space
You access the temporarily device by it's mountpoint /tmp/space. Needless to say, once you unmount this volume (for instance, with a reboot), the data on it will be gone. Use with care! :) Tested on Ubuntu, should work with any 2.4 or 2.6 kernel...

Tuesday, October 28, 2008

Clearing Linux' Memory Cache

Note to self: Here are some commands you can use to clear the memory cache on a Linux system. The following command sequence will do the following: - display the memory stats - flush filesystem buffers - drop cached memory - display the memory stats again
$ free $ sudo sync $ sudo echo 3 | sudo tee /proc/sys/vm/drop_caches $ free
Or, if you like one-liners:
free; sudo sync; sudo echo 3 | sudo tee /proc/sys/vm/drop_caches; free
This is the ouput that i got. The values in bold are my commands, if you look at the underlined values, you will see the memory drop.
linux01:~ $ free
             total       used       free     shared    buffers     cached
Mem:       2061568    2029968      31600          0     429284    1247252
-/+ buffers/cache:     353432    1708136
Swap:      1036152        104    1036048
linux01:~ $ sudo sync
linux01:~ $ sudo echo 3 | sudo tee /proc/sys/vm/drop_caches
3
linux01:~ $ free
             total       used       free     shared    buffers     cached
Mem:       2061568     724828    1336740          0        340     392092
-/+ buffers/cache:     332396    1729172
Swap:      1036152        104    1036048
I tested this on SLES10 and Centos5 and Ubuntu8.04. My Ubuntu 6.06 and 7.10 boxes do not seem to have the file /proc/sys/vm/drop_caches . Sources: 1 and 2.

Monday, October 27, 2008

Enable Time Sync from inside a Linux VM

Note to self: Command to enable VMware Time Synchronisation on a Linux VM without a GUI (with VMware Tools installed):
vmware-guestd --cmd "vmx.set_option synctime 1 0"
To disable it you have to turn around the last two parameters, like this:
vmware-guestd --cmd "vmx.set_option synctime 0 1"
If you want to apply a setting that is already current you will get the message:
"Invalid old value"
This is tested on RedHat Enterprise Linux 5 and SUSE Linux Enterprise Server 10. Source: Timekeeping best practices for Linux on kb.vmware.com

Thursday, March 20, 2008

Prototype + JSON + PHP - I Got It :)

This week I haven been playing with the great EditArea, a webbased sourcecode editor (examples here). To fetch the data from my PHP server I decided JSON would be the way to go, due to it's clean nature. I soon ran into trouble parsing the JSON data my PHP server threw at the client. It took me a while to figure out how to do this nicely with Prototype, but I found out how at this page (search for responseJSON, which is the magic keyword...) On the server side the key is to make PHP send the header "Content-type: application/json", and print the json_decoded data as response. Prototype can fetch this data by doing an Ajax.Request. On the client side you can let Prototype make an Ajax.Request to the server, and get the property 'responseJSON' from the object you received. Below is a working example. To get it running you need to create these two files, test.html and test.php . You also need the file prototype-1.6.0.2.js in the same directory. I suspect it to work on all Prototype versions from 1.6RC upwards.
test.html
<html>
 <head>
  <title>Prototype/JSON/PHP test</title>
  <script language="Javascript" type="text/javascript" src="prototype-1.6.0.2.js"></script>
  <script language="Javascript" type="text/javascript">
  function LoadFile(id,target){
      new Ajax.Request("test.php?action=load&id="+id, {
          onSuccess: function(transport) {
              var theObject = transport.responseJSON;
              $(target).innerHTML = theObject.text;
          }, method: "get"
      });
  }
  </script>
 </head>
 <body>
  <a href="javascript:LoadFile(1,'filelist')">File 1</a> 
  <a href="javascript:LoadFile(2,'filelist')">File 2</a> 
  <a href="javascript:LoadFile('third','filelist')">File 3</a>
  <div id="filelist"></div>
 </body>
</html>
test.php
<?php
# Make sure this is the first output.
header('Content-type: application/json');

# The list of files.
$files[1]='This is the first file';
$files[2]='And this the second one!';
$files['third']='This must be the third then...';

# Get ID and create array.
$id=$_GET['id'];
$return['id']=$id;
$return['text']=$files[$id];

# Return the JSON string.
echo json_encode($return); 
?>
Code formatted with Format Code. Note: Make sure you don't send any other text (that includes spaces or newlines) before sending the header with PHP! This will cause errors that sound like: "Warning: Cannot modify header information - headers already sent by......" Good luck! :)

Thursday, March 13, 2008

xampp-dsl: A Damn Small LAMP Appliance

Today I ran across a nice Virtual Appliance that consists of the well-known xampp stack (version 1.6.1) installed on top of the tiny Linux distro Damn Small Linux (DSL 3.3). You can get the appliance here . The download weighs in at 154MB and the unpacked size is 379MB.

Saturday, March 8, 2008

KQemu on Ubuntu Gutsy Gibbon

QEMU is an open source processor emulator in which you can run Virtual Machines. It comes with a kernel module called KQemu which accelerates the VM's to 'near native' speeds. Due to some licensing issues KQemu is not distributed and installed with QEMU, though it is easily available in Ubuntu and a lot of other operating systems. This message appears when starting QEMU while KQemu is not loaded:
Could not open '/dev/kqemu' - QEMU acceleration layer not activated
This howto shows how to get KQemu running on Ubuntu 7.10 (Gutsy Gibbon) First install the following packages:
sudo apt-get install kqemu-source kqemu-common build-essential
Then run the following commands:
sudo module-assistant prepare kqemu sudo module-assistant auto-install kqemu
If everything went right (so no errors occurred) you can load the module with this command:
sudo modprobe kqemu
Now check if the module is loaded correctly. If you get a line of output starting with the word kqemu it means the module is loaded properly:
lsmod | grep qemu
To use this module as a user the right permission need to be set:
sudo chmod a+rw /dev/kqemu
You QEMU VM's should now run without the warning above and run a whole lot faster!