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.

Downloads

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.

Run:

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 192.168.56.1 netmask 0xffffff00 broadcast 192.168.56.255

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

Add the following to the file:

auto eth1
iface eth1 inet static
address 192.168.56.2
netmask 255.255.255.0

Note, if your local vboxnet interface IP address is different, e.g. 192.168.57.1 choose something like 192.168.57.2 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 192.168.56.2
PING 192.168.56.2 (192.168.56.2): 56 data bytes
64 bytes from 192.168.56.2: icmp_seq=0 ttl=64 time=0.550 ms
64 bytes from 192.168.56.2: icmp_seq=1 ttl=64 time=0.318 ms
64 bytes from 192.168.56.2: icmp_seq=2 ttl=64 time=0.415 ms
^C
--- 192.168.56.2 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@192.168.56.2

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

Apache:

apt-get install apache2.2-common

MySQL:

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"?

PHP:

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 http://192.168.56.2/ (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
uk.foo.vm -> will point to /home/magedev/public_html/uk.foo
bar.dev -> 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
  </Directory>
  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
</VirtualHost>

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:

<?php 
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'],"",$_SERVER['SCRIPT_FILENAME']); 

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:

<?php
phpinfo();

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:

192.168.56.2 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.



About