Log in

Sign in with your email address and password:

No account? Sign up here.
Forgot your password? Reset it here.
Didn't receive account confirmation email? Request a new one.


LAMP Local Development Environment Using VirtualBox

In this short article we’ll look at setting up a Linux Apache MySQL PHP (LAMP) development environment locally using VirtualBox and a Windows machine as host. While I’ve successfully used Windows-based WAMPserver, I actually prefer this approach over WAMP because it replicates a real Linux-based server more accurately (in most cases if the same O/S is used for the online webserver, configuration settings are simple to port over). So, let’s get started!

1.  Install VirtualBox on your PC and create a virtual machine (VM)

Here‘s an excellent tutorial on getting a VM up and running on VirtualBox – simply follow these steps exactly. At the time of writing, I installed Ubuntu Server LTS 14.04 (note that all you need is Ubuntu Server, not the desktop version). The fantastic thing about installing Ubuntu Server is that the folks at Ubuntu have created the setup process such that installing common packages are a breeze. Select the following packages/modules during installation:

Ubuntu Server Package Selection Install During the installation process, make a note of the usernames and passwords such as your Ubuntu username & password, same for MySQL root password. I like using keepass for keeping all passwords stored securely without having to memorize them. After finishing the Ubuntu Server installation, you will be asked to log in. Go ahead and give that a whirl, to make sure everything’s working OK. Now close your VM (send shutdown signal).

Set the network bridge adapter

In the VirtualBox VM Manager, select your newly created VM and click Settings. Under Network, make sure Adapter 1 is enabled and change ‘Attach to’ from NAT to ‘Bridged Adapter’: VM Bridged Adapter

 

Note: If this does not work for you, try applying these settings. (I used IP address 192.168.56.102 and it worked for me when using wireless internet and the above setting did not work). In fact using this static IP setting is better since your IP won’t change …

 

2. Connect to your VM using Putty

We are going to connect to our VM using Putty. The reason for this is that we want to replicate a real remote web server as closely as possible. Another nice benefit is that we can tunnel MySQL connections from remote servers (in our case the VM is actually local but we’ll treat it as any other remote web server). If you haven’t already got it, go ahead and install Putty from the download page. (Note: We’re installing on our host system – Windows, not on our newly installed VM).

Add the VM IP address to your PC’s hosts file

Fire up your VM and log in to the Ubuntu bash command line. On the command line, type ‘ifconfig’ and make a note of the IP address: VM IP Address In my case, the IP is 192.168.0.7 Note: Each time you start up your VM, this IP address can (and likely will) change. The Windows hosts file entry will need to be updated (described below) each time this happens. This is probably the only downside of the VM approach, but if you keep your VM running most of the time (like I do) you will not have to redo this step much. On your PC: Fire up notepad in administrator mode (right-click on the Notepad icon and select ‘Run as administrator’). Open up your Windows hosts file (usually located at: ‘C:\Windows\System32\drivers\etc\hosts’ – make sure to select ‘All Files (*.*)’ in the file open dialog box from Notepad, since the hosts file has no extension). At the bottom of this file, add the IP address of your VM and the server/host name (whatever URL you like, I’ve opted for ‘twdev.localhost’ since I’ll be using it for twoggle development) to access your VM from within windows: Add IP Address to hosts file Save the hosts file and close it. At this point a good test is to ping your new host name from within a Windows command console (in Windows, go to start and type cmd.exe in the search box to launch the console).

You can also point your browser to your host name (in my case twdev.localhost) and you should see the default ‘Apache2 Ubuntu Default Page: It works’ titled page. If not, something has gone wrong and the above steps should be repeated / inspected to make sure you’ve followed all steps.

Connect using Putty

Fire up Putty and create a  new session, entering the host name (not IP address) under ‘host name’, port 22, etc. I usually put the username under the ‘Data’ sub-menu too, saving you having to type it in the bash console each time. You can also change the appearance of this session – I like using a different colour for different sesssions / connections, making it easy to distinguish between them in the Windows task bar (especially when grouped together). Remember, after any setting changes in Putty, to go back to sessions menu and hit ‘Save’. (Putty is weird like that).

Log in to your VM using Putty.

3. Set up an Apache2 VirtualHost file

Create the root folder for your development site

Change directory to /var/www and create the folder in which your development site will live. I use the following convention:

This structure lets me put log files and things in the twdev.localhost folder, while leaving the public_html folder exclusively for web (php) files. Most web servers are also structured in this way, so deploying to an online site will not take too many modifications.

We also need to set the correct folder permissions (or our site will complain it can’t be accessed).

Navigate to the /var/ folder.

Add your username to www-data group:

Change folder permissions:

We now have a root web folder set up with correct permissions – hooray!

Create the Apache2 virtualhost file

Change to directory: /etc/apache2/sites-available

Copy the default VirtualHost file to the same servername as your host name. Note: The new file must have extension ‘.conf’

Open the new file (using sudo!) for editing (I like using vim but use whatever you prefer, vi, nano, etc) and make amendments according to the figure below:

Ubuntu Apache2 VirtualHosts file

Once you’re finished editing the sites-available .conf file, save it and exit. Now go to /etc/apache2/sites-enabled and create a symbolic link as follows:

Run ‘ll’ to make sure your symbolic link was successfully created.

Restart the Apache2 server:

Test our new PHP development environment

In our web root folder, create an index.php file, and put the following contents in it (make sure the file start with ‘<?php’ of course):

Save the file.

In Windows, point your browser to the host name. We should now see the output of our index.php script!

LAMP VM Dev Environment Test

 

We now have a fully functional development server set up. From here, we can treat our VM as any other Linux server: Install git, phpmyadmin or any other tools. From here I will be installing the new Yii2 PHP framework, which also requires a MySQL connection of course (tip: MySQL is already running, simply connect to it using localhost and your previously selected authentication details!).

I hope you’ve found the article useful (please share it if you have!). Any questions or comments, just post them below. Happy web development!

Note: If you have trouble with setting up virtualization and have to install Ubuntu 12.04 server (32-bit), you will need to most likely upgrade to PHP 5.5 in order to run things like Yii2 and the latest PHP frameworks. If you have, during installation, selected a country other than US (I picked Australia) and then can’t seem to install certain packages, regenerate your sources list here (using United States as the selected country): http://repogen.simplylinux.ch/  and then follow the instructions to upgrade to PHP 5.5 here.

Note 2: If requiring modules like mcrypt or GD to be installed, make sure to install these first before upgrading to PHP5.5 as some Ubuntu installations will actually uninstall PHP when installing mcrypt (annoying, yes!).

 


Comments

Submit a Comment