Libvirt-php

libvirt-php, originally called php-libvirt, is a project that was started by Radek Hladik in 2010 to integrate libvirt support to PHP.

In February 2011 the binding was moved to libvirt.org.

This project is not affiliated with The PHP Group and the PHP project itself.

What is libvirt-php?

libvirt-php is a php module that provides PHP bindings for libvirt virtualization toolkit and therefore you can access libvirt directly from your PHP scripts with no need to have virt-manager or libvirt-based CLI/GUI tools installed.

Using libvirt-php

You can use libvirt-php extension to manage your domains (virtual machines) from your PHP scripts. This could be very useful if you manage several physical machines that are all serve like hosts for virtual machines. If all the host machines are running Apache webserver then you could easily write the management scripts using the libvirt-php extension to manage all of the virtual machines on the host.

Also, if you have the libvirt authentication setup to be able to remotely connect directly from the PHP script then management tool could be present just on one host machine providing the remote connectivity to all other hosts to manage all of the virtual machines in your environment.

Checking whether libvirt-php is installed

Once you have the libvirt-php module loaded in your PHP configuration you can test it's presence using a standard PHPInfo() output where you could be able to see libvirt extension information if enabled or you can try to look for libvirt string in the php -m output (if you're having the CGI/CLI-based version of PHP installed on your system) or you can automate it e.g. by using following command:

$ php -m | grep libvirt > /dev/null; echo $?

The second part after the semicolon is the next command to be processed and this will redirect the grepped php -m to null device so it will not show the command output. Instead, it will show the error code of the grep command which will be 0 for case the extension is installed properly or number 1 if the extension was not found in your PHP configuration.

If you don't have access to CGI/CLI-based version version of PHP you can use a simple PHP script like:

<?php
    print_r( libvirt_version() );
?>

If you have libvirt-php binding installed and binding is working successfully you should see output similar to following in your PHP script::

Array
(
    [libvirt.release] => 3
    [libvirt.minor] => 8
    [libvirt.major] => 0
    [connector.version] => 0.4.1
    [connector.major] => 0
    [connector.minor] => 4
    [connector.release] => 1
)

Keys in this output are saying what version of libvirt and libvirt-php you are using. The keys beginning with libvirt. are libvirt daemon related and the connector. keys are referring to libvirt-php since libvirt-php is just a connector to libvirt daemon so therefore it's called connector in the output.

If you don't have the extension installed properly you will see the error that the function libvirt_version() is unknown or even you may see error page instead if PHP configuration is bad and your PHP or Apache dies on it.