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!
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.
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
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:
Create a
magento-dev.local
file in the/etc/apache2/sites-available/
directory.To create and edit the file, run the following command:
sudo nano /etc/apache2/sites-available/magento-dev.local
Paste the following content in that file:
<VirtualHost *:80> # ServerName (domain) and admin email ServerAdmin webmaster@magento-dev.local 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 </VirtualHost>
Run the following commands in the terminal to create the
www
root folder: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/
To enable the site, run the following command:
sudo a2ensite magento-dev.local
To reload the Apache server, run the following command:
sudo service apache2 reload
To test the site, we have to add the following line in our host's file (
/etc/hosts
):127.0.0.1 magento-dev.local
This will point the domain
magento-dev.local
to the IP address 127.0.0.1. 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.
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.
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 http://www.wampserver.com.
For Mac OS, the alternative is MAMP. More information on MAMP is available at http://www.mamp.info.
A cross-platform web server is XAMP. You can download the installer for Linux, Windows, or Mac from their website at http://www.apachefriends.org/en/xampp.html.
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
You can download Magento and the sample data from Magento's website (http://www.magentocommerce.com/download). Download the latest Full release and the Sample data. The recipes of this book are based on the Magento Version 1.8.1.0. For the sample data, you can use the 1.6.1.0 Version.
The following steps show you how to install a clean Magento webshop with sample data:
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 thels -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 -rw-rw-r-- cron.sh 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
Tip
In Linux, hidden files or folders start wit a dot (.), such as the
.htaccess
file. The-a
option of thels
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.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.
To install the database, you have to run the following commands:
To create the database, run the following commands:
mysql -u root -p create database magento_dev; exit;
To import the SQL file, run the following commands:
mysql -u <<username>> -p magento_dev < "path_to_sample_data.sql"
Tip
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.
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:
Continue with the installation process by accepting the terms and conditions.
On the next screen, choose the correct language, locale, and currency for your store.
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.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.
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.
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.
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.
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.
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
The following steps show you how you can add the Magento code to the version control system Git:
We have to initialize the Git repository. To do this, navigate to the source folder and enter the following command:
git init
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.
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 thegit status
command, you will see a list of all the files.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
Run the following command to create the first commit:
git commit –m "add app folder"
Run the following command to track the file's status:
git status
Create a
.gitignore
file in the root, and add the following content in it:app/etc/local.xml
Run the
git status
command again, and you will see thatlocal.xml
is ignored.Commit the
.gitignore
file by running the following command:git commit .gitignore –m "add gitignore file"
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
cron.sh
favicon.ico
get.php
index.php
index.php.sample
install.php
LICENCE_AFL.txt
LICENCE.html
LICENCE.txt
mage
php.ini.sample
RELEASE_NOTES.txt
git commit –m "add additional files"
Ignore the other nonstatic Magento files and folders. Add the following content in the
.gitignore
file:/app/etc/local.xml /errors/local.xml /media/css/ /media/dhl /media/downloadable /media/import/ /media/js/ /media/catalog/ /media/customer /media/upload/ /media/wysiwyg/ /media/captcha/ /media/tmp/ /media/xmlconnect /var/import/ /var/export/ /var/cache/ /var/log/ /var/session/ /var/locks/ /var/package /var/report/ /var/resource_config.json sitemap.xml
Add the
media/
andvar/
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.
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.
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
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 http://netbeans.org and run the installer. Make sure that when you download it, you select the PHP version or the full version.
The following steps show you how to create a NetBeans project with the Magento files as the document root:
To create the project, open NetBeans and navigate to File | New Project.
In the dialog window, click on PHP Application with Existing Sources as shown in the following screenshot:
Click on Next and configure the following settings:
The following screenshot gives the sample values for these settings:
Tip
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.Click on Next.
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 MagentoIndex File: This field is set to
index.php
The following screenshot gives the sample values for these settings:
Click on Finish and your NetBeans project is ready.
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.
Behind the development environment in the IDE, a database client and a command-line tool are very useful when developing in Magento.
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.
To install phpMyAdmin, open your terminal and run the following command:
sudo apt-get install phpmyadmin
Follow the installer's instructions.
Edit the
/etc/apache2/apache.conf
file, and add the following code at the end of the file:Include /etc/phpmyadmin/apache.conf
Reload the Apache server with the following command:
sudo service apache2 reload
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.
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 https://github.com/classyllama/Wiz.
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 passwordwiz admin-createadmin
: This command creates a new administrative user from scratchwiz cache-clear
: This command clears all cache or specific cachewiz devel-showhints
: This command enables or disables frontend hintswiz module-list
: This command gives a list of all the installed modules