laurence dougal myers

EDIT 2012/04/23: This post was originally going to be the first in a series describing how to convert a static website into one that makes use of dynamic (server-side) technologies. However, I got embarressed with my PHP code and worried about security, so I've removed most of the post, but I have kept the instructions on setting up LAMPStack in a Virtual Machine.

The Development Environment

Deploying "work in progress" code to a live server is a terrible idea, especially when once is bumbling around trying to learn PHP. You should use a dedicated development environment. I primarily use Windows as my operating system, and I don't want to clutter things up installing PHP and MySQL just for a single project. A virtual machine sounds like the best approach - you can install whatever you want, set it up as a web server, muck around with configurations etc to your hearts content, and when you're not using it you just turn it off and continue with your everyday computering.

There's a few Virtual Machine host applications out there, I'm going with VirtualBox because it's free (for now).

I stumbled across a full Ubuntu virtual machine image, Bitnami LAMPStack. This contains an installation of Ubuntu Linux, with Apache, MySQL, and PHP already installed. Too easy! (They also provide an OpenSUSE image, if that's more your fancy.)

... Unfortunately, it doesn't quite work how I expected out of the box, so there's some tweaking required. I want to treat the VM the same as a remote host, so we need to open up some settings for remote connections.

Import Virtual Machine into VirtualBox

While the LAMPStack image contains an OVF file, which is a virtual machine configuration file that should be readable by VirtualBox, I received odd error messages. This isn't a big deal, you just need to manually create your own virtual machine and import the LAMPStack hard drive image.

First, download and install VirtualBox. Then download and extract the Bitnami LAMPStack virtual machine images somewhere. I believe the hard drive images are configured to expand to 17 GB as required, but from a fresh download they should be 1 GB big (or less).

  • Open VirtualBox
  • Click "New"
  • Name = "LAMPStack (BitNami)"
  • OS = Linux / Ubuntu
  • Next, choose how much memory to give the VM. Allocate at least 512 MB, I'm going with 1024 MB.
  • Next, "use existing hard drive", and point to the "VMDK" file in the Bitnami VM images. In my case, this was "bitnami-lampstack-5.3.8-2-ubuntu-10.10.vmdk".
  • Once done importing, select the new VM and click on "Settings"
  • System->Processor, tick "Enable PAE/NX"
  • Storage, click the "Add Hard Disk" icon on the IDE Controller, choose the existing VMDK image (same as before)
  • Storage, delete the SATA Controller entry for that VMDK image
  • (Optional) Network, choose "Bridged Adapter". This depends on your network configuration and how you want to be able to access the virtual machine. I found going with NAT assigned an IP address on the wrong network (e.g. 10.0.0.1 instead of 192.168.1.1).

Start Virtual Machine & Login

Start up the virtual machine. It will list some information, such as the VM's IP address. For my examples, I will be using IP address 192.168.1.2 to refer to the VM.

You will also be prompted to log in. The default login is:

bitnami / bitnami
You'll be prompted to change the password for the bitnami account.

Enable SSH/SFTP Access for File Transfers

We need a way to copy files onto the VM. The easiest option seems to be SFTP. You'll need an SFTP client, I use WinSCP but Filezilla also works.

We need to change a configuration file on the server to enable SSH connections. Luckily, BitNami have already provided a configuration file, we just need to replace the existing file. In the VM, run these commands (without the "$"):

$ sudo mv /etc/init/ssh.conf.back /etc/init/ssh.conf
$ sudo start ssh
Using WinSCP, you can connect to your VM from its IP address (e.g. 192.168.1.2), using the "bitnami" user login.

Web pages are served from this directory, which is primarily where we want to put files:

/opt/bitnami/apache2/htdocs

Enable MySQL Remote Access

I want to manipulate the database from my real computer, not the VM, so we need to enable remote access to MySQL. There's a bit of jiggery-pokery required to enable remote access to the MySQL database.

Note that if you add any MySQL users, you should grant access both from the "any host" wildcard ("%"), and from the various local host designations ("localhost", "127.0.0.1"), otherwise you may have issues logging in to MySQL on the VM (which will affect your application).

Root access

By default there is only one MySQL user set up, with this login:
root / bitnami

Grant root access from any host

$ /opt/bitnami/mysql/bin/mysql -u root -p -e "grant all privileges on *.* to 'root'@'%' identified by 'your_root_password'";
The "'root'@'%'" part says that the user "root", from any host, should be able to to anything on the server.

NOTE: this is probably very bad practice. You should only grant as much access as you want to non-root users. However, this is convenient for our development environment, as it allows us to fully manipulate the database from our main computer. Don't do this sort of thing on your real server!

Disable bind address

Open up "/installdir/msyql/my.cnf" in vi. Comment out this line by adding a "#" to the start:
bind-address = 127.0.0.1
(Some vi hints: use ":w" to save and ":q" to quit. You may need to press the Insert key before you can enter the hash symbol.)

Poke a hole in the firewall

Run this command to open up port 3306 (the default MySQL port):
$ sudo ufw allow 3306

Enable phpMyAdmin Remote Access

phpMyAdmin is a useful web interface to administer MySQL. By default, it is restricted to "localhost" access, so we need to change this.

Using vi, edit the configuration file at "/opt/bitnami/apps/phpmyadmin/conf/phpmyadmin.conf". Change the line highlighted in red (by default, it will say "Allow from 127.0.0.1").

  Alias /phpmyadmin "/opt/bitnami/apps/phpmyadmin/htdocs"

<Directory "/opt/bitnami/apps/phpmyadmin/htdocs">
AuthType Basic AuthName phpMyAdmin AuthUserFile "/opt/bitnami/apache2/users" Require valid-user Order allow,deny

  Allow from all
  Satisfy all
  ErrorDocument 403 "For security reasons, this URL is only accesible
  using localhost (127.0.0.1) as the hostname"
  </Directory>
You can then access the phpMyAdmin page from this URL (changing the IP address to your VM's):
http://192.168.1.108/phpmyadmin/
Log in using the MySQL "root" username & password.

Done

Finally, your development environment is ready to use! It's a good idea to restart the VM after making the above changes, but afterwards you can just let it sleep/hibernate when you're not using it, and it'll be ready to rock next time.