LAMP Ubuntu Server On VirtualBox

Posted On:
Filed Under:
LAMP on Ubuntu

This tutorial is to help you set up a LAMP (Linux Apache MySQL PHP) Ubuntu virtual machine server running on VirtualBox. VirtualBox is free for you to use, and supports Linux guest additions whereas the new version of Microsoft's Virtual PC for Windows 7 does not. Guest additions allow for better integration of desktop resolutions and mouse control. Folders can be shared between the host and guest machine allowing you to simply save the file you are working on and refresh your browser, there's no need to upload via FTP/SFTP to test your changes. Other advantages include upgrading or testing software like Drupal or Wordpress; you can test new modules or customisations before adding them to your live website.

This tutorial is based on setting up the virtual machine with your host as Windows 7, this will most likely work the same on other versions of Windows, or even other operating systems but some parts may differ slightly.

Setting up VirtualBox & Ubuntu

First of all you need to download VirtualBox. For this tutorial we will want the VirtualBox 3.2.6 for Windows hosts.

Then you will want to download the Desktop Edition of Ubuntu. For this tutorial the 32bit version is fine.

Now it's best to use the included 'Virtual Media Manager' for mounting image files like the Ubuntu setup iso. This can be accessed from the File menu in VirtualBox. You could always burn the image onto a CD or DVD and run the install that way if you prefer, it's entirely up to you.

Install VirtualBox

Install is pretty straightforward, once installed you need to click 'new' to create a virtual machine. The virtual machine wizard should load. Click 'next' then enter a name for your virtual machine, I called mine 'vmTemplate' as this will be the base install for all future virtual machines I want to create. Operating System is 'Linux' and Version is 'Ubuntu'. Click 'next', then leave the base memory as 512mb, that should be plenty. Next you want to 'create a new hard disk', this will be space allocated for the virtual machine, it will not affect your Windows installation. Then another wizard will be displayed for the hard drive, leave it set as 'Dynamically expanding storage', choose a filename and where to store the hard drive, 8GB should be fine. Finish both wizards to complete setting up your virtual machine.

To be able to share your internet connection and connect with your guest machine you will want to modify your network settings for your virtual machine. First of all you need to go into your Windows 7 Control Panel, select the Network and Sharing Center and then in the top left menu should be a link to 'Change Adapter Settings'. There you will see your Windows 7 Local Area Connection and also a VirtualBox Host-Only Network connection, using your mouse select both icons, right-click and select 'Bridge Connections'. Now return to VirtualBox, select your newly created virtual machine and click 'settings'. Under Network and the Adapter 1 tab change the 'Attached to' setting to 'Bridged Adapter' and then in the 'Name' dropdown select your bridged network connection, click OK.

Starting Your Virtual Machine & Installing Ubuntu

Double-click your virtual machine to run it, you will be prompted with a First Run Wizard, click 'Next'. First it will ask you what to use as the media source for your operating system setup. Now you can either select from your CD/DVD-ROM drives if you have Ubuntu on a CD, or simply click the folder icon to the right of the dropdown menu to load the virtual media manager where you can find the Ubuntu iso file on your machine. Click 'Finish' and your machine will start and load the Ubuntu install. Choose 'Install Ubuntu' and not 'Try Ubuntu' otherwise your changes will not be saved. See here for instructions for installing Ubuntu.

First Run & Update Ubuntu

When you load Ubuntu for the first time you will want to go to 'System' > 'Administration' > 'Update Manager' and install all available updates to make sure you have the most recent versions of all the installed software. In the top menu for your virtual machine window choose 'Devices' > 'CD/DVD Devices' and unmount the Ubuntu install ISO. Restart Ubuntu when the updates are complete using the power icon in the top right menu.

Install Guest Additions

To install Guest Additions choose 'Devices' > 'Install Guest Additions'. Then in Ubuntu under the 'Places' menu you should now see the VBOXADDITIONS CD, click this and the icon should appear on the desktop. Close the file window if it pops up. Right-click the CD icon on the desktop and choose 'Open With Autorun Prompt' and click 'Run'. Enter your password and complete the install. Press 'Return' when prompted to get back to the desktop. Right-click the icon on the desktop and 'Unmount', restart Ubuntu.

VirtualBox Snapshots

Now the base operating is installed and updated, now would be a good time to create a snapshot which can be a restore point should anything go wrong with your server. From the virtual machine window choose 'Machine' > 'Take Snapshot'. Give the snapshot a name and optionally a description. You can restore this point at any time by visiting the snapshots tab when a virtual machine is selected in the VirtualBox window and not running.

Install & Configure LAMP

Next we are going to install all the necessary components for your LAMP server via the Terminal Application which can be accessed via the 'Applications' > 'Accessories' menu.

Install Apache Server

Start off by opening the Terminal and enter the following, and hit enter:

sudo apt-get install apache2

Enter your password, when prompted enter 'Y'. Once installed you can open the Firefox Web Browser and enter http://localhost/ in the address bar to test. You should see a page showing 'It Works!'.

Install PHP 5

Using the Terminal again, enter the following, hit enter and when prompted hit 'Y':

sudo apt-get install php5 libapache2-mod-php5

Then restart the Apache web server:

sudo /etc/init.d/apache2 restart

Test PHP is working by entering the following in Terminal to create a PHP file:

sudo gedit /var/www/test.php

The Gnome Editor will load, enter the following and save, close the window to return to the Terminal:

<?php phpinfo(); ?>

If you then visit http://localhost/test.php in Firefox you will see the table showing details about your PHP installation.

Install MySQL

Enter the following in Terminal to download and install MySQL, when prompted hit 'Y' and configure your root user password:

sudo apt-get install mysql-server

Once installed, edit the my.cnf configuration file to set the MySQL server as localhost:

sudo gedit /etc/mysql/my.cnf

Look for the line which contains 'bind-address' and change it to:

bind-address = localhost

Save and close my.cnf

Install phpMyAdmin

Enter the following and press 'Y' when prompted:

sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin

Choose Apache2 and say Yes to setting up DBconfig-common (unless you know what you are doing). Set the password for the database admin, then enter your root password that you set up earlier in the MySQL installation.

Next edit php.ini to include the msql.so extension.:

sudo gedit /etc/php5/apache2/php.ini

Use cntrl + F to find 'mysql.so' and remove the ';' preceding it. Save and close. EDIT: Note this may no longer be required - if the line is not in the php.ini it's OK to skip this step.

Next edit the Apache config file to include phpMyAdmin, this is important otherwise you'll just get a 'page can not be found' when you try and open it in your browser.:

sudo gedit /etc/apache2/apache2.conf

Add this to the bottom of the file:

# Include phpMyAdmin
Include /etc/phpmyadmin/apache.conf

Save, close and then restart Apache:

sudo /etc/init.d/apache2 restart

Visit http://localhost/phpmyadmin and enter your root username and password to login.

Accessing Your Server

Viewing Your Server On Host Machine

In the terminal enter the following to find the IP address of your network adaptor for the server:

ifconfig

Look for the inet address on the eth0 adapter - it should be an IP address like 192.168.1.85. Open up your web browser on your host machine (Windows 7) and enter that IP address in the address bar to view your server and access phpmyadmin.

Setting The Server To Use A Static IP

If you wish to have your server use a static IP address enter the following in the Terminal:

sudo gedit /etc/network/interfaces

Then modify the file to look like this:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.85
netmask 255.255.255.0
network 192.168.1.255
gateway 192.168.1.254

The address IP can be whatever you wish so long as it isn't already taken by another adaptor on your network. You can find your netmask and gateway by entering 'cmd' in the search box on your Windows host machine. Look at the Ethernet adapter Network Bridge. In the command prompt enter:

ipconfig

Next you need to change the bind address in the my.cnf file:

sudo gedit /etc/mysql/my.cnf

Look for the line which contains 'bind-address' and change it to your new static IP:

bind-address = 192.168.1.85

Save and restart Ubuntu to start using the static IP.

See our next guide on sharing folders between the Windows Host and Ubuntu guest machine.