2012年8月6日星期一

PHP Virtualbox


This guide explains how you can run virtual machines with VirtualBox 4.1 on a headless Ubuntu 12.04 server. Normally you use the VirtualBox GUI to manage your virtual machines, but a server does not have a desktop environment. Fortunately, VirtualBox comes with a tool called VBoxHeadless that allows you to connect to the virtual machines over a remote desktop connection, so there's no need for the VirtualBox GUI.

I do not issue any guarantee that this will work for you!

1 Preliminary Note
I have tested this on an Ubuntu 12.04 server (host system) with the IP address 192.168.0.100 where I'm logged in as a normal user (user name administrator in this example) instead of as root.

2 Installing VirtualBox
To install VirtualBox 4.1 on our Ubuntu 12.04 server, we open /etc/apt/sources.list...

sudo vi /etc/apt/sources.list

... and add the following line to it:

[...]
deb http://download.virtualbox.org/virtualbox/debian precise contrib

Then we download the VirtualBox public key...

wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -

... and update our package database:

sudo apt-get update

Afterwards, we install VirtualBox 4.1 as follows:

sudo apt-get install linux-headers-$(uname -r) build-essential virtualbox-4.1 dkms

(The dkms package ensures that the VirtualBox host kernel modules are properly updated if the Linux kernel version changes.)

Starting with version 4.0, VirtualBox has introduced so called "extension packs" and has outsourced some functionality like remote desktop connection support (VRDP) that was part of VirtualBox packages before version 4.0 into these extension packs. Because we need remote desktop connections to control our virtual machines, we need to install the appropriate extension pack now. Go to http://www.virtualbox.org/wiki/Downloads, and you will find a link to the following extension pack:

VirtualBox 4.1.18 Oracle VM VirtualBox Extension Pack
Support for USB 2.0 devices, VirtualBox RDP and PXE boot for Intel cards.

Download and install the extension pack as follows:

cd /tmp
wget http://download.virtualbox.org/virtualbox/4.1.18/Oracle_VM_VirtualBox_Extension_Pack-4.1.18-78361.vbox-extpack
sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.1.18-78361.vbox-extpack

(Make sure you grab the latest version from the VirtualBox web site.)

(Make sure you run the last command with sudo - sudo VBoxManage extpack install ... - because otherwise you will get an error like this:

administrator@server1:/tmp$ VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.1.18-78361.vbox-extpack
0%...
Progress state: NS_ERROR_FAILURE
VBoxManage: error: Failed to install "/tmp/Oracle_VM_VirtualBox_Extension_Pack-4.1.18-78361.vbox-extpack": The installer failed with exit code 127: Error creating textual authentication agent: Error opening current controlling terminal for the process (`/dev/tty'): No such device or address
administrator@server1:/tmp$
)

Now we must add the user that will run VirtualBox (administrator in this example) to the vboxusers group:

sudo adduser administrator vboxusers

VirtualBox is now installed and ready to be used.
Managing A Headless VirtualBox Installation With phpvirtualbox (Ubuntu 12.04)

1 Preliminary Note
I'm assuming that a headless VirtualBox is already installed on the local Ubuntu 12.04 server, e.g. as described in the tutorial VBoxHeadless - Running Virtual Machines With VirtualBox 4.1 On A Headless Ubuntu 12.04 Server.

I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:

sudo su
  
2 Installing phpvirtualbox
First create a system user called vbox and add it to the vboxusers group:

useradd -m vbox -G vboxusers

Create a password for the vbox user:

passwd vbox

Create the file /etc/default/virtualbox and put the line VBOXWEB_USER=vbox in it (so that the VirtualBox SOAP API which is called vboxwebsrv runs as the user vbox):

vi /etc/default/virtualbox

VBOXWEB_USER=vbox
Next create the system startup links for vboxwebsrv and start it:

update-rc.d vboxweb-service defaults
/etc/init.d/vboxweb-service start

We need a web server with PHP support to serve phpvirtualbox - I'm using Apache2 here. Install Apache2 and PHP5 as follows:

apt-get install apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common apache2 apache2-doc apache2-suexec libapache2-mod-php5 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libapr1 php5-common php5-mysql php5-suhosin php-pear wget

Restart Apache2:

/etc/init.d/apache2 restart

I want to serve phpvirtualbox from Apache's default virtual host with the document root /var/www (I will install it in /var/www/phpvirtualbox) - if you have a different document root, you must adjust the following steps:

cd /var/www
wget http://phpvirtualbox.googlecode.com/files/phpvirtualbox-4.1-7.zip

Unzip phpvirtualbox and rename the phpvirtualbox-4.1-7 to phpvirtualbox for ease of use:

unzip phpvirtualbox-4.1-7.zip
mv phpvirtualbox-4.1-7 phpvirtualbox

Next go to the /var/www/phpvirtualbox/ directory...

cd /var/www/phpvirtualbox/

... and create the file config.php by copying it from config.php-example:

cp config.php-example config.php

Open config.php and fill in the password you created earlier for the vbox system user:

vi config.php

[...]
/* Username / Password for system user that runs VirtualBox */
var $username = 'vbox';
var $password = 'secret';
[...]

That's it already - you can now open a browser and access phpvirtualbox as follows:

http://www.example.com/phpvirtualbox/

The default username is admin, the password is admin as well:
  
This is how phpvirtualbox looks - much like the native VirtualBox GUI:
  
The first thing you should do is change the admin password. Go to File > Change Password:

Type in the old password and specify a new one:

If you know the native VirtualBox GUI, it's now very easy to use phpvirtualbox. For example, if you want to create a new virtual machine, you have the same wizard as you have in the VirtualBox GUI:


没有评论: