PHP UK Conference 2012 schedule – London

This year, for the first time, I will be attending to the PHP Conference in London. There are going to be so many interesting talks.
Have a look with your eyes..
Here you find the official schedule. Unfortunately it’s still provisional.

Anyway I took the time to create a simple spreadsheet to organize, with my colleagues, what talks we will be attending.
Here you can download it: PHP UK Conference 2012 Schedule Spreadsheet.

For those of you who haven’t registered to the PHP conference yet, you can register with a significant discount, instead of the original price, thanks to the PHP London meetup.

I’ll see you there! :-)

Enjoy the conference! :-)

Logical Volume Management basics

I have explained how to activate an LVM logical volume before, but never talked much about LVM itself. Here is a brief introduction.

I have been playing with LVM for about a couple of years now and I must say I have found it very practical, even for managing simple home desktops and servers. The main feature I use is the ability to resize volumes online, but LVM is useful for other things too: creating snapshots, combining multiple disks into a single volume group, and adding redundancy through mirroring.

Physical Volumes, Volume Groups, Logical Volumes

LVM works in three layers:

  • Physical Volume (PV) — a disk or partition that LVM can use. Initialised with pvcreate.
  • Volume Group (VG) — a pool of storage made from one or more physical volumes.
  • Logical Volume (LV) — a virtual partition carved out of a volume group. This is what you format and mount.

Start by preparing a partition (use fdisk to set the type to 8e — Linux LVM):

sudo pvcreate /dev/sdb1
sudo vgcreate my_volume_group /dev/sdb1
sudo lvcreate -L 10G -n my_volume my_volume_group

Now format and mount it:

sudo mkfs.ext4 /dev/my_volume_group/my_volume
sudo mount /dev/my_volume_group/my_volume /mnt

Resizing

Need more space? If you have free space in the volume group:

sudo lvextend -L +5G /dev/my_volume_group/my_volume
sudo resize2fs /dev/my_volume_group/my_volume

The filesystem is resized online — no unmounting required.

Snapshots

LVM snapshots let you take a point-in-time copy of a volume. Useful for backups:

sudo lvcreate -L 1G -s -n my_snapshot /dev/my_volume_group/my_volume

The snapshot starts small and grows as changes are made to the original volume. Remove it when no longer needed:

sudo lvremove /dev/my_volume_group/my_snapshot

Enjoy!

How to run a system command in background from PHP

If you are at the command line and want a process to run in the background, you add an & at the end of the command:

sleep 10 && echo "Done!" &

The same approach works from PHP when you need to run a long process without making the user wait:

exec("tar czf /backup/site.tar.gz /var/www > /dev/null 2>&1 &");

The > /dev/null 2>&1 discards output so PHP doesn’t wait for it. The & sends the process to the background.

PHP functions compared

  • exec() — returns last line of output, does not block if output is redirected
  • shell_exec() — returns full output as a string
  • system() — outputs directly to the browser
  • passthru() — for binary output

Using nohup and PIDs

exec("nohup tar czf /backup/site.tar.gz /var/www > /dev/null 2>&1 & echo $!", $output);
$pid = $output[0];

nohup keeps the process alive after PHP exits. The PID lets you monitor or kill it later.

Enjoy!

jQuery shorthand for the document ready wrapper function

If you use jQuery, you know the $(document).ready() wrapper is the standard way to make sure the DOM is fully loaded before your JavaScript runs. But writing it out every time gets repetitive. There’s a shorthand that does exactly the same thing with fewer keystrokes.

The long version

$(document).ready(function() {
  // your code here
});

The shorthand

$(function() {
  // your code here
});

That’s it. $(function() { ... }) is equivalent to $(document).ready(function() { ... }). jQuery internally maps the shortcut to the same event handler, so there’s no performance difference — it’s purely a readability and typing convenience.

What about the arrow function?

If you’re writing modern JavaScript, you can also use arrow functions:

$(() => {
  // your code here
});

All three forms do the same thing. Pick whichever you find easiest to read.

Enjoy!

Re-open last tab/tabs closed on Google Chrome/Chromium

We’ve all done it. You’re flicking through tabs, your hand moves faster than your brain, and bam — you closed a tab you still needed. You stare at the screen for a second before remembering there’s a keyboard shortcut for that.

Ctrl + Shift + T (or Cmd + Shift + T on a Mac).

That’s it. Press it once and the last tab you closed comes back. Press it again and the one before that comes back. Chrome and Chromium keep a stack of your recently closed tabs in memory, so you can walk backwards through your mistakes as far as you need.

It works across sessions

If you close Chrome entirely and reopen it, Ctrl + Shift + T will restore the last window you had open with all its tabs. This is separate from the “Continue where you left off” startup setting — it’s a one-time restore that works even if you have Chrome set to start on a blank page.

Right-click also works

If you prefer the mouse, right-click on any tab and select “Reopen closed tab” from the menu. Same result, one extra click.

What about the History panel?

For tabs you closed a while ago (not just the last few), go to Menu > History or press Ctrl + H. You’ll see a full chronological list — the “Recently closed” section at the top shows your last few tab closures with their full titles, making it easier to find the one you actually want.

The shortcut is one of those things you don’t think about until you need it. And once you know it, you’ll use it more often than you’d expect. :-)

Install NX server/client on Ubuntu Lucid

My favourite NX server is Freenx.
FreeNX is a GPL implementation of the NX server/client that allows you to graphically access your machine over the Internet.
FreeNX runs natively on SSH, it’s open source and, above all, fast.

So, first of all you need to add the free-nx repository:

sudo add-apt-repository ppa:freenx-team

and then update and install freenx:

sudo apt-get update
sudo apt-get install freenx

The client I use instead, is from NoMachine.
The Linux version can be download from here.
If you use a different Ubuntu version you can follow the Freenx documentation.

Enjoy!

Free 3GB SpiderOak account – (Dropbox alternative)

SpidearOak gives you 2GB to start with. If you want to start with 3GB instead use this link.
Keep reading if you want to find out more.

So, first of all, SpiderOak is a web-based file hosting service “similar” to Dropbox.
You don’t know what Dropbox is? oh well :-). You can have a look at the Dropbox intro video or keep reading..
Anyway, I’ve used Dropbox for a couple of years, until last month.
I was using it to share some files among different computers/phone and I must say that it has always worked well for me.
I always look for software that do things better but, in this case, I was even more encouraged to find an alternative when, last July, Dropbox made its license more restrictive.
As you can see from the last paragraph in the Wikipedia Dropbox criticism “In early July 2011 Dropbox revised their Terms of Service several times after first including language that appeared to give them an unrevokable license to any material uploaded. They subsequently clarified that language in response to the maelstrom of criticism and concern that resulted from artists and corporate users with confidential data.”
The bottom line here is. Dropbox is still a good service but using it for confidential data MIGHT (I repeat myself, MIGHT) be risky.

The Dropbox staff can get access to your data. No one is saying that they do (on a regular basis at least).
So, SpiderOak, is a good and safer alternative to Dropbox.
Safer because SpiderOak based their entire architecture on a zero-knowledge policy and encryption.
I’ve already installed it on three Ubuntu boxes and my Android phone and I can guarantee that is fast, easy to use and has a nice GUI.
SpiderOak-GUI
As you can see from the screenshot, it can be used to backup, synchronize and share data.

Another great thing is that you can get up to 50GB of space for free (with a refer-a-friend program).

There are two huge differences between the SpiderOak and Dropbox refer-a-friend program:
1) SpiderOak gives you 1GB per each friend (not 250MB);
2) SpiderOak gives BOTH of you that extra GB (not the referrer only like Dropbox).

So, for example, this is my SpiderOak referral link. If you subscribe to their service trough that link you get 1GB extra as well as me. That makes you start from 3GB rather than 2 only…

It’s good for me, it’s good for you. I like it that way… :-D
It’s also cross-platform so you can enjoy it on Linux, Mac, Windows, IOS and Android.
You can add as many devices as you want.

Under Ubuntu Lucid (and probably others similar as well), if the “Launch SpiderOak at Login” option does not work for you add it manually:
Preferences -> Startup applications -> Add .
Name: SpiderOak (or whatever you want)
Command: /usr/bin/SpiderOak (which is the result of “which SpiderOak”)
Comment: Online backup/sync solution (or whatever you want)

Once again. Here is the link to open a 3GB SpiderOak account.
That’s all for now. Hope you enjoy your new free sharing service.

Hope it helps!

How to install Firefox 5 on Ubuntu Lucid

The Firefox version bundled with Ubuntu Lucid is still 3.6. It is stable but definitely not the fastest browser around, especially with Chrome and Chromium raising the bar on speed.

The best way to keep Firefox up to date is to let Mozilla do the work with their own repository.

sudo add-apt-repository ppa:mozillateam/firefox-stable
sudo apt-get update
sudo apt-get install firefox

This adds the Mozilla team PPA and installs the latest stable Firefox. After that, updates come through the regular apt-get upgrade cycle.

Enjoy!

How to fix GRUB 2 with Ubuntu live cd

When GRUB fails and your system won’t boot, the errors can be intimidating. But fixing it with a live CD is straightforward.

Boot from an Ubuntu live CD or USB. Once the desktop loads, open a terminal.

First, mount your root partition:

sudo mkdir /mnt/newroot
sudo mount /dev/sdxy /mnt/newroot

Bind the virtual filesystems:

sudo mount --bind /dev /mnt/newroot/dev
sudo mount --bind /proc /mnt/newroot/proc
sudo mount --bind /sys /mnt/newroot/sys

Chroot into your system:

sudo chroot /mnt/newroot

Reinstall GRUB:

sudo grub-install /dev/sdx
sudo update-grub

Exit the chroot, unmount everything, and reboot. Remove the live CD when prompted.

For UEFI systems, the command changes slightly:

sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi

If you can still reach a grub rescue prompt, try booting manually:

set root=(hd0,msdos1)
set prefix=(hd0,msdos1)/boot/grub
insmod normal
normal

Enjoy!

Mount Windows share on Linux

Sometimes I still have to deal with Windows. :-/ This time it’s an office shared disk. I was able to mount it automatically but only root could write in it, which meant I couldn’t paste files from Nautilus.

To overcome this I added the following entry in /etc/fstab:

//192.168.0.1/deviceName$ /mountpoint cifs
domain=hq,username=myUsername,password=myPassword,
_netdev,uid=username,gid=username 0 0

The _netdev option ensures the share is mounted only when the network is available, which is particularly useful on laptops that switch networks.

Once you have changed the fstab, unmount the network drive if it was already mounted:

sudo umount /mountpoint

and then mount it again:

sudo mount -a

If you only need occasional access to a Windows share, smbclient is a quick alternative:

smbclient //192.168.0.1/deviceName$ -U myUsername

This opens an FTP-like interface for browsing and transferring files.

Hope it works for you!