Setting up a LAMP enabled Ubuntu 11.10 VM for local WordPress and Php development

§ Programming

When it comes to local WordPress and generic Php / MySQL development, many designers find more handy to install tools like EasyPHP or Xampp on their primary operating system; personally, I’ve always found it far more interesting to instead build my own LAMP stack into a sandboxed and specifically crafted Linux virtual machine: I believe it is a more secure, scalable and adaptable setup. Also, this praxis offers a not irrelevant occasion to learn and practice some useful and very common Unix tools.

1. Installing VirtualBox and getting Ubuntu 11.10

VirtualBox is an amazing piece of software originally developed by Innotek GmbH, later acquired by Sun Microsystems and now maintained by Oracle. Choose the correct version for your operating system and follow the simple installation steps. Then browse the Ubuntu website and download the latest 32-bit .iso file: although the Linux world offers hundreds of efficient distributions, in my humble opinion Ubuntu is still one of the most reasonable choices for client side developers looking for a friendly Unix-based testing and development environment.

2. Creating your new virtual machine from scratch

Open VirtualBox and click on the «New» button in the top menu; you will be introduced to the «New Virtual Machine Wizard», that will guide you through the VM creation process. Click «Next» and type the name for your new VM; I suggest you to enter the exact title of the Ubuntu .iso file you just downloaded (for example: ubuntu-11.10-desktop-i386): the fields below will be auto-updated to the values «Linux» and «Debian». Set the «Base Memory Size» to at least 1024mb in the subsequent tab; click «Next», choose «Create new hard disk», check the «Boot Hard Disk» option and continue. You will be asked what type of virtual hard disk to create: you will probably want to prefer the «Dynamically expanding storage» option, as it initially occupies a very small space on your physical hard disk and grows as the guest operating system claims more room. In the following step, select a location where to store the virtual hard disk data and leave the «size» parameter at its default value (8 gb). Click «Next», then «Finish».

Attaching an .iso file to the newly created virtual machine

Select the newly created virtual machine by clicking on its name in the main VirtualBox panel and then press the «Settings» button in the top menu. Click the «System» item in the left menu and deselect the «Floppy» option from the «Boot order» submenu on the right. Proceed to the «Display» tab, maximize the «Video Memory» parameter to at least 128mb and enable the «3d Acceleration» option. Open the «Storage» panel, click on the «IDE Controller» empty disk and select the previously downloaded Ubuntu .iso image by clicking the other disk icon on the right (see the image above). Create an empty folder on your VM desktop (right click → «Create Folder») and name it «vb_share»; browse to the «Shared folder» tab in the VirtualBox «Settings» submenu and add the «vb_share»  folder as shared item: it will serve as an exchange point for files between your host OS and the VM.

3. Installing Ubuntu 11.10 on your virtual machine

Screenshot of the Ubuntu installation menu

Go back to VirtualBox main menu, select the virtual machine again by clicking on its name and press the «Start» icon in the top menu. The VM will boot from the attached Ubuntu .iso image that we just selected in the «Storage» panel. The installation process is very easy and straightforward; click on the «Install Ubuntu» button and just make sure to set the «Ata Vbox Harddisk» as preferred drive when prompted to select a disk (see the image above). When required to choose the keyboard layout, I suggest you to use the «Detect Keyboard Layout» feature to get the correct settings to work right away.

At the end of the installation process, you will be finally asked to fill the «Who are you?» form with an username, a computer name and a password; you should pay particular attention to the latter, as it will be fundamental to the installation and administration of the LAMP stack that we are going to configure on top of the VM’s operating system. An alert will inform you that you need to restart the machine: ignore it and power off the VM by closing its window and selecting «Power off the machine» from the «Close Virtual Machine» popup.

Open the «Settings» → «Storage» submenu and detach the Ubuntu .iso file from the VM IDE Controller by clicking on the «CD / DVD Drive» icon on the left and selecting «Remove disk from the virtual drive», as we don’t want our virtual machine to start up from the installation disk anymore.

Boot the VM by pressing the «Start» button in the VirtualBox top menu. An alert will probably warn you about the impossibility to take advantage of the new «Unity» Ubuntu interface due to poor hardware performance: ignore it; you will fix this issue in the next step by installing the «VirtualBox Guest Additions».

4. Installing VirtualBox Guest Additions

Screenshot of the VirtualBox Guest Additions installation process

Log into your virtual machine, click on the Ubuntu logo in the top left corner of the desktop, search for the «Terminal» application (see the image below) and launch it. Enter the following commands in the shell to update possibly obsolete packages, install the Dynamic Kernel Module Support dkms package and reboot the machine:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install dkms
sudo reboot

Then select «Devices» → «Install Guest Additions» from the VirtualBox VM window menu. You will be asked to choose an application to launch the «VBOXADDITIONS» software (see the image above): choose the option «Open Autorun Prompt», click «Run» on the following alert and enter your user’s password when required. Wait for the installation process to finish and then restart your virtual machine by clicking on the icon at the top right corner of the Ubuntu desktop and selecting «Restart».

Log in and create a text file on the desktop (right click → «Create Document» → «Empty File») named «vb_share.sh» and containing the following lines of code (right click on its icon → «Open with Text Editor»):

#!/bin/bash sudo mount -t vboxsf 'vb_share' /mnt/'vb_share'

Close the «Gedit» text editing application. By clicking on the «vb_share.sh» file icon and selecting «Run in Terminal» (you will be required to enter your user password to gain «root» privileges), you should be able to mount the shared folder and subsequently find it at the following location /mnt/vb_share.

5. Installing the LAMP stack

Screenshot of the Ubuntu 11.04 operating system: the Terminal application

Launch the «Terminal» application again and install, in order, the tasksel tool, the LAMP stack and PhpMyAdmin by typing the following commands (you will again be required to enter your user password):

sudo apt-get install tasksel
sudo tasksel install lamp-server
sudo apt-get install phpmyadmin

Screenshot of the MySQL Server configuration process

During the database server setup, you will be asked to enter another password for the MySQL «root» user (see the image above), that will be later required during the PhpMyAdmin installation process.

When the operation is finished, open the VM’s default Web browser (tipically Mozilla Firefox) and navigate to the following locations:

http://localhost

http://localhost/phpmyadmin

You should be able to see the Apache2 Web Server welcome page «It works !» and the PhpMyAdmin login page. Should the latter appear to be unreachable, it would be necessary to go back to the Terminal application, type the following and try again:

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf
sudo /etc/init.d/apache2 reload

6. Configuring Php and the Apache Web Server

Screenshot of the Php configuration process

Right click on the VM desktop and choose «Create New Document» → «Empty File» from the context menu; right click on the newly created file icon and select «Properties»: change the filename to «mail.out», then click on the «Permissions» tab and set the «Access» entry to «Read and write» for all users. In the next steps, you will manage to redirect outgoing mail to this text file by configuring the «php.ini» file variable «sendmail_path»: it is a neat, expedient way to test your Php email-based scripts without having to install and configure any complex and potentially insecure mail servers on your local VM.

Open the «Terminal» application and create a new, empty text file at /var/www by typing the following (in order to be able to write in the Apache DocumentRoot folder, we need to gain the «root» privileges; therefore, you will be again required to enter your user password):

sudo gedit /var/www/php-info.php

Copy the following Php code, paste it in the file you just created and save the document:

<?php phpinfo(); ?>

Close the «Gedit» application. Now relaunch your Web browser and navigate to the following location:

http://localhost/php-info.php

The phpinfo() function output should appear on screen; search the page (Ctrl + F on Mozilla Firefox and Chromium) for each of the following lines and take note of their corresponding values:

Loaded Configuration File
date.timezone

The «Loaded Configuration File» variable should be equal to /etc/php5/apache2/php.ini, while the «date.timezone» entry should be empty. Open the «php.ini» file by typing the following in the «Terminal» application:

sudo gedit /path-to-loaded-configuration-file/php.ini

Find the «date.timezone» entry, uncomment it by deleting the semicolon and set its value according to your local timezone; for example:

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Europe/Rome
; http://php.net/date.default-latitude
; date.default_latitude = 31.7667

Search for the «sendmail_path» variable, uncomment it if necessary and update its value:

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = tee -a /home/your-username-chosen-during-ubuntu-installation/Desktop/mail.out

Screenshot of the php.ini file

Note the Unix command «tee» followed by the option «-a» (append) and the path to the «mail.out» file you created at the beginning of this chapter.

Get back to the «Terminal» application, restart the Apache Web Server to make the changes to «php.ini» take effect and create a new empty Php file named «test-mail.php» by typing the following:

sudo /etc/init.d/apache2 reload
sudo gedit /var/www/test-mail.php

Copy and paste into it the following code, save it and close the «Gedit» application:

<?php
$to = 'nobody@example.com';
$subject = 'Your subject';
$message = 'Your message';
$headers = 'From: webmaster@example.com' . PHP_EOL .
           'Reply-To: webmaster@example.com' . PHP_EOL .
           'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); ?>

Run the Php script by browsing to the following URI:

http://localhost/test-mail.php

Now open the «mail.out» text file previously created on the desktop; if all is gone well, you should be able to verify that the following output was written in it:

To: nobody@example.com
Subject: Your subject
X-PHP-Originating-Script: 0:php-info.php
From: webmaster@example.com
Reply-To: webmaster@example.com
X-Mailer: PHP/5.3.5-1ubuntu7.2
Your message

Lastly, it is necessary to enable Apache’s Web Server «mod.rewrite» module and symlinks feature to allow your possible WordPress installation to properly use custom permalinks. Get back to the «Terminal» application and enter the following commands:

sudo a2enmod rewrite
sudo gedit /etc/apache2/sites-enabled/000-default

The «000-default» configuration file will open in the text editor ready to be modified; search for the following block of parameters and change the directives «AllowOverride None» to «AllowOverride All»:

<VirtualHost *:80>
   ServerAdmin webmaster@localhost
   
   DocumentRoot /var/www
   <Directory />
      Options FollowSymLinks
      AllowOverride All
   </Directory>
   <Directory /var/www/>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
   </Directory>

Save the file, close the «Gedit» application and restart the Apache Web Server:

sudo /etc/init.d/apache2 reload

Your new local LAMP enabled testing environment is now ready to rock. Enjoy !

Leave a reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>