Ubuntu server as a Windows service
To host the production environment of a Rails application I had recently developed, I was offered a standalone server, running Windows 7 Professional, on a static IP address. Rails + Windows is an unholy union, and to isolate as well the Windows machine from compromises on the web application, I decided to run Ubuntu Server on a VirtualBox instance. The final purpose was to run Ubuntu without any GUI, reachable exclusively via SSH, and handled as a Windows service; in this way, the server would be automatically started or shutdown with the Windows machine.
And this is the tale on how I accomplished it. Obviously this story changes with the time, eventually becoming obsolete. It relates to the end of 2011, when the most recent Ubuntu Server distribution is 11.10, and the most updated VirtualBox release is 4.1.x. Furthermore, the host is running Windows 7 Professional (64 bits), using a i5 2500K CPU and 8 gigabytes of memory.
- Download and install virtualbox
- Download the Ubuntu server distribution. To speed up the download, the best is to download the alternate torrent distribution (currently: ubuntu-11.10-server-amd64.iso.torrent), ensuring that the md5 hash matches.
- Start virtualbox, and create a virtual machine (I named mine Wimel) with type Ubuntu (64 bits), using 1.5 Gigas and a dynamically allocated VDI hard disk of 50 Gigas; these are arbitrary sizes, the memory depends obviously on the final usage requirements, and 1 Gigabyte should be enough for most purposes; likewise, 50 Gigas is a lot of space. Enough to say that once the server and all my required applications were installed, only 2 gigabytes were used. But being dynamically allocated, it means that there is space (a lot!) to grow, without immediately using it.
- After creation, go to the instance configuration in virtualbox. On storage, provide the path to the downloaded Ubuntu iso, and, on system, allocate 2 processors. Once again, this allocation reflects my own needs, where I gave as much importance to the linux machine as to the Windows host.
- Start now the virtual machine. Select language and keyboard, provide a hostname and use the entire disk (no LVM). For a Rails server, I only installed the OpenSSH server (LAMP, specially, not required). Afterwards, install the GRUB loader to the master boot record, and complete the installation.
- After the required reboot, perform the usual maintenance on a Debian
distribution:
sudo apt-get update sudo apt-get upgrade
- The objective is to have this machine reachable from the Internet.
How this is accomplished depends obviously on your network configuration;
my situation favored allocating an static IP address, and this
link
provides the complete solution:
sudo vi /etc/network/interfaces
And replace in this fileauto eth0 iface eth0 inet dhcp
withiface eth0 inet static address 192.168.0.105 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1
(Please change the address/network as required for your specific needs). To update now the DNS host:sudo vi /etc/resolv.conf
which should contain one single line: your own DNS server:nameserver 192.168.0.1
Finally, shutdown the machinesudo shutdown -h now
- This is still not the complete network solution; it is needed to go now to the virtualbox configuration of this machine and select Bridge adapter instead of the default NAT. After this, restart once again the virtual machine.
- The Ubuntu virtual machine is now reachable via SSH. Obviously, it is needed some network address translation magic to access this machine from the internet. In special, with the static IP address (192.168.0.105 in this case) it is needed to setup NAPT (Network Address and Port Translation) to access the port 22 on 192.168.0.105 via an external port (like 5022). This depend on your router; I have an old Alcatel router that allows this setup, while a much newer router, a Netgear WNDR3700 does not fully allow it (I can perform the NAPT translation only from the same or lower port, not from a higher port).
- Once this process is completed, the machine can be accessed using
SSH, using:
ssh -p port user@host
where port is the NATP port and host is the host allocated to the router. Likewise, it is possible to access it using SFTP with:sftp -oPort=port user@host
- Now it comes the second part of the whole setup: the virtual machine
has to be setup to start automatically on windows startup.
Shutdown first the virtual machine, and get ready to install the
great VBoxVmService,
currently on version 3.0. Download it and install it on
c:\vms
Then, follow the instructions onc:\vms\doc\howto.txt
which basically, implies a configuration filec:\vms\vboxvmservice.ini
looking like:[Settings] ServiceName=VBoxVmService RunAsUser=.\xxUSERxx UserPassword=xxPASSWORDxx VBOX_USER_HOME=C:\Users\xxUSERxx\.VirtualBox RunWebService=no PauseShutdown=5000 [Vm0] VmName=Wimel ShutdownMethod=savestate AutoStart=yes
This setups one single virtual machine called Wimel, but multiple machines could be setup automatically. Note that the windows user and password must be hardcoded in the file, so pay attention to the access rights. - Once the configuration file is on place, install the service with:
VmServiceControl.exe -i
Then, reboot the system - It should work perfectly.... although it didn't work for me none of
the times I tried it. I had to do, in addition:
- Open Panel Control / System / Administrative tools / Services
- Locate vboxvmservice. Open it, and then:
- In init session, enter again the user / password
- Set it up to start as automatic (no delayed).
And that's the lengthy process to have it working. And having it working great.