Magento 1.8 Development Cookbook

By Bart Delvaux , Nurul Ferdous
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Getting Started with Magento

About this book

Magento is an open source e-commerce platform which has all the functionality to function from small to large online stores. Its architecture makes it possible to extend the functionalities with plugins where a lot of them are shared by the community. This is the reason why the platform is liked by developers and retailers.

A practical developer guide packed with recipes that cover all the parts of Magento development. The recipes will start with the simple development exercises and get the more advanced as the book progresses. A good reference for every Magento developer!

This book starts with the basics. The first thing is to create a test environment. Next, the architecture, tools, files and other basics are described to make you ready for the real work.

The real work starts with the simple things like theming and catalog configuration. When you are familiar with this, we will move on to more complex features such as module and database development. When you have survived this, we will move on to the last part of making a shop ready for launch: performance optimization and testing. This book will guide you through all the development phases of Magento, covering the most common pitfalls through its recipes.

Publication date:
February 2014


Chapter 1. Getting Started with Magento

In this chapter, we will cover the following topics:

  • Preparing the development environment

  • Installing Magento with sample data

  • Adding version control to the source code

  • Configuring the development tools



You have probably heard of Magento. It is an out-of-the-box e-commerce platform with many features, such as catalog navigation, promotion rules, RSS feeds, product search, product tags, product reviews, tax rules, reports, and order management.

The company behind Magento is Varien. In 2007, they started thinking about an open source e-commerce platform that would be designed for a large number of uses. After one year of developing, the first stable release Magento 1.0 came out in 2008. When Magento became more popular in the later years, eBay showed interest in it and now owns 100 percent of Magento.

If you install Magento and configure it, then you can start selling products online. But when you want a custom theme, extra shipping methods, and other features, you have to extend the software as per the rules of Magento. If you know something about PHP programming, follow the steps described in the recipes, and you can start editing your Magento store.

We will create a development environment where you can start working. We will install sample data so that the shop is not empty and is ready to go. If you are new, you can start here. If you are a pro, you can still start here, because we follow the best practices of Magento development. Let's get started. Good luck!


Preparing the development environment

We will set up a development environment with Magento. For this, we need to set up a LAMP (Linux, Apache, MySQL, PHP) environment. In that LAMP environment, we will create a Fully Qualified Domain Name (FQDN) and a virtual host.

Getting ready

We have to set up a development server that we will use to run Magento. When we have a Ubuntu desktop environment, we have to install the latest versions of the following software:

  • Apache2

  • PHP

  • MySQL server

  • Extra PHP libraries

We can install these software by running the following commands on a CLI interface. These commands are based on a Ubuntu-based Linux distribution. To run the commands on a desktop with Ubuntu OS, launch the Terminal program:

  • To install the web server Apache2, run the following command:

    sudo apt-get install apache2
  • To install PHP, run the following command:

    sudo apt-get install php5
  • To install the MySQl server, run the following command:

    sudo apt-get install mysql-server
  • To install the required PHP extensions that Magento uses, such as the MySQL bridge, run the following command:

    sudo apt-get install php5-mysql php5-curl php5-gd php-pear

How to do it...

When everything is installed, we will create a virtual host with an FQDN. We want our development environment to be available at http://magento-dev.local/. To do this, we have to create a virtual host with this domain name. This domain points to the IP of our previously created web server.

The following steps describe how you can create a virtual host with an FQDN:

  1. Create a magento-dev.local file in the /etc/apache2/sites-available/ directory.

  2. To create and edit the file, run the following command:

    sudo nano /etc/apache2/sites-available/magento-dev.local
  3. Paste the following content in that file:

    <VirtualHost *:80>
     # ServerName (domain) and admin email
     ServerAdmin [email protected]
     ServerName magento-dev.local
     DocumentRoot /var/www/magento-dev.local/public # Folder of the site. We have to create this
     # Log file locations
     LogLevel warn
     ErrorLog /var/log/apache2/magento-dev.error.log
     CustomLog /var/log/apache2/magento-dev.access.log combined
  4. Run the following commands in the terminal to create the www root folder:

    1. To create the site folder (document root), run the following command:

      sudo mkdir /var/www/magento-dev.local/public
      sudo chown -R www-data:www-data /var/www/magento-dev.local/
    2. To enable the site, run the following command:

      sudo a2ensite magento-dev.local
    3. To reload the Apache server, run the following command:

      sudo service apache2 reload
    4. To test the site, we have to add the following line in our host's file (/etc/hosts): magento-dev.local

      This will point the domain magento-dev.local to the IP address This is the IP address of the local web server (localhost), so a request to this URL doesn't go to the Internet but goes to the local web server.

How it works...

This recipe describes how to install a web server from a CLI interface. If you already have a web server with a specific domain, you can skip this chapter.

The Magento files will be installed in the public directory. When a request is made to the domain, the www-data user will execute the request. So, it is best that all files and folders have the www-data user and group to avoid permission problems.

On a Linux server, every file and folder has three types of permissions. These permissions are read, write, and execute (rwx). You can set these permissions in three scopes: for owners, groups, and others. On every file request, Linux decides, based on the permissions, whether a user can read, write, or execute a certain file.

For an HTTP request, the www-data user will be used to execute a request. So, it is important to ensure that the www-data user has enough file permissions to run the application. To change file permissions, you can use the chmod command. To change the owner and group, you can use the chown command.

There's more…

It is also possible to run Magento and the web server on other operating systems. To run a web server, we need Apache, MySQL, and PHP. It is possible to install these software on a Windows or Mac device.

The variant for Windows operating systems is WAMP (Windows, Apache, MySQL, and PHP). You can find more information about this variant on the WAMP website at

For Mac OS, the alternative is MAMP. More information on MAMP is available at

A cross-platform web server is XAMP. You can download the installer for Linux, Windows, or Mac from their website at


Installing Magento with sample data

When you install Magento, you can start with an empty database or a database with some sample products and configurations. For our development environment, the best approach is to start with the sample data because we can start using Magento directly after installation.

For a new webshop, it is recommended to start with an empty database and do the configuration yourself. To install Magento, we need the following data:

  • The Magento code

  • The sample data

Getting ready

You can download Magento and the sample data from Magento's website ( Download the latest Full release and the Sample data. The recipes of this book are based on the Magento Version For the sample data, you can use the Version.

How to do it...

The following steps show you how to install a clean Magento webshop with sample data:

  1. Extract the code in the appropriate folder of our web server. In our case, this is /var/www/magento-dev.local/public. Take a look at the folder structure by running the ls -la command in the site's root folder. We have to take care that the hidden files are included in the folder:

    ls -la
    drwxrwxr-x .
    drwxrwxr-x ..
    -rwxrw-r-- .htaccess
    -rw-rw-r-- .htaccess.sample
    -rw-rw-r-- api.php
    drwxrwxr-x app
    -rw-rw-r-- cron.php
    drwxrwxr-x downloader
    drwxrwxr-x errors
    -rw-rw-r-- favicon.ico
    -rw-rw-r-- get.php
    -rw-rw-r-- .htaccess
    -rw-rw-r-- .htaccess.sample
    drwxrwxr-x includes
    -rw-rw-r-- index.php
    -rw-rw-r-- index.php.sample
    -rw-rw-r-- install.php
    drwxrwxr-x js
    drwxrwxr-x lib
    -rw-rw-r-- LICENSE_AFL.txt
    -rw-rw-r-- LICENSE.html
    -rw-rw-r-- LICENSE.txt
    -rw-rw-r-- mage
    drwxrwxr-x media
    -rw-rw-r-- php.ini.sample
    drwxrwxr-x pkginfo
    -rw-rw-r-- RELEASE_NOTES.txt
    drwxrwxr-x shell
    drwxrwxr-x skin
    drwxrwxr-x var


    In Linux, hidden files or folders start wit a dot (.), such as the .htaccess file. The -a option of the ls command that we used shows all the files and folders, including the hidden ones. It is important to see that the .htaccess file is in the directory because this file contains the configuration for URL rewrites and other server configurations.

  2. When you extract the sample data archive, you see a media folder and a SQL file. The SQL file contains the database, the media folder, and the images. To install the media folder, merge this folder with the site's root media folder.


    It is important to install the sample data before running the Magento install wizard. If Magento doesn't find the sample data in the database, the wizard continues with an empty database without the sample data.

  3. To install the database, you have to run the following commands:

    1. To create the database, run the following commands:

      mysql -u root -p
      create database magento_dev;
    2. To import the SQL file, run the following commands:

      mysql -u <<username>> -p magento_dev < "path_to_sample_data.sql"


    To avoid permission problems, ensure that all files and folders have the right permissions. For security reasons, it is recommended that all files have just enough permissions so that only the right users can access the right files. When you give all the rights (777), you don't have permission problems; but, every user on the server can read, write, and execute every file of your application.

  4. The next step is to run the Magento installation wizard. Go to the site's URL and the installer will be displayed as shown in the following screenshot:

  5. Continue with the installation process by accepting the terms and conditions.

  6. On the next screen, choose the correct language, locale, and currency for your store.

  7. On the Configuration page, fill the form with the right data:

    • Database Type: Enter MySQL in this field.

    • Host: Enter localhost in this field.

    • Database Name: Enter magento_dev (your Magento database name) in this field.

    • User Name: Enter root (your Magento database username) in this field.

    • User Password: Enter root (your database user password) in this field. It is possible to leave this empty.

    • Tables Prefix: Leave this field empty (if filled, all tables will have the prefix that is entered here).

    • Base URL: Enter http://magento-dev.local/ (the URL of your webshop) in this field.

    • Admin Path: Enter admin (the path to the backend) in this field.

    • Enable Charts: This check box must be checked.

    • Skip Base URL Validation Before the Next Step: This check box must be unchecked (if checked, the wizard will check for a valid URL when processing this form).

    • Use Web Server (Apache) Rewrites: This check box must be unchecked.

    • Use Secure URLs (SSL): This check box must be unchecked.

  8. Submit the form and continue to the next step. In this step, you can configure the admin account. Fill in the right data and remember the login and password, because this is required to manage the store. Leave the encryption key empty.

  9. After submitting this form, the installation wizard terminates. The setup is complete. You can now visit your store and enter the backend with the username that you have created in the installation wizard.

How it works...

We just created a fully functional Magento store. Technically, you can start selling products.

We have started with the sample data. The SQL file that we inserted in the database contains all the data for the sample webshop. When you want to create an empty shop, you can start with an empty database.

When installing a new shop, always follow the installer. The installer creates the app/etc/local.xml file and installs the values that you have set in the installer. These values are currency, timezone, backend user, and so on. The installer will show up when there is no local.xml file in the app/etc/ folder. In all other cases, the webshop will show up.

With a complete local.xml file in the app/etc folder, you can install Magento. However, this is not recommended because you don't have a backend user and the other configurations.


Adding version control to the source code

With version control, you can manage the changes that are made in the computer files. When you add this to a programming code, you can track all the changes you have made to the code from the time you initialized it. There are some very popular version control systems on the market, such as Subversion (SVN), Mercurial SCM, CVS, and Git SCM.

In this recipe, we will use Git SCM to add version control to our previously created Magento code. We will initialize an empty repository. After that, we will do several commits to add all the Magento code to the repository.

Getting ready

Go to your command line, and install Git on your local server by running the following command in the terminal:

sudo apt-get install git

How to do it...

The following steps show you how you can add the Magento code to the version control system Git:

  1. We have to initialize the Git repository. To do this, navigate to the source folder and enter the following command:

    git init
  2. Run the following command and you will see that all the files are marked as untracked:

    git status

    In the next steps, we will add the other folders to the repository.

  3. Add the app/ folder to the repository by running the following command:

    git add app/ 

    This will add all the files in the app/ directory. When you run the git status command, you will see a list of all the files.

  4. Run the following command to remove the local.xml file from the repository but not from the drive:

    git rm --cached app/etc/local.xml
  5. Run the following command to create the first commit:

    git commit –m "add app folder"
  6. Run the following command to track the file's status:

    git status
  7. Create a .gitignore file in the root, and add the following content in it:

  8. Run the git status command again, and you will see that local.xml is ignored.

  9. Commit the .gitignore file by running the following command:

    git commit .gitignore –m "add gitignore file"
  10. Add the other application files and folders to the repository by running the command git add <folder or filename>. Run this command for the following folders:

    • downloader (for installing plugins)

    • errors (configuration about error handling)

    • js (the JS library folder)

    • lib (contains PHP libraries)

    • pkginfo (information about upgrades)

    • shell/ (contains PHP CLI scripts)

    • skin/ (the CSS and images folder)

    • api.php

    • cron.php


    • favicon.ico

    • get.php

    • index.php

    • index.php.sample

    • install.php

    • LICENCE_AFL.txt

    • LICENCE.html

    • LICENCE.txt

    • mage

    • php.ini.sample


  11. Run a new git commit command:

    git commit –m "add additional files"
  12. Ignore the other nonstatic Magento files and folders. Add the following content in the .gitignore file:

  13. Add the media/ and var/ folders to the repository with the following command:

    git add media/.htaccess
    git add var/.htaccess

    We only need the .htaccess files, the other files are ignored because they are dynamic.

How it works...

When working with a version control system, you have to keep in mind that another person who clones the project can set up the environment with a database and the code in the Git. That environment has to be the same as the one you have committed.

It is very important that every Magento core file and your customized files are in the Git repository. You don't have to add configuration files, such as app/etc/local.xml and errors/local.xml, in version control. When running your code on another server, the settings in the configuration files are mostly different from the settings on the server.

Dynamically generated files such as cache files and user images are stored in the media and var folder, so we don't need the content of these folders.

The only important file in these folders is the .htaccess file, which has the configuration to restrict the var folder and the media folder.


Configuring the development tools

When you start customizing your Magento store, a good development environment with the following components saves time:

  • A code editor (IDE)

  • A MySQL client (phpMyAdmin or MySQL Workbench)

  • A command-line tool

Getting ready

NetBeans is an open source IDE that can be used for a lot of programming languages. The PHP support is well maintained and there are a lot of integrations with other systems (Git, SVN, and MySQL).

To install NetBeans, you have to download it from their site at and run the installer. Make sure that when you download it, you select the PHP version or the full version.

How to do it...

The following steps show you how to create a NetBeans project with the Magento files as the document root:

  1. To create the project, open NetBeans and navigate to File | New Project.

  2. In the dialog window, click on PHP Application with Existing Sources as shown in the following screenshot:

  3. Click on Next and configure the following settings:

    • Source Folder: This field is set to the location of your Magento code

    • Project Name: The NetBeans project name is entered in this field

    • PHP Version: This field is set to PHP 5.3 or higher

    • Default Encoding: This field is set to UTF-8

    The following screenshot gives the sample values for these settings:


    When you are working with a version control system (SVN, Git, or something else), it is recommended to check the option Put NetBeans metadata into a separate directory. If not checked, a .nbproject folder is created in your Magento root, which you will not have in your version control system. Another possibility is to add the .nbproject folder in the .gitignore file.

  4. Click on Next.

  5. Configure the following settings:

    • Run as: In our case, this field is set to a local web server

    • Project URL: In this field, enter http://magento-dev.local, the URL that we have configured for Magento

    • Index File: This field is set to index.php

    The following screenshot gives the sample values for these settings:

  6. Click on Finish and your NetBeans project is ready.

How it works...

When the project is started, you see the document root in the left-hand side column of the window. When you double click on a file, the file will open in the main window. In this window, you can view the file's history, format the code, refactor variable names, and a lot more.

There's more…

Behind the development environment in the IDE, a database client and a command-line tool are very useful when developing in Magento.

MySQL client phpMyAdmin

phpMyAdmin is a MySQL client that runs as a web application. It is an easy-to-use tool for direct SQL input in a database.

  1. To install phpMyAdmin, open your terminal and run the following command:

    sudo apt-get install phpmyadmin
  2. Follow the installer's instructions.

  3. Edit the /etc/apache2/apache.conf file, and add the following code at the end of the file:

    Include /etc/phpmyadmin/apache.conf
  4. Reload the Apache server with the following command:

    sudo service apache2 reload
  5. Go to http://magento-dev.local/phpmyadmin, and you will see the login screen. You can log in with the credentials of the database user for your Magento database.

Command-line tool Wiz

Magento has a lot of system tasks that you have to execute in your development process. All these actions could be triggered in the backend. However, because this is a time consuming job, a shell interface can save you a lot of time.

Wiz is a command-line tool that you can download from

Follow the installation instructions on their website. When installed, you have to change the directory of your shell to your Magento root. If you are in the Magento root, all the commands you run are executed for that Magento installation.

The following are some useful commands:

  • wiz admin-resetpass: This command can be used when you forget your admin password

  • wiz admin-createadmin: This command creates a new administrative user from scratch

  • wiz cache-clear: This command clears all cache or specific cache

  • wiz devel-showhints: This command enables or disables frontend hints

  • wiz module-list: This command gives a list of all the installed modules

  • wiz sql-cli: This command opens the MySQL command line

About the Authors

  • Bart Delvaux

    Bart Delvaux is an experienced web developer with several years of experience in the PHP world. He has worked with the most important frameworks in PHP, such as Drupal and Zend Framework, but Magento is his specialization.

    Bart has obtained all the Magento developer certifications: Front End Developer, Developer, as well as Developer Plus. He currently works for ISAAC Software Solutions, a company that specializes in software solutions such as web shops, apps, system integrations, and more.

    Bart finished a large variety of Magento projects in his Magento career that started in 2010 with the principle "quality above quantity". Having gone from handling a basic shop to shipping modules and large, complex Magento stores, Magento holds no secrets from him.

    Bart has also worked on Magento 1.8 Development Cookbook, Packt Publishing. Now that Magento 2 is out, it is time for the next one!

    Browse publications by this author
  • Nurul Ferdous

    Nurul Ferdous is an open source enthusiast and IT specialist from Bangladesh who is currently working for TM Secure Inc. as a LAMP consultant. In fact, he is a soldier turned programmer. He started his career with the Bangladesh Air Force. He has also served in RAB as an intelligence staff where he was nominated for the President's Police medal for his contribution to national security. He is a true passionate programmer. He started his run on software development back in 2004, while he was working in the Bangladesh Air Force. His primary skills are as a PHP developer. He is a Zend Certified PHP 5 Engineer, and contributes to a number of PHP projects, blogs on PHP-related topics, and presents talks and tutorials related to PHP development and the projects to which he contributes. He also contributes on open source community regularly. He is also a certified professional on TDD and Code Refactoring. He has served in some top notch software companies both at home and abroad, such as BIPL, Right Brain Solutions Ltd., TM Secure Inc., Inc., and so on, as a programmer, software engineer, and consultant. He also writes at his personal blog when he is not baking with codes.

    Browse publications by this author
Magento 1.8 Development Cookbook
Unlock this book and the full library for FREE
Start free trial