Basic Magento/PHP development environment - Part 2

In this second part we're continue some setup and go through a test magento install. You can find the first part here if you missed it.

MySQL configuration

We now need to grant our host machine access to connect to MySQL on our vm. Here you'll use the MySQL root password you setup previously. Note: if you used a different IP address adjust the commands accordingly.

mysql -u root -p

Once connected run the following SQL (fee free to change the password):

su root
nano /etc/mysql/my.cnf

Find and comment out the bind-address setting:

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           =

Restart MySQL:

/etc/init.d/mysql restart

Using an external database manager such as SequelPro or MySQL Workbench you should be able to connect to the database on the virtual machine.

I'll be using SequelPro on my system, but the settings should be similar for MySQL Workbench.

We'll go ahead an add a database named "magedev" that we'll use later on during Magento's installation.

Sharing folders with guest system.

We'll now setup a local directory that will be accessed on the virtual machine. This will allow applications on the host machine to work on files locally and changes will be immediately seen by the guest vm. There will be no need to transfer files or mount the remote host via [s]ftp or sabma.

I personally like to keep all project files inside of ~/Documents/Projects. I will share this parent directory so that any new project I add will be accessible by the vm. From the Devices menu select "Shared Folders", click on the Add Shared folder icon.

Folder settings:

On the virtual machine run the following:

su root
mkdir /mnt/Projects</b>
chown magedev:magedev /mnt/Projects/

We'll now configure our shared folder to mount on start up:

nano /etc/fstab

Add the following to the bottom of the file:

Projects /mnt/Projects vboxsf defaults,ttl=5,uid=1000,gid=1000  0 0

Now run:

mount -a
ls /mnt/Projects/

You should see any files you have on your local Projects folder. If you started with a new directory, try adding some files to make sure everything is working properly. Note, the uid and gui are of our magedev user, chances are that your system should have the same values.

Installing Magento

We'll start off by downloading the source off of Magento's SVN. Feel free to download the zip/tar and extract the source yourself from here. From the host machine:

svn export ~/Documents/Projects/magedev

Lets add a local host for our project, add magedev.vm

to your /etc/hosts file.

Now we must link the ~/Documents/Projects/magedev directory to our magedev user's public_html directory. On the virtual machine run:

ln -s /mnt/Projects/magedev /home/magedev/public_html/magedev

Now navigate to http://magedev.vm/, you should be greeted by the Magento Installation Wizard. As these next

Localization settings:

If you've been following along the information for MySQL should be:

host: localhost
datbase: magedev
username: root
password: magedev

Web Access Options:
Base URL: http://magedev.vm/
Admin Path: admin
Enable Charts: Checked
Skip Base URL Validation...: Checked
Use Web Server (Apache) Rewrites: Checked
Use Secure URLs (SSL): Unchecked

Session can be set to "File System" (not shown in screen shot).

Once you hit continue Magento will proceed to install, once completed you can create the initial admin account:

This is the final step in the installation and you should be able to go the backend and frontend.

If everything worked out well for you then you should see the same results. If you ran into any issues feel free to leave a comment.

One Small Kink

Due to the use of VirtualDocumentRoot directive in Apache mod_rewrite tends to break/get confused and magento's .htaccess needs some tweaking. Modify magento's .htaccess file:

nano .htaccess

Locate the "#RewriteBase /magento" line and replace it with: "RewriteBase /".

Basic Magento/PHP development environment - Part 1

In this tutorial we'll walk through setting up a basic PHP development environment for use with Magento. This will be a two part series. The first half we'll be primarily focused on installing PHP, MySQL, Apache on Debian using VirtualBox as our virtualization platform. This walk through was done on OS X 10.6.8, but should not be too different on *nix or Windows. Let's get to it.


Virtual Box Downloads
Debian: I chose the Small CDs, amd64 ISO image, the version at the time of writing is 6.0.4 and can be downloaded here.

Creating a new virtual machine

Click on the "new" button to create a new virtual machine.

Give your new virtual machine a name, e.g "MageDev", select "Linux" as the Operating System and "Debian 64bit" as the version.

For memory settings I set the value to 512MB. Choose accordingly based on your host system's configuration but don't be too cheap.

For the Virtual Hard Disk settings, leave "Start-Up Disk" checked, select "Create new Hard Disk".

The "Create New Virtual Disk" prompt will appear, leave the disk type as VDI.

The next prompt will ask if you'd like the disk dynamically sized, I left it as is, feel free chose differently.

Select a name for the disk, e.g. MageDev and an appropriate size, I chose 10GB.

Continue and create the virtual machine, it will appear in your list of machines.

Installing Debian

Highlight "MageDev", click on Settings then click on Storage Tab, click on the disk icon to the right of the drop down.

Select "Choose a virtual CD/DVD disk file." Select the downloaded Debian ISO image file, click OK.

Close the settings window and start the MageDev machine. You should now be greeted by the Debian installer screen.

At around this point you may see the "Mouse pointer integration" information box. Read the details and feel free to tick the box as it will continue to appear every time the virtual machine is restarted.

Select "Install" and press enter. The next dialog windows can be changed to suite your preferences, I chose the following:
Language: English,
Location: US,
Key map: American English

The next few screens will be network settings, I went with the following. Feel free to change them if you're comfortable doing so:

Hostname: mage
Domain name: dev

You'll then get prompted to provide a password for the root system user and an initial user account. Feeling creative, I went with the following:

Root password: magedev
User account:
Full name: mage dev
Username: magedev
Password: magedev

Select time zone: Pacific

The next screens will deal with disk partitioning, I went with the following options for the next screens:
Guided - use entire disk
Select the SCSI1 disk (should be the only available option)
Disk partitioning: All files in one partition (make it easier on yourself)
Finally select "Finish partitioning and write changes to disk".

If you followed along correctly you should have another prompt that looks like this.

Select "yes" and press enter. The base system will now install. Once this has completed a configuration screen for the package manager will appear. I selected US as the mirror country, being the yank that I am, and went with the first mirror on the list. Feel free to go with the defaults of your region or gamble and try something different.

The following screen allows you to configure the proxy server, leave this field blank unless you know what you're doing. The package manager files will proceed to download.

Once that process has completed a prompt for the package "popularity-contest" will appear. Choose "no" unless you care to provide anonymous package statistic information back to Debian.

The next screen will be "Software selection", I did the following:
De-select "Graphical desktop environment"
De-select "Laptop" (if selected)

There's no need to select web server, as we'll install those packages manually later on.
Select Continue.

When the Grub bootloader prompt shows up select "yes" to install it. After Grub has been installed the machine will reboot and start up.

Network Configuration

Unfortunately the default network settings does not allow the host machine to connect to the guest. This is unlike VMware's NAT adapter which does allow the host to communicate to the guest.

To work around this behavior we'll add an additional adapter to the guest machine, in order to do so shutdown the guest. Select "ACPI Shutdown" from the Machine menu.

Select the MageDev virtual machine and click on the Settings icon, select the Network tab. Click on the "Adapter 2" tab, check the "Enable Network Adapter" box, select "Host-only Adapter" from the "Attached to" drop down.
Click OK.

You may also detach the debian ISO used to install the OS at this point from the Storage settings.

Once the machine has started up you can login with your super secret username and password, magedev. We'll need to login as root in order to do the final network configuration, run "su root" and enter the password.


nano /etc/network/interfaces

it will open the file in a basic text editor. We'll need some information from the host machine: run ifconfig or ipconfig (for Windows). There should be an interface named "vboxnet0" and with similar settings:

vboxnet0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	ether 0a:00:27:00:00:00 
	inet netmask 0xffffff00 broadcast

Note the IP address, you will use this information in the guests networking interfaces file.

Add the following to the file:

auto eth1
iface eth1 inet static

Note, if your local vboxnet interface IP address is different, e.g. choose something like for the guest's IP address.

Press ctrl+x, and Y to save and exit. Reboot the virtual machine, run "reboot", once it has started up you should be able to ping the guest machine from your machines terminal:

PING ( 56 data bytes
64 bytes from icmp_seq=0 ttl=64 time=0.550 ms
64 bytes from icmp_seq=1 ttl=64 time=0.318 ms
64 bytes from icmp_seq=2 ttl=64 time=0.415 ms
--- ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.318/0.428/0.550/0.095 ms

If everything went we well you should see similar results and can now proceed to installing the software packages.

Installing software

In order to install the necessary packages we'll need to run as root, login and run "su root" as before.

To make things easier we'll start off by installing an SSH server, run:

apt-getinstall install openssh-server

Once that's installed from the host machine you can now ssh to the virtual machine using Terminal or PuTTY.

ssh magedev@

Now that ssh is running you can minimize the virtual machine window and work in your favorite terminal/ssh client.

All of the the following commands must be ran as the root user , so make sure to "su root". During the next few steps you may get a prompt asking if you want to continue , press Y if everything looks OK.

Optional, you're favorite command line editor, which presumably is vim

apt-get install vim


apt-get install apache2.2-common


apt-get install mysql-common mysql-server

You will get a prompt asking for a root user password. For those not too familiar with MySQL, this is not the same as the system's root password. Choose something clever, perhaps "magedev"?


apt-get install php5-common php5-curl php5-dev php5-gd php5-mcrypt php5-mysql php5-sqlite php-pear

Note, this ended up installing the php-suhosin package, which is an extension for hardening PHP. I chose to remove this package as it may require some additional configuration to get Magento to run. If you intend on using the extension on your production server you may want to leave it or install it at a later point.

apt-get --purge remove php5-suhosin

Assuming you did not run into any trouble you should now be able to access apache from your browser. Go to (or the IP address you set in the vm). You should see an "It works!" page:

Configuring Apache

As this is a development setup, we'll do something a bit different than the typical Apache install. We'll be using Apache's VirtualDocumentRoot directive to dynamically pull up sites based on the requested host name. This will allow us to have the following type of mapping:

test.vm -> will point to /home/magedev/public_html/test
foo.vm -> will point to /home/magedev/public_html/foo
admin-foo.vm -> will point to /home/magedev/public_html/admin-foo -> will point to /home/magedev/public_html/ -> will point to /home/magedev/public_html/bar

There will be no need to restart apache whenever we want to start a new project, only map the host name on our host system to point to our virtual machine. Those steps are describe further below.

We'll now disable default Apache configuration:

cd /etc/apache2/sites-available/
unlink ../sites-enabled/000-default
nano magedev

Place the following in the editor, save and quit.

<VirtualHost *:80>
  <Directory /home/magedev/public_html>
        Options Indexes FollowSymLinks MultiViews +Includes
        AllowOverride All
        Order allow,deny
        allow from all
  UseCanonicalName Off

  # splittable logs
  LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
  CustomLog /var/log/apache2/access_log vcommon
  VirtualDocumentRoot /home/magedev/public_html/%-2
  ErrorLog /var/log/apache2/error.log
  php_admin_value auto_prepend_file /usr/share/php/fixdocroot.php
  RewriteLogLevel 1
  RewriteLog /var/log/apache2/dynamic.rewrite.log

We now need to have apache read our new site configuration file and enable the necessary Apache modules.

ln -s /etc/apache2/sites-available/magedev /etc/apache2/sites-enabled/000-magedev
ln -s /etc/apache2/mods-available/vhost_alias.load /etc/apache2/mods-enabled/
ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/

We'll also make things easier for ourselves and have the apache server run as our magedev user:

nano vim /etc/apache2/envvars

Set both APACHE_RUN_USER and APACHE_RUN_GROUP to "magedev". Restart Apache:

/etc/init.d/apache2 restart

Now we have to fix the document root for PHP to behave normally.

nano /usr/share/php/fixdocroot.php

Place this in the file:


You should now be able to log out of root and back into the magedev user. You do that by just typing "exit". We'll now create our local apache base directory:

cd && mkdir public_html

Lets test our configuration:

mkdir public_html/test
nano public_html/test/index.php

File Contents:


We will now add a host name entry to our host machine's /etc/hosts file (this file is located elsewhere on Windows).

sudo vim /etc/hosts

Add the following line to the bottom: test.vm

Save and quit. Now in your browser go to http://test.vm/ you should see a PHP info output:

If you made it this far (you brave soul) and you see similar output, your initial setup is complete. In the next part we'll setup a sample Magento project. If you're ready for more, continue on to part 2.

Interactive Magento Console

I started working on a console script to help aide in testing certain Magento functionality without having to go through a browser. This will also avoid adding debug code into places like action methods, templates or index.php. In all its alpha/first round glory I present my simple "interactive Magento console" script.

It's meant to be ran at the base directory of a Magento project. It will include app/Mage.php and run Mage::app(), similar to what's done in the cron.php. All standard methods are available but some configuration may not be setup. For instance, if you have custom events that only trigger when the "frontend" area is loaded, it must be initiated manually by calling:

Mage::app()->loadAreaPart('frontend', 'events');

I've started testing the script with a block class I'm working on and noticed the theme and package name were not set up. Knowing which areas need to be setup prior to executing code will depend on the type of object and one's familiarity with Magento's internals.

Testing Custom Layout Block (output prefixed with #):

Mage::app()->loadAreaPart('frontend', 'events');
$l = Mage::app()->getLayout();
$b=$l->createBlock('magenation_catalog/product_view_spotlight', '', array('product_sku'=>'foo'));
echo Mage::getDesign()->getTemplateFilename($b->getTemplate(), array('_area'=>'frontend'));
echo $b->toHtml();

One nice benefit when testing a block's output is that it is possible to modify the template file without having to restart the console session. Altering the Block class would require the console to be restarted so the latest version of the class can be included. Fortunately readline methods which the script uses allow for history to be saved.

Get it: Interactive Magento Console on github

View Merged Magento Layout XML

Here's a quick way to view the Layout XML that is generated in magento, it can be added at the end of index.php.

It's a quick and easy way to verify custom layout changes have been merged in, an XML file will be saved in the specified path.