Jump to content

Installing VirtualBox + phpVirtualBox on Ubuntu Server 16.04 LTS


brent
 Share

Recommended Posts

The Overview  ]

This blog was written to provide a step-by-step tutorial on deploying a headless VirtualBox installation on Ubuntu Server 16.04 LTS that can be easily managed from a web-based GUI. 

 
Picture
 

Picture

Considerations ]

Performance: VirtualBox is a Type 2 Hypervisor that runs as an application on the host operating system. Depending on your use case, it may make more sense to utilize a Type 1 Hypervisor (e.g. Kernel-based Virtual Machine) that eliminates the overhead of the host OS. 

Prerequisites ]
  • Fresh installation of Ubuntu 16.04 LTS
  • Basic understanding of Linux command-line interface
[ Dependencies ]
  • VirtualBox  (Free and open-source hypervisor developed by Oracle)
  • phpVirtualBox  (Web-based front-end to VirtualBox written in PHP)
  • Apache2  (Web server to host phpVirtualBox)
  • ufw  (Firewall to secure web server)
Installing VirtualBox ]

Once you have installed Ubuntu 16.04 LTS, let's update our package lists and make sure we are fully patched. 
sudo apt-get update
sudo apt-get upgrade
The next step is to install the latest VirtualBox build from the Ubuntu repositories. 
sudo apt-get install virtualbox
From here, we can download the latest VirtualBox "extension pack" to add some additional closed source functionality to VirtualBox that cannot be obtained from the repositories. You will want to confirm your version of VirtualBox using "vboxmanage --version" before proceeding. At time of writing, the repository installed version 5.0.24.

https://www.virtualbox.org/wiki/Downloads 
Picture
 
cd /tmp
wget 
http://download.virtualbox.org/virtualbox/5.0.24/Oracle_VM_VirtualBox_Extension_Pack-5.0.24-108355.vbox-extpack
sudo vboxmanage extpack install Oracle*.vbox-extpack
Picture
 
Now we will create a service account called "vbox" to manage our VMs. To leverage certain resources on the host, I am also adding the vbox user to the cdrom, dialout, floppy, and audio groups. 
sudo useradd -m vbox -G vboxusers,cdrom,dialout,floppy,audio
sudo passwd vbox
 We will also define the VBOXWEB_USER in /etc/default/virtualbox.
sudo vi /etc/default/virtualbox

VBOXWEB_USER=vbox
Picture
 
By default, the vboxweb service runs as the root user. The next step is to adjust the vboxweb service to specify the correct PID directory, user, and group.
sudo systemctl edit vboxweb

[Unit]
After=
After=network.target virtualbox.service


[Service]
User=
User=vbox
Group=
Group=vboxusers
ExecStart=
ExecStart=/usr/bin/vboxwebsrv --pidfile /home/vbox/vboxweb.pid --background
PIDFile=
PIDFile=/home/vbox/vboxweb.pid
We are ready to enable the vboxweb service. Let's reboot to confirm it is working as expected on startup. 
sudo systemctl daemon-reload
sudo systemctl enable vboxweb
sudo systemctl start vboxweb
Installing phpVirtualBox ]
Before we can install phpVirtualBox (web-based GUI for VirtualBox), we need to install Apache2, PHP, and unzip.
sudo apt-get install apache2 build-essential php libapache2-mod-php php7.0-soap php-pear unzip
At this point, the Apache2 installation should be working already. You can test it by going to the IP address of your Ubuntu server in a web browser. 
Picture
 
From here, we will download phpVirtualBox and extract it to the root of the web server.
cd /tmp
wget "http://sourceforge.net/projects/phpvirtualbox/files/latest/download" -O phpvirtualbox.zip
unzip phpvirtualbox.zip
sudo mv phpvirtualbox-* /var/www/phpvirtualbox
 
We need to update the config.php file with the username and password for our "vbox" service account, as well as redefining $location from the IPv4 loopback address to the localhost DNS name.
cd /var/www/phpvirtualbox
sudo mv config.php-example config.php
sudo vi config.php

var $username = 'vbox';
var $password = 'your_password_here';
var $location = 'http://localhost:18083/';
Picture
 
We want to adjust the DocumentRoot to /var/www/phpvirtualbox.
cd /etc/apache2/sites-available
sudo cp 000-default.conf phpvirtualbox.conf
sudo vi phpvirtualbox.conf


/var/www/phpvirtualbox
Picture
 
Let's disable the default Apache2 site and enable the phpVirtualBox site.  
sudo a2dissite 000-default
sudo a2ensite phpvirtualbox
sudo systemctl reload apache2
If everything went according to plan, we should be able to login to phpVirtualBox.

Username: admin
Password: admin
Picture
 
Once logged in, please select the "File" menu and change the default password for phpVirtualBox.
Configuring Startup and Shutdown for VMs  ]
VirtualBox does not automatically suspend virtual machines on shutdown nor will it resume virtual machines on startup. We can fix that using two simple scripts. The first script will export a list of running VMs once a day. The second script runs as a service and will suspend the list of VMs on shutdown and resume the list of VMs on startup. 
sudo mkdir /opt/vbox
sudo chown vbox:vboxusers /opt/vbox
sudo vi /opt/vbox/check_vm_state.sh


# !/bin/bash
# export list of running VMs
vboxmanage list runningvms | sed "s/\"\(.*\)\".*/\1/" > /opt/vbox/runningvms.txt
Let's mark this script as executable and adjust our vbox account's crontab to run it once a day at midnight. 
sudo chmod +x /opt/vbox/check_vm_state.sh
sudo crontab -u vbox -e

00 00 * * * bash /opt/vbox/check_vm_state.sh
Now we are ready to create our "StartVM" script. It uses the exported list of running VMs (generated by the nightly cron job) to start VMs on boot or save state on shutdown. If you would prefer a traditional power off, you can modify the VBoxManage command during the stop) scriptblock and change savestate to acpipowerbutton.
sudo vi /usr/bin/StartVM
#!/bin/bash
# /usr/bin/StartVM

case "$1" in
  start)
    while read line; do
       echo "Starting VirtualBox VM - $line"
       sudo -H -b -u "vbox" /usr/bin/VBoxManage startvm "$line" --type headless
       sleep 1
    done < /opt/vbox/runningvms.txt
    ;;
  stop)
    while read line; do
       echo "Saving state of Virtualbox VM - $line"
       sudo -H -u "vbox" /usr/bin/VBoxManage controlvm "$line" savestate
       sleep 1
    done < /opt/vbox/runningvms.txt
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

exit 0
 The next step is to create a service file for our StartVM script.  
sudo vi /etc/systemd/system/StartVM.service

[Unit]
Description=VirtualBox Start/Stop VM Service
After=virtualbox.service

[Service]
Type=forking
ExecStart=/usr/bin/StartVM start
ExecStop=/usr/bin/StartVM stop

RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Finally, we can mark our script as executable and enable the new StartVM service. 
sudo chmod +x /usr/bin/StartVM
sudo systemctl daemon-reload

sudo systemctl enable StartVM
Picture
 
Securing phpVirtualBox ]

The final step is to secure the phpVirtualBox installation. In the ideal world, you would have a valid HTTPS certificate (e.g. LetsEncrypt.) If not, a self-signed HTTPS certificate will suffice. Additionally, we will enable some basic firewall rules to limit access to the phpVirtualBox installation.

Enabling HTTPS / SSL

If you would like to install a self-signed certificate, it is a relatively simple process. Given the nature of HTTPS, you will receive warnings in your browser about an insecure connection. Despite these off-putting warnings, a self-signed certificate for HTTPS will offer more security than standard HTTP because your VirtualBox password is encrypted in transit.

Let's start off by generating a self-signed SSL certificate and enabling the Apache2 SSL mod. 
sudo make-ssl-cert generate-default-snakeoil --force-overwrite
sudo a2enmod ssl
The next step is to disable the default SSL site and modify our existing phpvirtualbox.conf to enable HTTPS.   
cd /etc/apache2/sites-available
sudo rm phpvirtualbox.conf
sudo vi phpvirtualbox.conf


<VirtualHost *:80>
      Redirect permanent / https://192.168.1.124
</VirtualHost>

<VirtualHost *:443>
      DocumentRoot /var/www/phpvirtualbox
      SSLEngine on
      SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
      SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>
Don't forget to update the HTTP to HTTPS redirect to the correct DNS name or IP address. To test the changes, let's reload the Apache service!
sudo systemctl reload apache2
Enabling Firewall (ufw)

Now that we have a working web server, we need to properly secure it. Enabling the firewall can also accidentally disable your SSH access. The rules below allow SSH, HTTP, and HTTPS from all addresses. You will likely want to be more restrictive.
sudo ufw allow ssh
sudo ufw allow http

sudo ufw allow https
Once you are comfortable with your firewall rules, we can enable the firewall permanently. 
sudo ufw enable
Creating A Virtual Machine ]
Congratulations! You should be ready to create your first virtual machine. 
Picture

 

Error Version phpvirtualbox work around

cd phpvirtualbox/
cd ./endpoints/lib/

edit: config.php

find and repllace (comment out old, add new):

//define('PHPVBOX_VER', '5.0-5');
define('PHPVBOX_VER', '5.1-0');

create 2 symlinks:

ln -s vboxweb-5.0.wsdl vboxweb-5.1.wsdl
ln -s vboxwebService-5.0.wsdl vboxwebService-5.1.wsdl
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...