Often Linux just does it better! Often I find myself developing a Windows machine without access to a Linux development server, but I still need to access to some of the Linux binaries and features such as cron jobs, the at command and binaries such as imagemagick, pdftotext, etc. Some things can be emulated with ported binaries or through Cygwin, but I feel a lot more comfortable developing on a platform that is representative of the live server the web site will run on.
I have not removed XAMPP as it is very useful for developing small scripts or sites without the overhead of running a virtual machine. Therein lies the problem with this solution – it is virtual machine based and that will mean your local development machine will suffer when the virtual machine steals computing time from the CPU or more memory. So whilst I can use XAMPP on my netbook I am not so sure a VM will run smoothly.
Setting up a new development environment
Download VirtualBox 2.1 from http://www.virtualbox.org
Download the latest Ubuntu from http://www.ubuntu.com Your choice of server or desktop. I chose desktop to give me another web browser testing environment.
Install VirtualBox and open it up
Click create new VM (Virtual Machine)
Give your VM name (I called mine Development)
From the drop downs choose
- Linux
- Ubuntu (only choose Ubuntu 64 bit if you are running 64 bit Windows)
Choose how much of your machines physical memory the virtual machine is allowed to steal. I chose 512MB as I am running the desktop version of Ubuntu.
You will need to create a new virtual hard disk – a new hard disk wizard will open
- Choose dynamically expanding storage
- Choose
- Give the virtual hard disk a name (mine is called Development just like my virtual machine)
- The location of the storage on your hard drive
Now your new virtual machine should be setup so right click on it and choose Start
The Virtual Machine will pop up a message asking you to go through its first run wizard
Choose your installation location
- CD/DVD ROM
- Image file – point this to the location of your Ubuntu installation ISO you downloaded and saved earlier
This will then mount your CD image and boot from it
Install Ubuntu by going through its installation wizard which is very easy and does not require detail here (if you have chosen the desktop version this will be even easier because you can use the mouse!)
Once you have the virtual machine setup you will want the nice Linux drivers so you can have a larger screen size and for networking etc. For this you need to install the Virtual Guest Additions.
- Mount a new CD drive/ISO Image in your virtual machine whilst it is running from the top menu item called Devices
- Add the new image which you will find in your VirtualBox program folder
Back to your virtual machine and open up a terminal window
- In your terminal navigate to the CD Rom drive
- Execute the following command sudo sh ./VBoxLinuxAdditions-x86.run
- Once complete you need to reboot the machine so execute sudo reboot
Once the machine reboots get back into the terminal and run the following commands
- sudo apt-get update
- sudo apt-get upgrade
- sudo apt-get install apache2 mysql-server-5.0 php5 php5-xdebug
- Just to be on the safe side restart apache with sudo /etc/init.d/apache2 restart
- Try accessing your web server by opening a web browser in the virtual machine and typing in localhost if you chose the desktop and you should see a message like It Works!
We need to be able to see folders on our host machine inside the development machine quickly and easy so from the top menu bar choose Devices -> Shared Folders
- Add a new folder
- Tell it which folder to look in
- Give it a memorable name
- Tick make permanent
Jump back into the virtual machine and execute the following commands to add your shared folder to the virtual machine, in a terminal
sudo mkdir /mnt/yourFolderNameHere (mine is /mnt/htdocs/)
sudo mount -t vboxsf memorableFolderName /mnt/yourFolderNameHere
- Replace memorableFolderName with name you set in step 9.III
- Replace /mnt/yourFolderNameHere with the folder you made in step 10.I
If you navigate to /mnt/yourFolderNameHere and execute ls you should see a list of the files on your host systems shared folder
If that worked then we want to add our folder to the fstab file so that the mount point is loaded every time our virtual machine boots up
sudo vim /etc/fstab
Add the following line to the bottom of the file:
memorableFolderName /mnt/yourFolderNameHere vboxsf defaults 0 0- Replace memorableFolderName with name you set in step 9.III
- Replace /mnt/yourFolderNameHere with the folder you made in step 10.I
Now to make this development machine available over the network shut the virtual machine down by going to top menu bar Machine -> Close and choosing Power Off
Right click on the virtual machine and choose settings
From the left hand menu choose Network
- Tick Enable Adapter
- Adapter Type: Intel PRO/1000 T Server
- Attached to: Host Interface
- Tick Cable Connected
- Choose the interface on your host machine that gives you access to the outside world. You can find out which one this is by looking in your control panel.
Save the settings and start your virtual machine back up again
To find out the IP address of your virtual machine open a terminal execute ifconfig
You should now be able to visit your web server from your host machine by entering that IP address into your web browser.
Configuring our web server
We need to enable mod_rewrite which involves making a simple symbolic link
- Navigate to /etc/apache2/mods-enabled
- Execute sudo ln -s /etc/apache2/mods-available/rewrite.load rewrite.load
Reboot the server sudo /etc/init.d/apache2 restart and test it is function ok.
Setting up Mass Virtual hosts
The idea behind this step is to minimise the time it takes to setup a new host on the server. Basically all you have to do is create a new directory an away you go. (Don’t forget it will still need to appear in your hosts file).
We need to enable mod_vhost_alias
- Navigate to /etc/apache2/mods-enabled
- Execute sudo ln -s /etc/apache2/mods-available/vhost_alias.load vhost_alias.load
Now to setup the rules it will create virtual hosts by
Execute sudo vim /etc/apache2/apache2.conf
Go to the base of the file and add the following rules:
# Mass Virtual Hosting UseCanonicalName Off LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon CustomLog /var/log/apache2/access.log vcommon VirtualDocumentRoot /mnt/htdocs/%1 <Directory /mnt/htdocs/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny Allow from all </Directory>
VirtualDocumentRoot sets the directory that the VHosts public directory is contained in. This will basically convert http://subdomain.localhost/ to /mnt/htdocs/subdomain and pull the relevant files through.
Now restart the apache process sudo /etc/init.d/apache2 restart
A side note when using this method that you should be aware of. This will affect your rewrite rules if they are placed into a .htaccess file. To avoid any problems always declare the RewriteBase rule in your .htaccess.
For example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
VirtualBox Shared Folders Permissions
Currently you maybe having permissions issues with your shared folders because they might be being mounted as root:root. To get them to load with a specified user and group you will need to edit your /etc/fstab file again and change ‘defaults‘ to be ‘uid=username,gid=groupname‘ – an example would be ‘uid=simon,gid=www-data‘.
A full line example would be from:htdocs /mnt/htdocs vboxsf defaults 0 0
to htdocs /mnt/htdocs vboxsf uid=simon,gid=www-data 0 0