Skip to main content Help Control Panel

Rod Morison Software —  Technotes

Home «   Linux «   Sysadmin «  

How To Build a Vhosted Linux Development Server on Windows using VirtualBox

I've been doing a fair bit of PHP and Python on Linux recently. My favorite PHP editor, PhpEd is Windows only. However, I prefer to develop LAMP code on LAMP. Here's a solution: Linux on VirtualBox, networked to the host Windows OS.

!!! NOTE: The latest version of VirtualBox, 3.0.2 at this time, puts a few things in different places, particularly the VT-x/AMD-V and Nested Paging settings. Check the latest VirtualBox docs for more info


-  Get and install VirtualBox for your system.
-  Get the install iso for your preferred Linux distro, in my case Ubuntu
-  Create a new virtual machine in VirtualBox, e.g., by clicking "New" and using the Virtual Machine Wizard

NewVirtualMachine.png
-  Navigating the wizard is straightforward. Name your machine, set the OS type, etc. I configure for 768MB RAM and create a new 30 GB hard drive. If your DB or file storage needs for dev are large, up the disk as needed. The RAM is plenty for most dev LAMP servers unless you run big backend progs.
-  Once you've created the machine and disk, before booting the VM, go to the Settings dialog, General, Advance tab and turn on Nested Paging. VirtualBox will only use it if available, but if it is available, it greatly streamlines the memory interface between guest and host.

NestedPaging.png
-  Now attach the boot/install iso you downloaded for your linux distro. Use the VirtualBox Media Manager to attach the iso, then mount it on your VM.

AttachIso.png
-  Next, go to the Network entry in Settings and enable Adapter 2, set Attached to: to Host-only Adapter and Name to VirtualBox Host-Only Ethernet Adapter. This adapter will be for Apache name and IP virtual hosts exposed on the host OS.

NetworkAdapter.png
-  Finally (as far as the VM setup goes), setup a share to a folder on Windows. This will map to /var/www in the linux guest and be used there by Apache. Personally, I map a vhosts folder on windows to /var/www/vhosts on the Linux side, then put my domains under vhosts, i.e., /var/www/vhosts/mydevsite.com. (Looking ahead, you'll edit the files on the Windows side, but they will be served on the Linux side.)

SharedFolders.png
-  Start the VM and install your Linux OS (not shown). I recommend updating your Linux with your distro's package manager. With Ubuntu Desktop, that's pretty trivial once you're installed and booted (it may pop up an update request before you even make it to the Update Manager menu):

UbuntuUpdateMenu.png
-  You'll probably want the VBoxAdditions installed in your Linux guest. VBoxAdditions allows cut/paste between host/guest, seemless mouse operation, etc. First, be sure to eject any mounted CD or ISO in the guest OS. From the VirtualBox "Devices" menu outside the guest console select "Install Guest Additions...". That should mount a VBoxAdditions CD. Run sudo /cdrom/VBoxLinuxAdditions-x86.run from a shell in the guest. (If you installed a 64bit AMD Linux, look for VBoxLinuxAdditions-amd64.run.) This script will require a reboot and may need to be re-run after certain kernel updates.

InstallVboxAdditions.png
-  Finally, time to install the LAMP goods. From a shell in the Linux guest (again, for Ubuntu packages):
sudo apt-get install apache2 mysql-server php5 php5-cli
sudo apt-get install php5-mysql libapache2-mod-php5

-  Time to setup that vhosts mount point on the guest system.
sudo mkdir /var/www/vhosts
sudo chmod 777 /var/www/vhosts
sudo vi /etc/fstab
and add the line vhosts /var/www/vhosts vboxsf iocharset=utf8,umask=000,uid=33 0 0
sudo mount vhosts
df -h /var/www/vhosts
and you should see the disk info for you host system disk, now mounted under Linux
Filesystem            Size  Used Avail Use% Mounted on
vhosts 269G 174G 96G 65% /var/www/vhosts

-  Now, finish up the host-guest networking. From the VirtualBox app on the host, select Preferences... in the File menu.

VirtualBoxPreferencesMenu.png Choose the Network settings. You should have a "VirtualBox Host-Only Ethernet Adapter network" already listed. (This network was used for your Adapter 2 setup above.) Select the little screwdriver icon and edit the details of that Host-only network. For mine, I turn off the builtin DHCP server, and set the network address to a range that doesn't conflict with anything on my other private networks:

HostOnlyNetworkConfig.png
-  I'm going to setup 3 guest IPs on the Host-only because that's what I need for the current project. I'm going to use Apache IP Virtual Hosts, which are required for https vhosting. If you have only 1 site, or all your sites are http and can use the Name Virtual Host directive, you only need one.

!!! I tried Gnutls for SSL Name Vhosting...I'll spare the details, but give it a pass.

Back on the guest, append the following to /etc/network/interfaces, e.g., sudo vi /etc/network/interfaces
auto eth1:0
iface eth1:0 inet static
name Ethernet alias LAN card
address 192.168.56.11
netmask 255.255.255.0
broadcast 192.168.56.255
network 192.168.56.0

auto eth1:1
iface eth1:1 inet static
name Ethernet alias LAN card
address 192.168.56.12
netmask 255.255.255.0
broadcast 192.168.56.255
network 192.168.56.0

auto eth1:2
iface eth1:2 inet static
name Ethernet alias LAN card
address 192.168.56.13
netmask 255.255.255.0
broadcast 192.168.56.255
network 192.168.56.0
and then
sudo update-rc.d -f networking remove
sudo update-rc.d networking defaults
sudo /etc/init.d/networking restart
ifconfig
and ifconfig should give you something like
eth0      Link encap:Ethernet  HWaddr 08:00:27:41:86:02  
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe41:8602/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:30837 errors:0 dropped:0 overruns:0 frame:0
TX packets:15654 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:46594350 (46.5 MB) TX bytes:853804 (853.8 KB)
Interrupt:11 Base address:0xc020

eth1 Link encap:Ethernet HWaddr 08:00:27:ff:f6:55
inet6 addr: fe80::a00:27ff:feff:f655/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:298 errors:0 dropped:0 overruns:0 frame:0
TX packets:33 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:48980 (48.9 KB) TX bytes:6287 (6.2 KB)
Interrupt:10 Base address:0xc240

eth1:0 Link encap:Ethernet HWaddr 08:00:27:ff:f6:55
inet addr:192.168.56.11 Bcast:192.168.56.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:10 Base address:0xc240

eth1:1 Link encap:Ethernet HWaddr 08:00:27:ff:f6:55
inet addr:192.168.56.12 Bcast:192.168.56.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:10 Base address:0xc240

eth1:2 Link encap:Ethernet HWaddr 08:00:27:ff:f6:55
inet addr:192.168.56.13 Bcast:192.168.56.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:10 Base address:0xc240

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:40 errors:0 dropped:0 overruns:0 frame:0
TX packets:40 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3290 (3.2 KB) TX bytes:3290 (3.2 KB)

-  On Windows (use notepad) add to \Windows\System32\drivers\etc\hosts:
192.168.56.10   myhost
192.168.56.11 dev.mysite1.com
192.168.56.12 dev.mysite2.com
192.168.56.13 dev.mysite3.com

-  On the guest add to /etc/hosts
192.168.56.10   myhost
192.168.56.11 dev.mysite1.com
192.168.56.12 dev.mysite2.com
192.168.56.13 dev.mysite3.com

-  Check your work with a ping on the host. Open a command window and ping dev.mysite1.com

GuestIpPing.png
-  Ok, time to setup an Apache IP Virtual Host. I'll just script out a vanilla sample, your setup will certainly vary. On the Linux guest system:
sudo a2enmod ssl
sudo mkdir -p /var/www/vhosts/mysite1.com/httpdocs
sudo mkdir -p /var/www/vhosts/mysite1.com/log/
sudo vi /etc/apache2/sites-available/dev.mysite1.com
and add the following vhost setup on one of the IPs you created above.

The certs are Ubuntu standard issue. If you're on a different distro...

<VirtualHost 192.168.56.11:80>
ServerName dev.mysite1.com
ServerAdmin "me@mysite1.com"
DocumentRoot /var/www/vhosts/mysite1.com/httpdocs
CustomLog /var/www/vhosts/mysite1.com/log/access_log combined
ErrorLog /var/www/vhosts/mysite1.com/log/error_log

<IfModule mod_ssl.c>
SSLEngine off
</IfModule>

<Directory /var/www/vhosts/mysite1.com/httpdocs>
<IfModule mod_php5.c>
php_admin_flag engine on
php_admin_flag safe_mode off
</IfModule>
Options +Includes +ExecCGI
</Directory>

</VirtualHost>


<VirtualHost 192.168.56.11:443>
ServerName dev.mysite1.com:443
ServerAdmin "me@mysite1.com"
DocumentRoot /var/www/vhosts/mysite1.com/httpdocs
CustomLog /var/www/vhosts/mysite1.com/log/ssl_access_log combined
ErrorLog /var/www/vhosts/mysite1.com/log/ssl_error_log

SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

<Directory /var/www/vhosts/mysite1.com/httpdocs>
<IfModule mod_php5.c>
php_admin_flag engine on
php_admin_flag safe_mode off
</IfModule>
Options +Includes +ExecCGI
</Directory>

</VirtualHost>

-  Complete the Apache config with
sudo a2ensite dev.mysite1.com
sudo /etc/init.d/apache2 restart
[/snippet[
and a nice bit of housekeeping <code>sudo vi apache2.conf</code>, find and after the ServerRoot directive add
[snippet]
ServerName "MySite"
or whatever you'd like to call it.
-  Now, check that Apache is good with a testfile on the guest

echo >/var/www/vhosts/mysite1.com/httpdocs/phpinfo.php '<?php
    phpinfo
();
?>'
and from the host browse to http://dev.mysite1.com/phpinfo.php and https://dev.mysite1.com/phpinfo.php. You should see

PhpInfo.png
-  That's it for this walkthrough. No doubt your config will vary, but this should give you the bones if you want to try this approach. Here are a couple next steps.

Comments

Derekdon on Nov. 17 2009
Great tut so far, but I keep getting an error when I try to restart the network.
  • if-up.d/mountntfs[eth0]: waiting for interface eth1:0 before doing NFS mounts
  • if-up.d/mountntfs[eth0]: waiting for interface eth1:1 before doing NFS mounts
  • if-up.d/mountntfs[eth0]: waiting for interface eth1:2 before doing NFS mounts
SIOCSIFFLAGS: Cannot assign requested address SIOCSIFFLAGS: Cannot assign requested address Failed to bring up eth1:0 SIOCSIFFLAGS: Cannot assign requested address SIOCSIFFLAGS: Cannot assign requested address Failed to bring up eth1:1 SIOCSIFFLAGS: Cannot assign requested address SIOCSIFFLAGS: Cannot assign requested address Failed to bring up eth1:2

I'm using VB 3.0.10 with a Ubuntu 8.10 64bit guest OS, running on a WinXP x64 host machine.

Any idea what might be wrong?

Also I followed this setup tut first - http://www.sitepoint.com/blogs/2009/10/27/build-your-own-dev-server-with-virtualbox/

Thanks,

Derek.
Derekdon on Nov. 17 2009
Also I meant to mention that when I follow this error with a

sudo ifconfig

command it doesn't show the updates to the network, just the defaults.
Derekdon on Nov. 17 2009
Sorry, forgot to also say that my wireless config tool states my ip address etc on the host machine is as follows:

IP Address: 192.168.1.XX (XX think this changes at times) Subnet Mask: 255.255.255.0 Gateway: 192.168.1.254

Just in case it helps.

D.
Buy r4 dsi on Jan. 20
Ok I got as far as the bridged adaptor, then went to modify the iptables files, but I can't find the sysconfig folder. I'm running Ubuntu 8.10, which appears to be different to Fedora. I've been using Windows for the last 15 years or so and have only started with Ubuntu in the last couple of months, so its all new to me.
Buy r4 dsi on Jan. 20
Ok I got as far as the bridged adaptor, then went to modify the iptables files, but I can't find the sysconfig folder. I'm running Ubuntu 8.10, which appears to be different to Fedora. I've been using Windows for the last 15 years or so and have only started with Ubuntu in the last couple of months, so its all new to me.

buy r4 dsi

Stay tuned
recent comments
podcasted files
AddThis.com