Setting up a LAMP on a Virtual Box
An almost step-by-step tutorial

Get the "AMP" from the "LAMP"

Installing the AMP (that is Apache-MySQL-PHP - however, this does not reflect the real order of the installation sequence) is the easy part. Configuring them is a bit more difficult. Many people say to use XAMPP. XAMPP is an easy to install Apache distribution containing MySQL, PHP and Perl. But let me ask you: how many production environment use XAMPP? I think none. But our goal is to simulate a production server as much as possible with all the advantages and disadvantages. And to do this we need to install manually and configure manually.

A for the Apache

Open the terminal window and type:

$> sudo apt-get install apache2

To make sure everything went well open a web browser (typically Firefox) and type in the location bar: http://localhost.

You should see something like this:

The webserver is running.

Unfortunately being root is not too safe even on a virtual machine, so we need to give the control over the web content to our default user.

In the terminal type:

$> sudo chown –R developer:developer /var/www

We also need to tell the web server that any self-created file should be created in our name. We need to edit the ennvars file:

$> sudo mcedit /etc/apache2/envvars

Search for the line (F7) ‘export APACHE_RUN_USER’ row and change that line and the next one to:

export APACHE_RUN_USER=developer export APACHE_RUN_GROUP=developer

Of course in a production server this would be a security risk, but on a virtualized developer server we can cheat a little bit. This is almost the same as adding the user ‘developer’ to the ‘www-data’ group or vice versa. With this we just simply skipped to waste time on group policies.

Before restarting the web server, we should define that this is localhost:

$> sudo mcedit /etc/apache2/httpd.conf

By default it is a blank file, so just simply add the following line:

ServerName localhost

With this we avoid the “Could not reliably determine...” message upon restart. Now type:

$> sudo /etc/init.d/apache2 restart

In the future use this command when it’s needed to restart the web server.

P for the PHP

In the second step we break the AMP sequence and jump to the P and set up the PHP scripting language. Open the terminal and type:

$> sudo apt-get install php5 libapache2-mod-php5

Restart the web server so the apache’s php5 module will be loaded. To test PHP do the following:

$> mcedit /var/www/test.php

In the editor type the following:

<?php phpinfo(); ?>

Then press F2 to save and F10 to exit the editor.

In the browser open the following URL: http://localhost/test.php

You should see something like this:

The PHP info page

M for the MySQL

Now it’s time for some database. In the terminal window type the following:

$> sudo apt-get install mysql-server

During the installation process you will be asked for an administrator (root) password to access the MySQL DBMS. I typically use some very easy to remember like ‘root’ or ‘x’ because on a production server you won’t have root access anyway (unless the server is yours) so only the ‘production-like’ user’s settings are important.

To test it, type in the terminal and give the MySQL password when asked.

$> mysql –u root –p The MySQL console

To make the MySQL work together the Apache and PHP you need to install some extensions and change the PHP’s default settings. In the terminal type (after quit from MySQL console with Ctrl+D):

$> sudo apt-get install libmysqlclient16-dev php5-mysql

We can also install some other extensions not in exact relation with the MySQL but we may need them in the future and finally “Today is a good day to die”...

$> sudo apt-get install php5-gd php5-mcrypt php5-curl

Now we have to restart the web server again. With reloading the test.php in the browser we should see MySQL and PDO sections:

New modules added to the PHP

Or you can check the module list in the Terminal:

$> php -m | grep mysql

About PDO:The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server. (Source:

To test whether the PHP can connect to the MySQL edit the test.php and use the following script:

<?php $link = mysql_connect('localhost', 'root', 'x'); if($link){ echo 'MySQL connect success!<br>'; } else { echo 'Could not connect: '.mysql_error().'<br>'; } try{ $db = new PDO('mysql:dbname=mysql;host=localhost','root','x'); echo 'PDO MySQL connect success!'; } catch(PDOException $e){ echo 'Could not connect (PDO): '.$e->getMessage(); }

If you don’t get error message, then you are ready to build your PHP applications.

MySQL connection established.