Installing Ubuntu In A VirtualBox Session For Web Development

Overview

Often when I teach a web development class I get a number of questions related to what is a good way to install software suitable for web development on one's computer. Of course, officially, this is all provided on the school's computers, computer science students (i) want to learn how to set up such on their own machines and (ii) want to be able to do web development even when they are not connected to the Internet. Although outside the official support of any web course(s) that I may be teaching, the purpose of this article is to outline how one would set out to do this using Ubuntu and VirtualBox.

Disclaimer: While I have made the effort to provide accurate information, it is entirely possible there are errors, etc. Do read the documentation for everything mentioned herein and your use of such is completely at your own risk.

Step I: Acquire VirtualBox & Ubuntu

Download and Install VirtualBox

This is the easiest step! Simply go to http://www.virtualbox.org and download the latest VirtualBox program and install it. If VirtualBox will be run on a 64-bit computer, be sure to download the 64-bit version.

VirtualBox is a software program that is capable of running an entire PC inside a virtual machine. This means other operating systems can be installed inside of it, booted, and then run –all inside a window on your computer. Nicely this allows one to have and use other operating systems on your computer without actually having to resize, create, and format partitions, set up your computer for dual booting, etc. This also allows you to use the operating system you use the most at the same time as other operatings systems (one per virtual machine that you run) at the same time –which is otherwise impossible using only a single computer.

Download Ubuntu

Before one can do anything with VirtualBox, one needs installation disc(s) for an operating system so that it can be installed. For this article, download the Ubuntu CD-ROM ISO image from http://www.ubuntu.com. Again, if this will be run on a 64-bit computer, be sure to download the 64-bit "amd64" version of Ubuntu. The "amd64" version will work with Intel or AMD64 chipsets.

Now that VirtualBox has been installed and the Ubuntu installation CD-ROM ISO file, the next step is to create a virtual machine session in VirtualBox and install Ubuntu!

Step II: Establish The Virtual Machine

Create The Virtual Machine

Unlike a real computer, with VirtualBox one can configure the which hardware resources as well as the amount of RAM, hard disk space, etc. that the virtual machine will be allowed to have. To use any software program that let's you run virtual machines, it is highly recommended that you have extra of all of the resources required to run the operating system and the software in it. Currently, to boot your computer and to run software you computer needs to have a certain amount of RAM available and a certain amount of hard disk space available + free RAM and free hard disk space to do things. When running a virtual machine, the same is true –but one will also need an additional amount of RAM and hard disk space that the virtual machine needs as well.

Important: If you don't have enough RAM or disk space to do this step, then you will have to acquire it in order to proceed further.

The amount hard disk space available to your virtual machine is easy to set at the beginning and harder to increase later. Since VirtualBox has a "grow" option for hard disk space that increases in size as more space is used, one is best to create an appropriately large hard drive. As for RAM, this can be easily changed at a later point in time.

To create the virtual machine in VirtualBox, follow these steps:

  1. Run VirtualBox.
  2. Click the New button to create a virtual machine.
  3. A Create New Virtual Machine dialog will appear.
  4. Click the Next > button.
  5. Type in a Name for the virtual machine (e.g., Ubuntu).
  6. Select the Operating System (OS) Type for the machine (e.g., Operating System: Linux, Version: Ubuntu or Ubuntu 64-bit).
  7. Click the Next > button.
  8. Select the Base Memory Size. If unsure, go with the recommended amount (e.g., 384 MiB).
  9. Click the Next > button.
  10. Ensure that the Boot Hard Disk checkbox is selected.
  11. Select the Create new hard disk radio button.
  12. Click the Next > button.
  13. A Create New Virtual Disk dialog window will appear. Click the Next > button.
  14. Under Storage Type select the Dynamically expanding storage radio button.
  15. Click the Next > button.
  16. Choose a Location to store the virtual hard disk image if the default is not liked.
  17. Choose a Size for your the disk (e.g., 8 GiB). Remember: this is not easily changed later!
  18. Click the Next > button.
  19. Click the Finish button to close the Create New Virtual Disk dialog.
  20. Click the Finish button to close the Create New Virtual Machine dialog.
  21. Ensure that the newly created virtual machine is selected.
  22. Click the Settings button in the tool bar.
  23. Click the Storage item in the left-hand side listbox.
  24. Click the Empty CD-ROM disc under IDE Controller.
  25. Click the CD-ROM drop-down button and select Choose a virtual CD/DVD disk file….
  26. Find and select the ISO file that was downloaded for Ubuntu.
  27. Click OK in the Settings dialog window.
  28. Click Start button in the toolbar to start the virtual machine to start installing Ubuntu!

Step III: Install Ubuntu

Installing Ubuntu

The last step in the previous section started the virtual machine. Just like a real computer would, you can see it (quickly!) count up the memory, look for the disk drives present, and boot off the Ubuntu installation CD-ROM. There are two modes to run Ubuntu off the CD-ROM: (i) as a LiveCD or to (ii) install. You can install Ubuntu with either option, but, the LiveCD option let's you run try Ubuntu without installing anything on your computer. There isn't a need to select the LiveCD option when using a virtual machine –except if you need to "rescue" an unbootable computer, so choose just to install Ubuntu.

Do the following steps when the installation screen appears:

  1. Click on English and choose Install Ubuntu.
  2. On the Time page choose the appropriate region (e.g., Canada) and an appropriate timezone city/locale (e.g., Toronto).
  3. Click the Forward button.
  4. On the keyboard page, choose the appropriate keyboard layout (e.g., USA keyboard layout).
  5. Click the Forward button.
  6. On the disk partitioning screen, select Use the entire disk.
  7. Click the Forward button.
  8. Enter in your full name, a login, a password, and a machine name.
  9. Click the Forward button.
  10. Click Install.
  11. Wait for all of the files to be downloaded and copied.
  12. Click Restart Now.
  13. Since your CD-ROM isn't real, removing it is a little different than what you may be used to! When prompted to remove the CD-ROM do the following:
    1. From the VirtualBox machine window's pull down menu, select the Devices menu.
    2. Select the CD/DVD Devices menu item.
    3. Uncheck the CD-ROM ISO file. This will "eject" the installation CD-ROM.
    4. Press enter inside the virtual machine window to reboot.
  14. After rebooting, login using your login and password.
  15. If prompted, install any updates.

Step IV: Post-OS-Install Software Configuration

Software Installation

With Ubuntu installed, all that remains is to install Apache web server, PHP, PostgreSQL, and a set of handy utilities! Here are the steps to do this:

  1. In Ubuntu, open a Terminal window via Applications | Accessories | Terminal.
  2. Run:
    sudo apt-get install aptitude
  3. Run:
    sudo aptitude install openssh-server
  4. Run:
    sudo aptitude install apache2 apache2-doc php5 \
    php5-mcrypt php5-ps php5-timezonedb php5-xmlrpc php5-xsl \
    php5-cli php5-curl php5-dev php5-gd php5-imagick php5-mysql php5-pgsql \
    php5-tidy php5-xmlrpc php5-xsl php5-xdebug php5-uuid php5-gmp php5-recode \
    sablotron libapache2-modxslt
  5. To install the PostgreSQL database run:
    sudo aptitude install postgresql postgresql-client postgresql-doc php5-pgsql
  6. To install the MySQL database run:
    sudo aptitude install mysql-client mysql-server php5-mysql
  7. Run:
    sudo aptitude install tinyca
  8. Run:
    sudo aptitude install vim ctags vim-doc vim-scripts
  9. Run:
    sudo aptitude install xsel
  10. Run:
    sudo aptitude install gedit-plugins
  11. Run:
    sudo aptitude install gimp
  12. Run:
    sudo aptitude install dwww w3-recs doxygen doxygen-doc
  13. Run:
    sudo aptitude install w3c-dtd-xhtml w3c-markup-validator
  14. Run:
    sudo aptitude install csstidy xmlstarlet
  15. Run:
    sudo aptitude install mutt postfix qpopper
    1. Choose Local only as the delivery option.
    2. Type in the name exactly the same as what was given the system during the install.
    3. Run:
      sudo nano -w /etc/postfix/main.cf
    4. Edit the line starting with mydestination= to read:
      mydestination = 60-334, localhost.localdomain, localhost, local.dev, www.local.dev
    5. Run:
      sudo /etc/init.d/postfix reload
    6. Run:
      sudo nano -w /etc/aliases
    7. Ensure that the aliases file has this content (with YOUR_LOGIN_NAME replaced with your login):
      root: YOUR_LOGIN_NAME
      postmaster: YOUR_LOGIN_NAME
      webmaster: YOUR_LOGIN_NAME
      webadmin: YOUR_LOGIN_NAME
  16. Before the mail program can be configured to deliver (localhost) mail, an email needs to be sent as follows:
    1. Run:
      mutt
    2. Answer No to the Create mail folder in home folder question.
    3. Hit m to generate an email to send.
    4. Enter your login name in the To: line.
    5. Enter Test in the Subject: line.
    6. Enter This is a test. in the body.
    7. Hit Ctrl-X and save the message (using the given file name).
    8. Hit y to send the message.
    9. Hit q to quit.
  17. With an email waiting, configure the Evolution mail client as follows:
    1. Open Applications | Office | Evolution Mail and Calendar.
    2. Since this is the first time you are opening Evolution, a dialog window will appear to allow you to configure its settings. Click Forward.
    3. Click Forward (if there is a backup screen).
    4. Enter your Full Name, your email address within the virtual machine (i.e., YOUR_LOGIN_NAME@localhost), and make it the default account.
    5. Click Forward.
    6. Set the Server Type to Local delivery and the path to /var/mail/spool/YOUR_LOGIN_NAME.
    7. Click Forward.
    8. Check messages every minute.
    9. Click Forward.
    10. Set the Outgoing Mail Server Type to SMTP and the Server Name to localhost.
    11. Click Forward.
    12. Click Forward.
    13. Click Apply.
  18. Right-click Applications | Office | Evolution Mail and Calendar and choose Add to Panel. This will add the Evolution icon to the panel at the top of the screen.
  19. Run Firefox:
    1. Create a bookmark for dwww:
      http://localhost/dwww/
    2. Create a bookmark for the w3-recs package's directory (via dwww):
      http://localhost/cgi-bin/dwww/usr/share/doc/w3-recs/html/index.html
    3. Create a bookmark for dwww's Help | Standards menu (i.e., for w3-recs package files):
      http://localhost/dwww/menu/shelp_standards.html
    4. Create a bookmark for the W3C Markup Validator:
      http://localhost/w3c-markup-validator
    5. Install these Add-Ons: Firebug, FirePHP, Firefinder for Firebug, FireQuery, FireXPath, Inline Code FInder for Firebug, and Web Developer.
  20. Configure the Apache web server for virtual hosting:
    1. Ensure that /etc/hosts has in it (e.g., sudo nano -w /etc/hosts) while noting that any 127/8 address will work:
      127.0.1.2 www.local.dev www
    2. Run:
      sudo nano -w /etc/apache2/sites-available/local.dev

      and add:

      <VirtualHost 127.0.1.2:80>
        ServerName www.local.dev
        ServerAlias local.dev
        ServerAdmin webmaster@localhost

        DocumentRoot /home/YOUR_LOGIN_NAME/web/public
        <Directory /home/YOUR_LOGIN_NAME/web/public>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride None
          Order allow,deny
          Allow from all
        </Directory>

        LogLevel warn
        ErrorLog /home/YOUR_LOGIN_NAME/web/logs/error.log
        CustomLog /home/YOUR_LOGIN_NAME/web/logs/access.log combined
        ServerSignature Off

        <IfModule mod_php5.c>
          php_flag magic_quotes_gpc Off
          php_flag magic_quotes_runtime Off
          php_flag file_uploads On
          php_flag short_open_tag On
          php_flag session.auto_start Off
          php_flag session.bug_compat_warn Off

          php_value upload_max_filesize 16M
          php_value post_max_size 16M

          php_value error_log /home/YOUR_LOGIN_NAME/web/logs/php_errors.log
          php_flag display_errors Off
          php_flag display_startup_errors Off
        </IfModule>

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.xml index.html index.htm
        </IfModule>
      </VirtualHost>
  21. Run:
    sudo nano -w /etc/apache2/ports.conf

    to have the following line in it (which must match the IP in the /etc/hosts file):

    NameVirtualHost 127.0.1.2:80
  22. Run:
    mkdir -p /home/YOUR_LOGIN_NAME/web/{public,log}
  23. Run:
    sudo chown -R www-data:www-data /home/YOUR_LOGIN_NAME/web/log
  24. Run:
    sudo a2ensite local.dev
  25. Run:
    sudo /etc/init.d/apache2 force-reload

Step V: Accelerate VirtualBox (Optional)

Installing VirtualBox Additions

VirtualBox provides a number of device drivers that provide features (e.g., Shared Folders) and accelerate the speed of the virtual machine. (The instructions below applied to VirtualBox version 3.x. I've not checked that they still apply to version 4.x.) To install these VirtualBox Additions do the following:

  1. Ensure you are logged into the virtual machine.
  2. Choose from the VirtualBox Session window Devices | Install Guest Additions….
  3. In the Ubuntu window, click on the Places menu.
  4. Open the CD-ROM menu item.
  5. After the CD-ROM drive appears, open Applications | Accessories | Terminal.
  6. Run:
    cd /media
  7. Run:
    ls
  8. Note the CD-ROM name, e.g., VBOXADDITIONS_3.1.8_61349.
  9. Run:
    cd VBOX*
  10. Run (if a 32-bit install, otherwise change the name appropriately):
    Run: sudo bash ./VBoxLinuxAdditions-x86.run
  11. Wait for the drivers to build.
  12. Run:
    exit
  13. In the CD-ROM window, click on the Eject icon for the CD-ROM. You may have to make the Window larger to see it.
  14. In the VirtualBox window menu, click on Devices | CD / DVD Devices… and uncheck VBoxGuestAdditions.iso.
  15. If you get slow screen updates, then turn off 3D in Ubuntu by:
    1. Opening System | Preferences | Appearance.
    2. Click on the Visual Effects tab.
    3. Choose None.
    4. Click Close.
  16. If VirtualBox is upgraded, remember to redo the above to upgrade your Guest Additions!

Step VI: Enable Shared Folders And Bridged Networking

Web development tasks can be simplified considerably by enabling the Bridged Networking feature and using VirtualBox' Shared Folders. Both of these are found in the Settings window.

The bridged networking feature allows the host OS to talk directly to the guest OS (i.e.. Ubuntu). After the the network setting is changed from NAT to Bridged and the computer restarted, the IP address of Ubuntu can be found by running:

sudo /sbin/ifconfig

and then using that to connect to your virtual machine from the host OS.

The Shared Folders feature makes it very easy to move files to/from the guest and host OSes. To configure Shared Folders do the following:

  1. Before booting Ubuntu, edit the Shared Folders settings and add the desired folder(s). Be sure to write down each shared folder's name as that will be needed below.
  2. One can mount the shared folder by running in a Terminal window:
    sudo mount -t vboxsf SHARED_FOLDER_NAME A_MOUNT_DIRECTORY
  3. Know that to mount a device in Linux the A_MOUNT_DIRECTORY must previously exist as a directory:
    sudo mkdir /media/windows
  4. For example, if the A_MOUNT_DIRECTORY was /media/windows and the share SHARED_FOLDER_NAME was myspace, you could mount it using:
    sudo mount -t vboxsf -o rw,uid=1000 myspace /media/windows
  5. You could also have it permanently mount the share every time you boot by adding something like this to /etc/fstab (where uid=1000 is only true if you are the only in your virtual machine):
    public_html /home/YOUR_LOGIN_NAME/web/public vboxsf rw,auto,uid=1000 0 0

Step VII: Read Documentation

There are a lot of commands and details above. Be sure to read the installed online documentation accessing via dwww right on the virtual machine. Additionally, seek out the documentation on the Internet for the various programs. Kindly do not send me, "How do I do this [but it is already on the page]? Can you do this for me [because I don't want to invest any time towards such]?" emails especially if you did not read all relevant documentation for the items of concern and give serious efforts towards resolving the issues you are trying to resolve. (Sadly people do send emails like that and they are very inappropriate.)

If you are new at and are learning how to do all of this, truly the best way is to struggle through it on your own especially armed with the level of detail I have provided on this page. If you do, you will learn a lot and you will remember it.