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.


No comments:

Post a Comment