Make Developer VM ready for Webapplications

We got an Ubuntu virtual machine named Devel2011 set up and running on VirtualBox. Next thing we need is apache, php and mysql to make it ready for running a web application.
Btw, VBoxManage showvminfo Devel2011 shows you the guest VM configuration.

1. Add VBoxGuestAdditions.iso as virtual CD/DVD to VM. On Mac OS X hosts, the ISO image file is located in the VirtualBox application bundle. To locate the the file, start the Finder, right click on the VirtualBox icon and select Show Package Contents. Manual says we need it, so we do.

2. We want the code not on vm but on the host. That’s easy, first create the shared folder on the host, then just go to VM settings in VirtualBox app and connect to that shared folder on the host.
Check the box for automount, then the shared folder on Mac is mounted as /media/sf_. The VM user needs to belong to group vboxfs to have access to that folder, so add users to that group.

3. You can create a symbolic link to a folder /htdocs sudo ln -s /media/sf_Sites/ /htdocs and access all shared data under /htdocs.

4. We need to install webserver, php and mysql on the VM:

apache webserver sudo apt-get install apache2
php5 module for apache sudo apt-get install php5 libapache2-mod-php5
php command line interface sudo apt-get install php5-cli
mysql server sudo apt-get install mysql-server

Now we have a VM with PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch, Apache/2.2.17 (Ubuntu) and MySQL 5.1.54-1ubuntu4.
Calling http://localhost in Firefox in VM say ‘It works’ ;)

5. We customize our VM by adding some vhosts. Add new custom files to /etc/apache2/sites-available, e.g. mydev and myshop. You can make a copy from default file and change to your preferences.

Change to directory /etc/apache2/sites-enabled and enables the customized sites by linking them into sites-available:
sudo ln -s ../sites-available/mydev 001-mydev
sudo ln -s ../sites-available/myshop 002-myshop
Delete 000-default if you don’t need it. Restart apache with sudo /etc/init.d/apache2 restart.

6. User www-data is running apache and that guy has initially no rights to access /htdocs/mydev/index.php e.g. as only member of group vboxfs are allowed access.
sudo usermod -a -G vboxsf www-data
Restart apache, bingo, now we can access the files in htdocs.

7. We need to tell apache how to to handle *.php files. Add the following entries to apache2.conf.

#Tell apache that all files with suffux ‘php’ are to be processed as php file.
AddType application/x-httpd-php .php

#The next line is for using php inside html files and also files with alternate suffix htm.
AddType application/x-httpd-php .php .html .htm

8. Almost finished, we want to access vm from host webbrowser. So add port forwarding for host port 8080 to port 80 of the guest VM.
VBoxManage modifyvm “Devel_2011″ –natpf1 “guestssh,tcp,,8080,,80″
We can access the web application running on the guest VM with http://localhost:8080.
If we have more than one vhost on VM running on port 80 we always access the first with this rule.

To access other vhosts, let them run on different ports on the guest VM and add additional port forwarding rules on the host.

This entry was posted in howto, virtual machine and tagged , , . Bookmark the permalink.

Leave a Reply