Magento 2 Development Cookbook - Bart Delvaux - E-Book

Magento 2 Development Cookbook E-Book

Bart Delvaux

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

With the challenges of growing an online business, Magento 2 is an open source e-commerce platform with innumerable functionalities that gives you the freedom to make on-the-fly decisions. It allows you to customize multiple levels of security permissions and enhance the look and feel of your website, and thus gives you a personalized experience in promoting your business.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 290

Veröffentlichungsjahr: 2015

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Magento 2 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Upgrading from Magento 1
Introduction
Creating a Magento 1 website with sample data
Getting ready
How to do it...
How it works…
Creating a Magento 2 website
Getting ready
How to do it...
How it works...
There's more...
Preparing an upgrade from Magento 1
Getting ready
How to do it...
How it works...
Upgrading the database
Getting ready
How to do it...
How it works...
There's more...
See also
Using an IDE
Getting ready
How to do it...
There's more...
Writing clean code with PHP MD and PHP CS
Getting ready
How to do it...
How it works...
There's more...
2. Working with Products
Introduction
Configuring the catalog defaults
Getting ready
How to do it
How it works
Working with attribute sets
Getting ready
How to do it
How it works
Working with product types
Getting ready
How to do it
How it works…
There's more…
A simple product
A configurable product
A bundle product
A grouped product
A virtual product
A downloadable product
Adding social media buttons
Getting ready
How to do it
How it works
Embedding an HTML object
Getting ready
How to do it
How it works
Changing the URL of a product page
Getting ready
How to do it
How it works
There's more
3. Theming
Introduction
Exploring the default Magento 2 themes
Getting ready
How to do it...
How it works...
Creating a Magento 2 theme
Getting ready
How to do it...
How it works…
There's more…
Customizing the HTML output
Getting ready
How to do it…
How it works…
Adding extra files to the theme
Getting ready
How to do it…
How it works…
There's more…
Working with LESS
Getting ready
How to do it…
How it works…
There's more…
Changing a page title
How to do it…
How it works…
Working with translations
Getting ready
How to do it…
How it works…
Adding widgets to the layout
Getting ready
How to do it…
How it works…
Customizing email templates
Getting ready
How to do it…
How it works…
4. Creating a Module
Introduction
Creating the module files
Getting ready
How to do it...
How it works...
Creating a controller
Getting ready
How to do it...
How it works...
There's more...
Adding layout updates
Getting ready
How to do it...
How it works...
Adding a translation file
Getting ready
How to do it...
How it works...
Adding a block of new products
Getting ready
How to do it...
How it works...
Adding an interceptor
Getting ready
How to do it...
How it works...
See also
Adding a console command
Getting ready
How to do it...
How it works...
See also...
5. Databases and Modules
Introduction
Creating an install and upgrade script
Getting ready
How to do it...
How it works...
Creating a flat table with models
Getting ready
How to do it...
How it works...
Working with Magento collections
Getting ready
How to do it...
How it works…
Programmatically adding product attributes
Getting ready
How to do it...
How it works...
Repairing the database
Getting ready
How to do it...
How it works...
6. Magento Backend
Introduction
Registering a backend controller
Getting ready
How to do it...
How it works...
Extending the menu
Getting ready
How to do it...
How it works...
Adding an ACL
Getting ready
How to do it...
How it works...
Adding configuration parameters
Getting ready
How to do it...
How it works...
Creating a grid of a database table
Getting ready
How to do it...
How it works...
Working with backend components
Getting ready
How to do it...
How it works...
Adding customer attributes
Getting ready
How to do it...
How it works...
Working with source models
Getting ready
How to do it...
How it works...
7. Event Handlers and Cronjobs
Introduction
Understanding event types
Getting ready
How to do it...
How it works...
See also
Creating your own event
Getting ready
How to do it...
How it works...
Adding an event observer
Getting ready
How to do it...
How it works...
Introducing cronjobs
Getting ready
How to do it...
How it works...
Creating and testing a new cronjob
Getting ready
How to do it...
How it works...
8. Creating a Shipping Module
Introduction
Initializing module configurations
Getting ready
How to do it...
How it works...
See also
Writing an adapter model
Getting ready
How to do it...
How it works...
Extending the shipping method features
Getting ready
How to do it...
How it works...
Adding the module in the frontend
Getting ready
How to do it...
How it works...
9. Creating a Product Slider Widget
Introduction
Creating an empty module
Getting ready
How to do it...
How it works...
Creating a widget configuration file
Getting ready
How to do it...
How it works...
Creating the block and template files
Getting ready
How to do it...
How it works...
Creating a custom configuration parameter
Getting ready
How to do it...
How it works...
There's more...
Finalizing the theming
Getting ready
How to do it...
How it works...
10. Performance Optimization
Introduction
Benchmarking a website
Getting ready
How to do it...
How it works...
Optimizing the frontend of the website
Getting ready
How it works...
How it works...
There's more...
Optimizing the database and MySQL configurations
Getting ready
How to do it...
How it works...
Optimizing the Apache web server
How to do it...
How it works...
Finding performance leaks in Magento
Getting ready
How to do it...
How it works...
Configuring OPcache, Redis, and Memcached
Getting ready
Zend OPcache
Memcached
Redis
How to do it...
How it works...
Optimizing the PHP configurations
Getting ready
How to do it...
How it works...
11. Debugging and Unit Testing
Introduction
Logging into Magento 2
Getting ready
How to do it...
How it works...
Getting started with Xdebug
Getting ready
How to do it...
How it works...
Running automated tests from Magento
Getting ready
How to do it...
How it works...
Creating a Magento test case
Getting ready
How to do it...
How it works...
There's more...
Index

Magento 2 Development Cookbook

Magento 2 Development Cookbook

Copyright © 2015 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: December 2015

Production reference: 1171215

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78588-219-7

www.packtpub.com

Credits

Authors

Bart Delvaux

Reviewers

Karen Kilroy

Pankaj Pareek

David Parloir

Marius Strajeru

Commissioning Editor

Veena Pagare

Acquisition Editor

Prachi Bisht

Content Development Editor

Aparna Mitra

Technical Editor

Abhishek R. Kotian

Copy Editor

Pranjali Chury

Project Coordinator

Izzat Contractor

Proofreader

Safis Editing

Indexer

Mariammal Chettiyar

Graphics

Disha Haria

Production Coordinator

Conidon Miranda

Cover Work

Conidon Miranda

About the Author

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!

I want to thank everyone who made it possible for me to complete this book. I would like to extend thanks to the people at Packt Publishing for the support and to my colleagues for their vision and support.

Lastly, I want to thank the people who contributed to Magento 2. They did a good job creating a new version of the popular Magento system, which is future-proof!

About the Reviewers

Karen Kilroy is a highly experienced developer, administrator, and instructor. She is a Magento-certified Front End Developer. As a hands-on developer and systems administrator with more than 25 years of experience in IT, which includes 20 years in web development, Karen has focused primarily on Magento for the past 7 years. Currently, she is employed at Amplifi as a Magento technical lead and works on several well-known commerce sites.

Karen got her start in Magento at a direct marketing company selling EdenPURE Heaters (edenpure.com), a site that generates millions of dollars in sales. Additionally, she was a courseware author and instructor for Magento's official training arm, Magento U, between 2010 and 2014. Karen is also a reviewer of Mastering Magento, 2nd Edition, Packt Publishing.

Prior to becoming involved with Magento, she customized LAMP content management systems, such as Joomla, Drupal, and WordPress. In the early days of web development, Karen led her own company, where she employed 20 developers doing Java and Lotus Notes/Domino work for large clients.

In her spare time, she is also a professional dragon boat coach and steersperson.

Pankaj Pareek is a certified software professional who has expertise in Magento, PHP, and other frameworks. He has provided his professional services in this field for more than 7 years.

A true professional, Pankaj works with the motto that knowledge increases when you share it with others. He is a person who has explored different aspects of the software field suo moto. Pankaj is a quick, curious learner who received various recognized certifications in the IT field in a very short span of time, namely Magento Developer (2013), Magento Solution Specialist (2015), and Zend Certified Engineer (2014).

I would like to express my gratitude toward my loving grandmother, family, colleagues, and the almighty god.

David Parloir has been a freelance Magento developer since the first version was released in 2008, and through this, he has also been the lead developer for several large global projects. Prior to this, David worked for several companies that focused on the development of e-commerce websites and even worked as a teacher of Magento for a short period. He is a self-taught developer who sees web development as more than a job—he sees it as a passion. David considers himself a craftsman, keeping up to date with the latest trends in this area while balancing the new skills he develops, with a desire for his code to be efficient, simple, and elegant.

Marius Strajeru is 32 years old and finished as a faculty of computer science in Iasi, Romania, in 2006. Since then, he has worked as a PHP developer for various software companies.

Marius' area of expertise is Magento; he has been working with Magento since version 1.0 came out in 2008. He started looking at Magento 2 as soon as he heard that the source code is available in a dev version.

www.PacktPub.com

Support files, eBooks, discount offers, and more

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Free access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

Preface

Magento is one of the most popular e-commerce platforms on the market. It contains a lot of e-commerce functionality, it is stable, and it is free. This means that a lot of people choose Magento for their online business.

The first stable version of Magento was released in 2008. The later releases were based on the first version of Magento. Technology changes quickly and Magento needed a big update—a big release Magento 2 is now ready.

Developing in Magento is not as easy as you would expect. Even if you have knowledge of Magento 1, a good guide with practical examples that shows you the best practice is a must have, and this is exactly what this book will do.

With Magento 2 Development Cookbook, we will cover the most important topics that will help you become a good Magento 2 developer. We will start with the basics and we will end with the more advanced topics.

This book is divided into several recipes, which show you which steps to take to complete a specific action. In each recipe, we have a section that explains how everything works.

We will start this book with the creation of a good development environment. For a good development environment, we need the right tools. We will install Magento and we will discuss how we can migrate data from a Magento 1 to a Magento 2 shop. Next, we will see some functional stuff. You will learn how the catalog system works, which product types are available, and a lot more.

After this, you will learn how we can create a Magento theme to change the look and feel of the Magento shop. But the main focus of this book will be the development part. We will create a custom module that we will extend with a lot of common features that are used in Magento projects, such as extra controller pages, database integrations, custom shipping methods, and extra backend interfaces.

At the end of this book, we will see how we can improve the performance of a Magento shop. Finally, we will see some debugging techniques, such as Xdebug and creating unit tests using the Magento test framework.

What this book covers

Chapter 1, Upgrading from Magento 1, provides an introduction to how you can install and migrate the data from a Magento 1 to a Magento 2 shop. We will also prepare our development environment in this chapter.

Chapter 2, Working with Products, gives you a more functional information about the possibilities of displaying products in your Magento shop.

Chapter 3, Theming, explains how you can customize the look and feel of your webshop using a custom Magento theme.

Chapter 4, Creating a Module, describes how to create a basic Magento module; how to extend that module with custom configurations, such as a custom page, translations, and blocks; and how to change behavior of standard Magento classes.

Chapter 5, Databases and Modules, demonstrates how you can extend a Magento module with database interactions, such as install and upgrade scripts, a custom entity that represents a database table.

Chapter 6, Magento Backend, shows you how to integrate a Magento module with the backend, such as adding configuration pages, creating overview pages, and extending the admin menu.

Chapter 7, Event Handlers and Cronjobs, describes how the event-driven architecture is implemented in Magento and how to integrate this in your module. Later in this chapter, you will learn how to create cronjobs and how to test them.

Chapter 8, Creating a Shipping Module, shows you how to create a module with the configurations that are required for a new shipping method.

Chapter 9, Creating a Product Slider Widget, will cover how to create a module with a custom widget, how to build the backend interface, and how to provide a good UI in the frontend of that widget.

Chapter 10, Performance Optimization, describes how to benchmark a site to explore the limits and how to improve the performance using different techniques such as Redis and Memcached.

Chapter 11, Debugging and Unit Testing, shows you how to use the PHP debugger Xdebug and how we can create automated tests using the Magento 2 testing framework.

What you need for this book

Magento 2 source codeA virtual Linux server (Ubuntu 15.10 or higher)On that virtual server, you need the following:
Apache 2.4PHP 5.5 or higherMySQL Server 5.6 or higherSSH access
NetBeans IDE (or any other good PHP editor like PhpStorm)A database client (such a phpMyAdmin)A standard web browserXdebugGit SCM

Who this book is for

This book is for web programmers who are familiar with PHP and want to start with Magento 2. This book is also for Magento 1 developers who want to know how everything works in Magento 2.

This book will start with the basics of Magento 2 development and will end with the more advanced topics. Even if you knowledge about Magento development, this book is a good reference if you want to more about a particular topic in Magento.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The widget.xml file is used to define widgets in the Magento installation."

A block of code is set as follows:

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route id="helloworld" frontName="helloworld"> <module name="Packt_HelloWorld" /> </route> </router> </config>

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/1234OT_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Upgrading from Magento 1

In this chapter, we will cover:

Creating a Magento 1 website with sample dataCreating a Magento 2 websitePreparing an upgrade from Magento 1Upgrading the databaseUsing an IDEWriting clean code with PHP MD and PHP CS

Introduction

Magento is one of the most complete e-commerce platforms on the open source market. With a default Magento installation, all the common e-commerce features, such as catalog navigation, promotion rules, tax settings, online payments, and so on are available.

The first version of Magento was released in 2008 after one year of development. Magento was initially designed as an e-commerce system that could be used for a wide range of uses. In later years, Magento became very popular as an out-of-the-box e-commerce system and a lot of minor versions of the 1.x series have been released in the last few years.

To be future proof, Magento started the development of a major upgrade of the system, also known as Magento 2. Magento 2 is a big improvement on every part of Magento. Every aspect is analyzed and rewritten with up-to-date technologies to be ready for the future. Everything, including the developer experience, maintainability, performance, and technologies will be improved.

In this chapter, we will upgrade the data of a Magento 1 installation to a Magento 2 installation. We will also prepare some tools that we can use in the following chapters of this book.

Creating a Magento 1 website with sample data

To start a Magento 2 upgrade, we need a Magento 1 webshop with some data. In this recipe, we will install the latest Magento version, 1.9, with the sample data for the new responsive theme.

Getting ready

To install a Magento 1 website, we need the following stuff:

A web server (Linux, Apache2, PHP, or MySQL)The Magento 1.9 codebaseThe Magento 1.9 sample data

Note

The Magento 1.9 codebase and sample data can be downloaded from the Magento site at http://www.magentocommerce.com/download.

The following stuff is recommended for the installation:

Command-line accessA virtual host (domain name) that is going to be your web root

Note

We recommend that you use a test server that is on your development machine. If you use a Linux or a Mac operating system, you can install the webserver on your local machine. If you have a Windows machine, you can use a virtual Linux server for your development.

How to do it...

Extract the Magento code archive in your webroot (the directory of the virtualhost). An ls -la command should give you the following output:
api.phpappcron.phpcron.shdownloadererrorsfavicon.icoget.phpincludesindex.phpindex.php.sampleinstall.phpjslibLICENSE_AFL.txtLICENSE.htmlLICENSE.txtmagemediaphp.ini.samplepkginfoRELEASE_NOTES.txtshellskinvar
Extract the sample data archive to a different folder from the webroot. Copy the contents of the media and skin folders to the media and skin folders in your webroot. We can do this by using the following cp command:
cp –R <path_to_sampledata_folder>/media/* <path_to_magento_folder>/media/cp –R <path_to_sampledata_folder/skin/* <path_to_magento_folder>/skin/
Create a database for the Magento 1 installation and name it magento1. We can do this by running the following commands:
mysql -u <username> -pcreate database magento1;exit;
Import the sql file that is in the sample data directory. This file contains a database that we will import into the magento1 database. We can do this by running the following command:
mysql -u <username> -p magento1< "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 because each user can read, write and, execute each file of your application. More information about file permissions can be found at http://devdocs.magento.com/guides/m1x/install/installer-privileges_after.html.

When the files are in the right place and the database is imported, we can run the Magento installer. Open your browser and go to the domain that is configured for your website. You should see the installer as 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 in the form with the right data:
Database Type: MySQL.Host: Enter the hostname or IP address of your database server (localhost if it is on the same machine).Database name: Enter magento1 in this field (or another name if you have a different name for your database).User name: Enter your database username.User password: Enter your database password.Tables prefix: Leave this field empty (the string in this field will be used to prefix all tables of your database).Base URL: Enter the URL of your website in this field.Admin path: Enter admin in this field. This will be the path of the backend.Enable charts: For development, it is recommended that this be unchecked.Skip Base URL Validation Before the Next Step: When checked, the wizard will check for a valid URL when processing this form.Use Web Server (Apache) rewrites: Check this when the apache module mod_rewrite is enabled.Use Secure URL's (SSL): This checkbox must be unchecked if you don't use HTTPS.
Submit this form and we will be forwarded to the next step. In this step, you can configure the administrator account. Fill in the right data and remember the username and password because this is required to manage the store. Leave the encryption key field empty.After submitting this form, the installation is complete. Optionally, you can submit the Magento survey. At the bottom of the page, there are buttons to navigate to the frontend and backend. When going to the frontend, you can see a demo shop with sample data as in the following screenshot:The layout is responsive. When scaling your browser to a smaller width, the website will switch to the mobile layout like in the following screenshot:

How it works…

We have just created a fully functional Magento 1 store. The webshop is fully configured and filled with data about products, customers, and orders, just the data we need to migrate to Magento 2 (in the upcoming recipes).

When installing a new shop, you have to follow the installer. This interface creates a configuration file app/etc/local.xml. If the file doesn't exist, Magento will launch the installer wizard. If the file is there, Magento will run the shop.

With a valid local.xml file, it is technically possible to install a new Magento shop, but this is not recommended because some settings such as a backend user, time zone, and currency are not set. These are actions that you have to do manually when choosing for this method.

Creating a Magento 2 website

In the previous recipe, we created a Magento 1 website with sample data that we will use for an upgrade. In this recipe, we will do the same, but we will create a Magento 2 website with the sample data for Magento 2.

Getting ready

To install Magento 2, we need the newest tools to run that application. Make sure your webserver has the following stuff installed:

PHP 5.5 or higherMySQL 5.6 or higherApache 2.2 or higherCommand line accessComposer

We can install Magento 2 in different ways. In this recipe, we will install Magento 2 using Composer. The advantage of this is that we can use GIT to add version control to our custom development.

How to do it...

We will install Magento 2 with Composer. For this, we need authentication keys. With an account on the magento.com site, go to Developers | Secure keys in the My Account section. On this page, you can generate public and private keys that will be your username and password in the next step.To install Magento 2 with composer, we have to run the following command:
composer create-project --repository-url=https://repo.magento.com magento/project-community-edition <installation_dir>
You will be prompted for a username and password. The username is the public key and the password is the private key that we generated in the previous step. When the command has run, the installation directory will have the following structure:
appbinCHANGELOG.mdcomposer.jsoncomposer.lockCONTRIBUTING.mdCONTRIBUTOR_LICENSE_AGREEMENT.htmlCOPYING.txtdev.gitignoreGruntfile.js.htaccess.htaccess.sampleindex.phplibLICENSE_AFL.txtLICENSE.txtnginx.conf.samplepackage.json.php_csphp.ini.samplepubREADME.mdsetup.travis.ymlupdatevarvendor

Tip

Check that the user and group of these files are the same as your Apache user. One recommendation is to execute all the commands as your apache user.

We have installed the codebase with composer. Now we can run the installation wizard. Open your browser and enter the URL of your site. You should see the following welcome screen:Hit the Agree and Setup Magento button and start the environment check.Click on Next and enter your database information as follows:
Database Server Host: The hostname or IP address of the database serverDatabase Server Username: The username of the database accountDatabase Server Password: The password for the accountDatabase Name: The name of the databaseTable Prefix: Optionally, you can give a prefix for each table
Go to the next step and check if the right information is filled for the URL part. In the advanced section, you can optionally configure HTTPS, apache rewrites, and your encryption key. For our test environment, we can leave these settings as they are configured.

Note

Make sure that the mod_rewrite option is enabled for the apache server. When not enabled, the URL rewrites will not work correctly.

In the next step, you can configure your time zone, currency, and default language.In the last step, you can configure your administration account. After clicking on the Next button, you are ready to install. Click on the Install Now button and the installer will start. This will take some time because the installer will add the sample data during the installation. You can open the Console Log to see what is currently happening.When the installer is ready, you will see the following success message:Run the following commands in your Magento installation directory to configure the sample data:
php bin/magento sampledata:deploycomposer updatephp bin/magento setup:upgrade
The preceding commands will download and install the sample data packages. Because they contain a lot of images, this could take some time. The setup:upgrade command will install the sample data, and this also takes some time.The installation of the webshop is now complete. You now have an up-and-running Magento 2 webshop. When you navigate to the category Gear | Bags, you should see something like in the following screenshot:

How it works...

We have now installed a Magento 2 website. Like we did in the previous recipe for Magento 1.9, we downloaded the codebase (using composer), created a database, and installed Magento.

For Magento 2, we used composer to download the codebase. Composer is a PHP dependency manager. All the dependencies are set in the composer.json file. For this recipe, there are the Magento and the magento-sample-data dependencies in the composer.json file. There is also a composer.lock file generated. In that file, the versions of the installed dependencies are stored.

Note