Using a virtual machine to play with multiple versions of PHP
This is an occasion to learn about a virtualization tool which I'm not familiar with, VirtualBox. The goal is to install PHP 5.4, which is not yet a stable release, to play around with new features such as traits without ruining the setup on my primary machine (which runs the super-stable PHP 5.3). Although it may be possible to run them together (I'm not a sysadmin), it's really simpler to install one of them in a virtual machine that can be thrown away if something goes wrong.
I usually run VMware, but this time I wanted to try something else. I heard good feedback about VirtualBox as it is open source and released under GPL2. It has been acquired by Oracle as part of the Sun Microsystems deal, but apart from a change in brand name I don't think it has changed that much its position.
The host operating system in my experiment is Ubuntu 11.10, desktop edition. A .deb package is available on the VirtualBox website, and I selected the i386 version since I'm running a 32 bit host.
I am also using a 32 bit for guest: another Ubuntu 11.10, this time in its server edition; the server version is faster than the desktop one and I don't need an X server to run PHP code.
Creating the machine
I created a new virtual machine by allocating 512MB of RAM and 8 GB of disk. This configuration should be enough for a development server which will never have to support real load.
The hard disk is contained in the image file, and is configured as dynamically allocated; I have no need for particular performance, so this version will just occupy disk space incrementally as it is used by the virtual instance. After the installation of ssh, PHP and its dependencies, the file occupies 1.8 GB.
VirtualBox wizard is very easy to follow, and once I started the machine for the first time I selected the .iso file of Ubuntu Server, which has been mounted and run to install the system from scratch.
As for VMware, switching between the host and the guest consists in clicking between windows; there may be some keyword shortcuts but they usually depend on the host/guest combination. For example, special shortcuts like Host + Del will reboot the guest by sending it a Ctrl + Alt + Del combo. However, working in a terminal continuously, as for all servers, means it's enough to write Unix commands.
I have also started an SSH server directly during the installation phase, to provide me as many consoles I need. Again, it's a server so once it's started we can interact with it as if it were in a remote location, with ssh (but with lower latency).
Note that while the virtual machine is not running you should modify the network adapter in its settings, selecting a Bridged Adapter. This configuration will make the virtual machine pick up a new IP address in the same network as the host, just like if it was attached to Ethernet next to it (this assuming you have a DHCP-capable router.)
Compiling and installing PHP
Well, apart from ssh, an easy to configure PHP environment consists of Apache and a PHP binary. Both would be provided by Ubuntu, but I intended to use an unstable release of PHP. In any case I will always work with ssh so that I can copy and paste commands.
Thus, I installed Apache 2 (non-threaded version) but not the default php core.
sudo apt-get install apache2-mpm-prefork
PHP 5.4 can be downloaded with wget:
wget http://downloads.php.net/stas/php-5.4.0beta2.tar.bz2 tar xvjf php-5.4.0beta2.tar.bz2 cd php-5.4.0beta2
A typical ./configure command for PHP's compilation includes PEAR, some database drivers and other stuff:
./configure --with-config-file-path=/etc/php5/apache2 \ --with-pear=/usr/share/php \ --with-bz2 \ --with-curl \ --with-gd \ --enable-calendar \ --enable-mbstring \ --enable-bcmath \ --enable-sockets \ --with-libxml-dir=/usr \ --with-openssl \ --with-regex=php \ --with-zlib \ --with-apxs2=/usr/bin/apxs2
Note the --with-apxs2 option which will make us able to use PHP not only from the command line but also from within Apache. Some extensions like PDO are included by default and do not need ./configure directives to be enabled.
The configuration phase will make you install some libraries like build-essential (targeting the GNU C compiler), apache2-prefork-dev, and libraries related to the extensions you have selectedi (e.g. libxml2-dev, libcurl4-openssl-dev, libbz2-dev, libpng-dev).
Run again and again ./configure until it does not complain anymore about missing libraries. The make command starts the actual compilation.
make will take forever to complete - especially in a virtual machine which has to share the CPU with its host. Start it and then leave it there compiling during lunch.
checkinstall (which should be installed as well) produces a custom .deb package: it's an alternative to *make install*. Just run it in the source folder after compilation.
sudo checkinstall dpkg -i php*.deb
Restart apache to see PHP in action:
If you want to help, run also PHP's test suite with make test and allow the program to send the result back to the PHP QA team. It's normal to get a red bar - many bugs or improvements are documented with a failing test which will be fixed in the next releases.
When you're finished playing with the VM, you have different options:
- shutdown is the normal thing to do: a signal is sent like if you pushed the power button; you can also execute shutdown -h now on the server.
- Saving the VM state takes a snapshot is of RAM and registers, so that you'll resume from the same state when you reopen VirtualBox. It's like hibernation, but performed from the hypervisor instead of the guest system.
- Powering the machine off is the equivalent of pulling the power cable, so do it only if you want to simulate a crash.