Tuesday, October 2, 2012

Hosting Ubuntu Server LAMP on Windows host


Say you want to run your web site from your home. The choices you have are to get a dedicated PC (buy some old machine) or to create a virtual machine on your powerful home PC. Well, the second option is not only cheaper but also has a lot of other advantages: you can copy virtual machines and move them around like ordinary files, you can take a snapshot of your current state and come back to it later if you messed things up and you can save the dedicated PC space for something more useful like a vase.

This guide installs Ubuntu Server 12.04 on VirtualBox 4.2.

Preparation

  1. Downlaod and install VirtualBox  for your system (This guide uses VirtualBox 4.2 64Bit).
  2. Download Ubuntu Server (This guide uses Ubuntu Server 12.04 64Bit)

Creating a new Virtual Machine

  1. Open VirtualBox Manager.
  2. Click the New button to open the "Create New Virtual Machine" wizard.
  3. On the wizard's welcome page:
    Click Next.
  4. On the VM Name and OS Type page:
    Choose a name for the new virtual machine, and in the OS type and version select Linux and Ubuntu (or Ubuntu 64), and click Next.
  5. On the Memory page:
    Leave the suggested memory capacity or type a value according to your system's use, and click Next.
  6. On the Virtual Hard Disk page:
    Leave the Startup disk radio button and the Create new hard disk checkbox selected and click Next.
  7. On the Create New Virtual Disk wizard
    Select VDI and click Next.
  8. On the Virtual Disk Storage Details, select one of the options.
    A fixed size hard disk will be wholley created and allocated immediatly after the next step.
    A dynamically allocated hard disk will be created but only a fraction of it will be allocated at first. The disk grows with the virtual machine's needs and thus each write operation will be slower at first than on a fixed size hard disk, but as the time goes by, the difference will be negligable. NOTE: a dynamically allocated hard disk does NOT grow forever. It only grows up to the maximum size you give it (on the next step). 
  9. On the Virtual Disk File Location and Size page:
    Select a folder to save the image file of the virtual hard disk, and select its size (8GB is the default and is fine for our purposes). Click Next.
  10. On the Summary page:
    Click Create
After disk creation, the new virtual machine's name will be listed in the left list box of VirtualBox Manager.

Starting the new virtual machine with Ubuntu Server's image 

  1. In VirtualBox Manager, make sure the newly create virtual machine is selected and click the Settings button.
  2. On the tab view on the left, select the Storage tab.
  3. Under the Storage Tree view, on the IDE Controller line, click the left button (Add CD/DVD)
    A message box will appear asking you if you want to leave the new virtual CD/DVD disk empty or to choose an image file. Obviously we'll click the Choose Disk button and select UBuntu Server's image file we downloaded earlier.
  4. Click Next to close the Settings dialog.
  5. Click the Start button.
    The new virtual machine will show up and run the Ubuntu Server installation disk. 

Installing Ubuntu Server

Ubuntu Server's installation is quite simple, all you have to do is follow the very clear instructions.
Important: on the On the Software Selection page, check the LAMP Server

Some points:
  • On each page you can press Esc to go back to earlier pages
  • On the Partition Disk page, if you don't want to handle parititions, select the Guided -use entire disk
  • On the Install GRUB boot loader on a hard disk page, choose to install GRUB since Ubuntu will  be the only OS on this virtual machine.

Set up port forwarding

The defualt network mode on a  new virtual box is NAT (Network Addres Translation). In this mode, the host and external machines cannot "see" the guest OS and all packets sent outside from the guest OS are rewritten as if the host sent them on its behalf. So how can the host and external machines reach our web server? Port forwarding to the rescue. We'll create a rule such that each packet arriving to the host at port 80 (HTTP port) will be forwareded to the guest OS at port 80. Think of it as a tunnel going from the host's port to the guest's port. 
  1. If the virtual machine is on, close it by closing its window and select Power off the machine in the popup dialog.
  2. In VirtualBox Manager, make sure the newly create virtual machine is selected and click the Settings button.
  3. On the tab view on the left, select the Network tab.
  4. On Adapter 1 tab page, make sure Enable Network Adapter radio button is selected, and expand the Advanced area. 
  5. Click the Port Forwarding button to open the Port Forwarding Rules dialog.
  6. Click on the right + button to add a new rule and edit it to:
    Name: WebServer
    Protocol: TCP
    Host IP: leave blank
    Host Port: 80
    Guest IP: leave blank
    Guest Port: 80

Testing

  1. In the VirtualBox manager, start the machine again and wait for it to finish loading.
  2. On your host machine, open your web browser and at the address bar enter localhost.
    You should see the famous Apache's It Works! page.

Upgrading and updating Ubuntu Server

  1. On the guest Ubuntu Server, login by entering your login name and password.
  2. At the prompt write sudo apt-get update and hit Enter. Write your password if prompted and press Enter again. This will update the list of available packages.
  3. When it finishes updating, write sudo apt-get upgrade and press Enter. This will install newer versions (if there are) of the packages.

Installing Guest Additions

From VirtualBox's site
"For any serious and interactive use, the VirtualBox Guest Additions will make your life much easier by providing closer integration between host and guest and improving the interactive performance of guest systems....".
Guest additions make the guest's graphics system far faster and responsive and enables drag-and-drop and copy-paste operations as well as shared folders between host and guest.
But since Ubuntu Server does not have a windowing system, guest additions will only help for sharing folder between the host and guest machines. 
  1. On Ubuntu's prompt write sudo apt-get install gcc make to install the make and the gcc compilation utilities.
  2. On the window menu of your virtual machine, select Devices and then select Install Guest additions.
  3. On Ubuntu's prompt write sudo mkdir /media/vboxadd to create a new directory under /media on which we will mount the guest additions image.
  4. Write sudo mount /dev/sr0 /media/vboxadd. This will mount the guest additions onto /media/vboxadd directory.
  5. Write sudo /media/vboxadd/VBoxLinuxAdditions.run to start the installation process.
    You may get an error saying that installing the X Window system drivers failed, ignore it since, as said above, we don't have a windowing system on Ubuntu Server.

That's It

Ubuntu Server is now ready for your new great site.
If there are any questions or enlightments, please comment.
Have a great day.


Tuesday, June 5, 2012

Android on a Virtual Machine

The Android emulator that is shipped with the Android SDK is very slow to load and work with. 
Fortunately, there is a faster alternative: Android-x86 on a virtual machine.
This guide will show you how to install Android-x86 on VirtualBox and connect it to adb.

Downloading and installing prerequisties

  1. Download and install VirtualBox.
  2. Download android-x86-2.3-RC1-eeepc.iso from the Android x86 project site

Creating a virtual machine in VirtualBox

  1. Open VirtualBox and press the New button to open the "Create New Virtual Machine" wizard.
  2. [Welcome to the New Virtual Machine Wizard]
    Next
  3. [VS Name and OS Type]
    Name: Android
    Operating System: Linux
    Version: Other
    Next
  4. [Memory]
    Base Memory Size: 256MB
    Next
  5. [Virtual Hard Disk]
    Select Create new hard disk
    Next
  6. [Welcome to the virtual disk wizard]
    Select VDI (Virtual Disk Image)
    Next
  7. [Virtual disk storage details]
    Select Fixed Size
    Next
  8.  [Virtual disk file location and size]
    Location: select a directory for a 4GB file
    Size: 4GB
    Next
  9. [Summary] (of the "Create New Virtual Disk" wizard)
    Next
  10. [Summary] (of the "Create New Virtual Machine" wizard)
    Next
  A new virtual machine named "Android"  will show up in the virtual machines list in VirtualBox.
  

Configuring the virtual machine

  1. Select the newly created "Android" virtual machine and press the Settings button.
  2. In the Storage tab, press the Add CD/DVD Device icon, press Choose Disk, and select the android iso image file you downloaded earlier.
  3. In the Audio tab, select SoundBlaster 16 as the Audio Controller.
  4. Press the OK button to save all settings.

 Installing Android-x86 on the virtual machine

  1. Select the "Android" virtual machine and press the Start button.
    The virtual machine will start with the Android-x86 iso disk image in its virtual disk device, and a menu will show up.
  2. Select Installation - install Android-x86 to harddisk. The cfdisk utility will show up.
  3. Using the right and left arrow keys, select New and press Enter.
  4. Using the right and left arrow keys, select Primary and press Enter.
  5. Leave the size field as is and press Enter.
  6. Using the right and left arrow keys, select Bootable and press Enter.
  7. Using the right and left arrow keys, select Write and press Enter.
  8. Write yes to confirm the write operation and press Enter. The parition table will be written to disk.
  9. Using the right and left arrow keys, select Quit and press Enter. This will bring you back to the previous menu, this time a new menu item called sda1 will be added.
  10. Using the up and down arrow keys, select sda1 Linux VBOX HARDDISK and press Enter.
  11. Using the up and down arrow keys, select ext3 and press Enter.
  12.  Press Enter to confim disk formatting. Disk will be formatted.
  13. Using the right and left arrow keys, select Yes to install boot loader GRUB and press Enter
  14. If you intend to develop Android application on this virtual machine, which I'm sure you are, select Yes  using the right and left arrow keys to let /system directory be read and write, and press Enter.
    Androidx86 will be installed into the harddisk.
  15. Using the up and down arrow keys, select Create a fake SD card and press Enter.
  16. Enter 512 or other size for the fake SD card and press Enter. SD Card image will be created.
  17. Press Enter to Reboot the virtual machine. The machine will restart and the installation menu will show up again since the virtual Android-x86 disk is still attached to the VM (Virtual Machine).
  18. On the menu of VirtualBox select Devices, CD/DVD Devices, IDE Controller (IDE Primary Master), Remove disk from virtual drive. This will virtually eject the virtual Android-x86 disk from the VM.
  19. On the menu of VirtualBox select Machine, Reset and confirm the operation in the confirmation dialog that appears. The VM will be reset and than the GRUB boot loader will appear. If no action taken, Android will show up in a few seconds.
    Android-x86 on VirtualBox
  20. If the mouse pointer disappears when you move the mouse over the VM's window, than mouse integration should be disabled.
    On the menu of VirtualBox, select Machine, Disable Mouse Integration.
  21. When you click inside the VM's window, the mouse will be captured by VirtualBox to the sake of  the  VM. When you want your mouse back, click the right CTRL key.

Connecting the VM to adb

So far we have an Android VM on a virtual network, connected to the internet by NAT. VirtualBox acts as a router: it sends all requests from the VM as if they came from VirtualBox itself, listens for responses and feeds them back to the VM. The host (the PC) cannot "see" the VM since VirtualBox forwards only responses to the VM's requests into the VM. So how can adb that resides on the host, connect to the Android VM we have just created? 
Port Forwarding to the rescue. We will make a rule in VirtualBox such that every packet that is destined to port 5555 on the host, shall automatically be forwarded to port 5555 on the virtual machine. Using this rule, when we'll want to connect adb to the VM, we will actually connect to the host itself, ie 
adb connect 127.0.0.1  or adb connect localhost.
So let's get started:
  1. Close the VM's window. "Close Virtual Machine" dialog will appear. Select  Power off the machine and press Enter.
  2. On VirtualBox, select the "Android" VM and press the Settings button.
  3. Go to the Network tab, expand the Advanced options and press the Port Forrwarding button. The "Port Forwarding Rules" dialog will show up.
  4. Click the Inset new rule button. A new raw will appear in the rules list.
  5. Set Name: adb, Protocol: TCP, Host Port: 5555, Guest Port: 5555 and click the OK button.
  6. Open a terminal (on linux) or console (Windows: Start, Run, cmd) and navigate to directory platform-tools under the android-sdk root directory.
  7. Write adb connect localhost and press Enter. adb will be connected to our VM.
    You just have to select it in Eclipse or Intellij IDEA.