Virtual machine LAMP server setup

From matiaslanzi
Jump to: navigation, search

Steps to create a local web development environment using a virtual machine.


  1. Virtual Box
  2. Ubuntu Server
  3. Apache
  4. MySQL
  5. PHP
  6. phpMyAdmin

You can download VirtualBox and Ubuntu server from their respective websites. MySQL, Apache and PHP comes as an optional install to Ubuntu server, we are currently using version 12.4.

Setting up the host environment

The host is the machine where we are going to install the virtualization software, so we need to make some configuration changes to the environment in order to achieve communication with the virtual server. This is achieved in two steps as follows:

1. Add an entry to the hosts file with the server name pointing to the local host. This is achieved by modifying /etc/hosts (linux) adding the desired server name and point it to the local IP Add the following line to the end of the file.

2. Create a folder to countain all the files to be served, this will be used as your site's root directory. You can place this directory anywhere in your host machine.

Setting up VirtualBox

If you haven't installed VirtualBox, this is the time to do it. Once VirtualBox is installed we will proceed to create a virtual machine that will host our operating system and server.

Launch VirtualBox, this will present a welcome message in the VirtualBox manager that allows for the creation of virtual machines.

Select New from the buttons presented at the top of the interface to create a new setup.

Name and operating system

This is where we will name the virtual machine and select what type of operating system we want to install.

We will give it the name "devserver". The sytem type is "Linux" The version is "Ubuntu 64bit"

You can install any desired operating system, but for our purpose we will use Ubuntu.

Memory size

This is the amount of RAM that will be available to the operating system. The default 512MB might too small for our purpose so lets enter something more generous like 4 or 8 GB.

Hard Drive

Here we will create a virtual hard drive, a file that will act as an HD for our operating system.

Select Create a virtual hard drive now.

Hard drive file type

Select VDI VirtualBox Disk Image.

Storage on physical hard drive

Select what type of storage you want to create.

Dynamically allocated will create a file that expands according to its usage. This option never contracts.

Fixed size will create a file that never changes size.

Use what ever suits your needs.

File location and size

Select where to store your hard drive image file. I recommend storing all your image files under the VirtualBox VMs folder located in the users directory.

Give it a size keeping in mind that all we're storing in this file is the operating system and not the site or sites to be served. So something around 8GB is a good number. This should be accessed according to your needs. This parameter can not be changed later.

This concludes the basic set up of a virtual machine. Further configuration is needed in order to install the system software and port routing.

Configure the virtual machine

Now we will proceed to configure the virtual machine with its operating system which is the virtual host that will serve the website, the server. For this we need to have a iso image of the operating system we want to install. In this case we will use Ubuntu 12.4 (64bit). Make sure you download it from the respective website.

Make sure the virtual machine previously created is selected and click on the "Settings" icon on the toolbar.

1. Select "Storage" and load the system image on the 'virtual' CD drive.

2. Go to "Network" and forward 3 ports as follows:

name      host     guest
HTTP      8080     80
MySQL     9306     3306
SSH       2222     22

or change the network adapter to "Bridged Adapter", this will make your virtual machine show up as a separate machine.

3. In "Shared Folders" add the folder that will contain web data. No need to remove the existing entry. Do not select "Auto mount" this puts the folder in the /media/folderName directory which will have conflicting permissions.

Close the settings.

Install Ubuntu

Now select the vm from the manager an start!

Ubuntu is now being installed...

Dismiss the annoying sheet, it's informative but annoying.

Go through the installation and make sure to include the LAMP server and OpenSSH.

Communicate with the VM

Basically SSH to the new VM using port 2222 if NAT is enabled or to port 22 if Bridged.

ssh -lusername -p2222

Now update and patch your new system.

Guest adittions

Once Ubuntu is installed and running, select 'Install guest additions' from the 'Devices' menu of your virtual machine console. This will make the guest additions installation available to the os. Guest additions are the glue that ties guest and host machines.


sudo apt-get install build-essential module-assistant linux-headers-$(uname -r) dkms zip unzip

Now mount it.

sudo mount /dev/cdrom /media/cdrom

Execute the installer, notice flag to avoid x11.

It is safe to ignore the Window System drivers error.

sudo sh /media/cdrom/ --nox11

Now lets add some users to the vboxsf group

sudo usermod -a -G vboxsf username
sudo usermod -a -G vboxsf www-data

Shared folder mapping

In order to use the shared folder created in the VM settings, we need to map it to the file system. I found that if the "Auto mount" option is selected when creating the folder in the VM settings, the server will have trouble accessing the directory. The workaround is to map it to any other directory. Please see Article

Server configuration

Apache configuration

Enable URL Rewriting module

sudo a2enmod rewrite

Create new site

sudo nano /etc/apache2/sites-available/sitename.conf

Use this basic site, for more options consult apache manual (paste this inside).

<VirtualHost *:80>
        DocumentRoot /media/sf_mysite/

        LogLevel info
        ErrorLog ${APACHE_LOG_DIR}/
        CustomLog ${APACHE_LOG_DIR}/ combined

        RewriteEngine On
        LogLevel warn

Now we can enable the site using:

sudo a2ensite sitename

PHP configuration

Php likes to run silent by default, lets enable some settings...

The php configuration is usually located in /etc/php5/apache2/php.ini, so lets use nano to edit it.

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

Search and modify the following entries...

error_reporting = E_ALL | E_STRICT
display_errors = On 
log_errors = On
log_errors_max_len = 0
error_log = /media/sf_devserver/php_errors.log

MySQL configuration

Edit the MySQL configuration located in /etc/mysql/my.cnf

Search for bind-address and change the IP to, this allows connections from all IPs.

bind-address =

Test server

Now that all software is installed we can test the server by typing its address in a browser.


This should show the "It's Working Page".

This is the default website on the server, if you set up another website, type its address and 8080 as the port.

The virtual server setup is complete.