Magento 2 Cookbook - Ray Bogman - E-Book

Magento 2 Cookbook E-Book

Ray Bogman

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

Over 50 practical recipes that will help you realize the full potential of Magento in order to build a professional online store

About This Book

  • Take advantage of the latest features in Magento 2 to set up an e-commerce store that fits your business needs
  • Packed with several advanced recipes, not just to manage your online store, but to extend and design it as well
  • Written in a cookbook style, you can pick and choose your recipe to carry out your day- to- day Magento store tasks

Who This Book Is For

The book is for existing Magento users who want to gain further expertise and insights into managing, designing, and extending their online store in Magento to fit their business needs. Working knowledge of Magento and basic familiarity with programming is expected.

What You Will Learn

  • Set up a Magento 2 project on Apache or Nginx.
  • Transfer your Magento 1 database to Magento 2 using the Magento 2 system tools.
  • Boost the performance of Magento 2 by enabling different types of caching.
  • Build a Magento 2 multi-store by creating a root catalog, subdirectories, and products.
  • Create and manage pages, blocks, and front-end apps.
  • Manage your Magento store by setting up the correct TAX rules.
  • Design custom themes within the Magento 2 framework.
  • Create basic and advanced extensions using Magento 2.

In Detail

Magento 2 is an open source e-commerce platform that has all the functionality to function from small to large online stores. It is preferred by developers and merchants due to its new architecture, which makes it possible to extend the functionalities with plugins, a lot of which are now created by the community.

This merchant and developer guide is packed with recipes that cover all aspects of Magento 2. The recipes start with simple how-to's then delve into more advanced topics as the book progresses.

We start with the basics of setting up a Magento 2 project on Apache or Nginx. Next, you will learn about basics including system tools and caching to get your Magento 2 system ready for the real work. We move on to simple tasks such as managing your store and catalog configuration. When you are familiar with this, we cover more complex features such as module and extension development. Then we will jump to the final part: advanced Magento 2 extensions.

By the end of this book, you'll be competent with all the development phases of Magento 2 and its most common elements.

Style and approach

Step by step guide for real world tasks for Magento users to gain a more advanced insight on managing, extending and designing their e-commerce store to fit their business needs.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 296

Veröffentlichungsjahr: 2016

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 Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why Subscribe?
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. Installing Magento 2 on Apache and NGINX
Introduction
Installing Apache
Getting ready
How to do it...
How it works…
There's more…
Installing NGINX
Getting ready
How to do it...
How it works…
There's more…
Installing PHP-FPM
Getting ready
How to do it...
How it works…
There's more…
Installing HHVM
Getting ready
How to do it...
How it works…
There's more…
Installing MySQL
Getting ready
How to do it...
How it works…
There's more…
Installing Magento 2
Getting ready
How to do it...
How it works…
There's more…
Installing Magento 2 on Hypernode
Getting ready
How to do it...
How it works…
There's more…
Managing Magento 2 on Docker
Getting ready
How to do it…
How it works…
There's more…
2. Magento 2 System Tools
Introduction
Installing Magento 2 sample data via GUI
Getting ready
How to do it...
How it works…
There's more…
Installing Magento 2 sample data via the command line
Getting ready
How to do it...
How it works…
There's more…
Managing Magento 2 indexes via the command line
Getting ready
How to do it...
How it works…
There's more…
Managing Magento 2 cache via the command line
Getting ready
How to do it...
How it works…
There's more…
Managing Magento 2 backup via the command line
Getting ready
How to do it...
How it works…
There's more…
Managing Magento 2 set mode (MAGE_MODE)
Getting ready
How to do it...
How it works…
There's more…
Transferring your Magento 1 database to Magento 2
Getting ready
How to do it...
How it works…
There's more…
3. Enabling Performance in Magento 2
Introduction
Configuring Redis for backend cache
Getting ready
How to do it…
How it works…
There's more…
Configuring Memcached for session caching
Getting ready
How to do it…
How it works…
There's more…
Configuring Varnish as the Full Page Cache
Getting ready
How to do it…
How it works…
There's more…
Configuring Magento 2 with CloudFlare
Getting ready
How to do it…
How it works…
There's more…
Configuring optimized images in Magento 2
Getting ready
How to do it…
How it works…
There's more…
Configuring Magento 2 with HTTP/2
Getting ready
How to do it…
How it works…
There's more…
Configuring Magento 2 performance testing
Getting ready
How to do it…
How it works…
There's more…
4. Creating Catalogs and Categories
Introduction
Create a Root Catalog
Getting ready
How to do it...
How it works…
There's more…
Create subcategories
Getting ready
How to do it...
How it works…
There's more…
Manage attribute sets
Getting ready
How to do it...
How it works…
There's more…
Create products
Getting ready
How to do it...
How it works…
There's more…
Manage products in a catalog grid
Getting ready
How to do it...
How it works…
There's more…
5. Managing Your Store
Introduction
Creating shipping and tax rules
Getting ready
How to do it…
How it works…
There's more…
Managing customer groups
Getting ready
How to do it…
How it works…
There's more…
Configuring inventories
Getting ready
How to do it…
How it works…
There's more…
Configuring currency rates
Getting ready
How to do it…
How it works…
There's more…
Managing advanced pricing
Getting ready
How to do it…
How it works…
6. Creating a Magento 2 Theme
Introduction
Creating a new theme
Getting ready
How to do it…
How it works…
Adding theme variants
Layout files
Template files
Magento UI library
Changing a layout XML of a Magento 2 module
Getting ready
How to do it…
How it works…
<container>
referenceContainer/referenceBlock
move
remove
update
Overriding template files
Adding CSS/JS to pages
Getting ready
How to do it…
How it works…
External files
JavaScript files
CSS files
Removing a file in the header
Using Grunt for CSS changes
Getting ready
How to do it…
How it works…
LiveReload
Adding static blocks to pages through layout XML
Getting ready
How to do it…
How it works…
Adding a block to a single page
Adding static blocks to pages through widgets
How to do it…
How it works…
Available widgets
CMS Page Link
CMS Static Block
Catalog Category Link
Catalog New Products List
Catalog Product Link
Catalog Product List
Orders and Returns
Recently Compared Products
Recently Viewed Products
Using a dynamic serving theme based on the client browser
Getting ready
How to do it…
How it works…
Creating theme-specific translations
Getting ready
How to do it…
How it works…
Generating a translation file
7. Creating Magento 2 Extensions – the Basics
Introduction
Initializing extension basics
Getting ready
How to do it…
How it works…
There's more…
Working with database models
Getting ready
How to do it…
How it works…
Creating tables using setup scripts
Getting ready
How to do it…
How it works…
There's more…
Data installation
Creating a web route and controller to display data
How to do it…
How it works…
Creating system configuration fields
Getting ready
How to do it…
How it works…
Creating a new tab
Creating a new section
Creating a new group
Creating a new field
There's more…
Creating a backend data grid
How to do it…
How it works…
The uiComponent configuration
Data source
Mass actions for grid
See also
Creating a backend form to add/edit data
Getting ready
How to do it…
How it works…
Building the form
Adding a form field
Loading the data
Saving the data
8. Creating Magento 2 Extensions – Advanced
Introduction
Using dependency injection to pass classes to your own class
Getting ready
How to do it…
How it works…
Modifying functions with the use of plugins – Interception
Getting ready
How to do it…
How it works…
Creating your own XML module configuration file
Getting ready
How to do it…
How it works…
Creating your own product type
Getting ready
How to do it…
How it works…
Working with service layers/contracts
How to do it…
How it works…
DemoRepositoryInterface
DemoInterface
See also
Creating a Magento CLI command option
Getting ready
How to do it…
How it works…
Index

Magento 2 Cookbook

Magento 2 Cookbook

Copyright © 2016 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 authors, 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: March 2016

Production reference: 1210316

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78588-706-2

www.packtpub.com

Credits

Authors

Ray Bogman

Vladimir Kerkhoff

Reviewer

Kevin Schroeder

Commissioning Editor

Wilson D'souza

Acquisition Editor

Reshma Raman

Content Development Editor

Parshva Sheth

Technical Editors

Pranil Pathare

Manthan Raja

Copy Editor

Tasneem Fatehi

Project Coordinator

Nikhil Nair

Proofreader

Safis Editing

Indexer

Monica Ajmera Mehta

Graphics

Disha Haria

Production Coordinator

Arvindkumar Gupta

Cover Work

Arvindkumar Gupta

Foreword

Ray and Vladimir have been THE BEST thing to happen to Magento ever since X.Commerce.

Ok, that may be a slight exaggeration..., but it remains true that besides the software itself, Magento has always thrived on the dedication and enthusiasm of its large community. Ray and Vladimir are prime examples of the literally hundreds of developers, merchants, project managers, extension providers, event organizers, and platform evangelists that contribute every day to help community members make the most of their Magento shop.

I met Ray and Vladimir in the early days of Magento and got to know them as two highly engaged Magento fanatics. They built shops, extended the functionality, and pushed Magento to its limits. Failed and succeeded more often than anyone can count. More importantly, they were always generous enough to share their learning with the community through blog posts, webinars, events, and as Magento Doctors!

After a not-to-be-specified number of days, the announcement—we finally have Magento 2! If there are two people in our community capable of taking a deep dive into this new amazing platform and giving us practical recipes, it will be Ray and Vladimir.

I look forward to seeing what we can make happen in this new chapter for Magento. A lot of things will change and this book will help you get started with over 50 recipes.

One thing, however, will never change: don't touch the core...

Guido Jansen

Magento Community Manager

About the Authors

Ray Bogman is an IT professional and Magento evangelist from the Netherlands. He started working with computers in 1983 as a hobby at first. In the past, he has worked for KPN, a large Dutch Telecom company, as a senior security officer.

He has been the CTO of Wild Hibiscus, Netherlands, since 2010, and cofounder and business creator of Yireo until 2011, Jira ICT since 2005, and CTO of SupportDesk B.V., which he cofounded in 2011.

At SupportDesk B.V., he is a Magento, Joomla, OroCRM, web / server / mobile performance specialist, and security evangelist. His focus during the day is business/product development and training webmasters and consultants about the power of Magento, from the basics up to an advanced level. He has trained over 1,000 Magento and 750 Joomla experts worldwide since 2005.

At Magento events such as Magento Developers Paradise, Meet Magento, and MagentoLive, he has been a regular speaker since 2009.

He has participated in reviewing Mastering Magento (2012), Mastering Magento Theme Design (2014), Magento Administration Guide (2014), Learning Magento Theme Development (2014), and the video, Mastering Magento (2013), all by Packt Publishing.

The writing of this book was a big thing for me. I spent every free minute with passion in writing this. I had many doubts to overcome this big challenge as a dyslexic person. But I never thought that writing this would give me lots of self-esteem, peace, and joy. This would not have happened without the loving support and patience of my wife, Mette, and daughter, Belize. Joining forces with my co-writer, Vladimir, was a lot of fun. Sharing thoughts and insights about our Magento passion was great.

Last but not least, I would like to thank all the people who made writing this possible.

Vladimir Kerkhoff is an experienced IT professional, 4-times Magento Certified Developer from the Netherlands, and working with Magento since late 2009.

Currently, Vladimir is the founder/owner of Genmato BV, a Magento extension development company. He is also available for cool freelance projects. Vladimir has had a long career in the IT industry. He started his career in 1992 in an IT company, managing customer corporate networks (Bionet, Novell Netware, and Windows Server). In 1996, he was the cofounder and CTO of Perfect InterNetworking Solutions (PINS), a large Dutch-managed hosting provider. After leaving PINS in 2009, he cofounded eFulFillers offering web shop fulfilment services based on the Magento platform.

The writing of this book was a nice new experience, forcing myself to dive into the new Magento 2 code and learn the new methods that are available.

About the Reviewer

Kevin Schroeder has more years of experience in software development and consulting than he would like to admit. He was worked at several notable companies, including Zend as a consultant and evangelist and Magento as a consultant with the Expert Consulting Group. After getting his first taste in browser testing, he realized that the current tooling amounted to cruel and unusual punishment and founded the Magium open source project under his company, 10n Software, with the purpose of making automated browser testing almost enjoyable and actually useful beyond just browser testing.

When he's not working on software, he is sleeping. When he's not sleeping, he is recording music (Coronal Loop Safari and Loudness Wars) or writing books (The IBMi Programmer's Guide to PHP, You want to do WHAT with PHP?, and Advanced Guide to PHP on the IBMi, all published by MC Press).

I would like to thank my wife, Laurie, and my three young 'uns who, after having me in their family, will have no problems coping with the rigors of adult life. Supreme thanks to life's Author, about whom many books have been written and none have been sufficient.

www.PacktPub.com

eBooks, discount offers, and more

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

Preface

Magento is one of the most successful open source e-commerce platforms out there today. It is powerful enough to build small or enterprise businesses.

The first stable version of Magento was released in 2008. Since the beginning, lots of merchants have chosen Magento to be their platform with great success.

The current global ecosystem of Magento includes 240+ merchants, 300+ partners, and 4500+ certified developers, and this number is growing on a daily basis.

Over the last few years, e-commerce has changed a lot, so Magento announced a new platform that will support this. This new era of commerce is called Magento 2.

Magento 2 offers enhanced performance and scalability. The new platform is built from scratch to enable lots of new features. It supports both B2C and B2B businesses, and will serve the best omnichannel shopping experiences to their shoppers.

This book will provide you with the necessary insights to get a better understanding on what is needed to build such a powerful commerce platform.

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

It will cover installing a Magento 2 website, configuring your categories and products, performance tuning, creating a theme, developing a module, and much more.

At the end of this book, you will gain the knowledge to start building a success website.

What this book covers

Chapter 1, Installing Magento 2 on Apache and NGINX, is a totally different ballgame compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the command-line interface for an experienced webmaster.

Chapter 2, Magento 2 System Tools, explains how to install Magento 2 via the command shell. Magento released a new powerful tool to manage and install sample data, reindex your database, back up your site, or flush your caches, which are just a few of the options.

Chapter 3, Enabling Performance in Magento 2, explains how to configure different types of caching options. In Magento 2, the Full Page Cache (FPC) can be handled by Varnish to give your store a performance boost. There are also external services that you can use as a cache.

Chapter 4, Creating Catalogs and Categories, shows you one of the major elements of a Magento store before creating products. Creating the correct product type including attributes is an important step in setting up a Magento store.

Chapter 5, Managing Your Store, covers setting up the correct tax rules, configuring an inventory, and creating customer groups.

Chapter 6, Creating a Magento 2 Theme, discusses the Magento 2 blank theme and how to use the fallback to create seasonal variations. It also explains how the new theme is set up and where files are stored.

Chapter 7, Creating Magento 2 Extensions – the Basics, contains the basic functions required to use extensions in a Magento 2 installation. It contains a brief introduction to new methods introduced in the Magento 2 framework and examples on how to create basic functions.

Chapter 8, Creating Magento 2 Extensions – Advanced, explains how to use advanced features in extensions for Magento 2. It also includes how to add unit/functional tests as this is a new requirement for extensions listed on the new Magento Connect.

What you need for this book

The following setup is recommended for maximum enjoyment:

Magento 2 source codeA virtual Linux server (Ubuntu 15.10 or higher; DigitalOcean)Hypernode (https://hypernode.com/)CloudFlare (https://www.cloudflare.com/)IDE (PhpStorm, NetBeans, and Sublime)A Git account

Who this book is for

This book is intended primarily for intermediate to professional merchants, webmasters, DevOps, solution integrators, and PHP developers who are interested in Magento 2.

It will cover the basic and advanced features of Magento 2 DevOps, performance, theming, development, and system configuration.

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: "Go to your favorite browser and search using your yourdomain.com."

A block of code is set as follows:

/usr/local/bin/php -f install.php -- \ --license_agreement_accepted "yes" \ --locale "en_US" \ --timezone "America/Los_Angeles" \ --default_currency "USD" \ --db_host "mysql.example.com" \

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

docker run --rm --name magento2 -it -p 80:80 --link mysql:mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=admin -e PUBLIC_HOST=yourdomain.com raybogman/mage2cookbook-docker $*

Any command-line input or output is written as follows:

service apache2 statusnetstat –anp | grep apache2

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: "Check on the second like for the Server API; this should be FPM/FastCGI."

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 for this book from your account at http://www.packtpub.com. 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.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.Hover the mouse pointer on the SUPPORT tab at the top.Click on Code Downloads & Errata.Enter the name of the book in the Search box.Select the book for which you're looking to download the code files.Choose from the drop-down menu where you purchased this book from.Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for WindowsZipeg / iZip / UnRarX for Mac7-Zip / PeaZip for Linux

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 https://www.packtpub.com/sites/default/files/downloads/Magento2Cookbook_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. Installing Magento 2 on Apache and NGINX

In this chapter, we will cover the basic tasks related to installing Magento 2 on Apache and NGINX. You will learn the following recipes:

Installing ApacheInstalling NGINXInstalling PHP-FPMInstalling HHVMInstalling MySQLInstalling Magento 2Installing Magento 2 on HypernodeManaging Magento 2 on Docker

Introduction

This chapter explains how to install Magento 2 on a hosting environment. When installing a new Magento 2 instance, we can use either a Linux, Apache, MySQL, PHP (LAMP) or Linux, NGINX, MySQL, PHP (LEMP) setup. Currently, options such as MariaDB or HHVM are equivalent to MySQL or PHP. Only HHVM will be converted in this chapter.

We will install a clean Magento 2 setup on a hosted virtual private server (VPS) for more advanced users and an easy-to-use installation on Hypernode.

The recipes in this chapter will primarily focus on a basic setup of how to install Magento 2. However, in some situations, we will dive in deeper related to the subject.

While Magento requirements differ from Magento 1, we will be using the latest and finest version from PHP, HHVM, NGINX, Apache, Redis, MySQL, and Ubuntu.

Tip

Creating a new Magento 2 stack could bring up minor issues. Always update to the latest available version, if possible.

Installing Apache

Throughout the following recipes, we will install the latest Apache 2.4.x version on a Software as a Service (SaaS) platform hosted by DigitalOcean. The current Apache version supports HTTP/2. This recipe will show you how to do this.

Tip

HTTP/2 is an optimized version of the Hypertext Transfer Protocol (HTTP), also known as HTTP version 2 (HTTP/2). Some of the new features of HTTP/2 are a more efficient usage of network resources and network latency. HTTP/2 allows multiple concurrent connections over a single Transmission Control Protocol (TCP) connection at once, which optimizes TCP load. Merging CSS and JS is not necessary anymore. HTTP/2 service also provides the possibility to use server push, which allows a proactive push of resources to the client.

Getting ready

For this recipe, you will need to create an account at DigitalOcean https://www.digitalocean.com/. No other prerequisites are required.

How to do it...

For the purpose of this recipe, let's assume that we need to create an Apache hosting environment. The following steps will guide you through this:

The first step is creating an account at https://cloud.digitalocean.com/registrations. All the steps to create an account are straightforward. Confirm your mail and update your billing profile.Start creating your first Droplet. Choose your hostname, and select your size (2GB CPU's or 40GB SSD Disk). Next, pick your region (DigitalOcean has many regions available worldwide). Select your image (we will use the latest Ubuntu). Select extra available settings such as Private Networking to run multiple servers including an Internet network or backup and IPv6 options. If you already have an SSH key, you can upload this:The Droplet takes approximately 60 seconds to get created. You will get an e-mail right after including all the additional login details such as the hostname, IP address, username, and password.Configure your domain name, yourdomain.com, to the addressed IP address from the Droplet:
yourdomain.com A 123.123.123.123 www CNAME yourdomain.com
Connect your Droplet with SSH using your favorite SSH client (putty or terminal) and change your password:
sudo ssh yourdomain.com
First, we will update your server to the latest updates available:
apt-get update && apt-get -y upgrade
Next, we will install Apache 2 using a third-party package. Currently, Ubuntu doesn't include the latest Apache 2.4.x with HTTP/2 support. The third-party vendor that we will use is https://launchpad.net/~ondrej/+archive/ubuntu/apache2. Run the following command:
echo "deb http://ppa.launchpad.net/ondrej/apache2/ubuntu wily main" | sudo tee -a /etc/apt/sources.list.d/apache2.listecho "deb-src http://ppa.launchpad.net/ondrej/apache2/ubuntu wily main" | sudo tee -a /etc/apt/sources.list.d/apache2.list
Before we can install Apache 2, we need to authorize the package by installing a signed key:
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0x4F4EA0AAE5267A6C
Now, we will update our Ubuntu system using the latest Apache2 packages:
apt-get update && apt-get -y install apache2
Once every Apache 2 package is installed, we can check whether everything is in order by running the following command on the shell:
apache2 -v

The output of this command is as follows:

root@mage2cookbook:~# apache2 -vServer version: Apache/2.4.17 (Ubuntu)

If you have followed steps 1 to 9, you will be able to see if the web server is running. Go to your favorite browser and search using your yourdomain.com.

Tip

In the DigitalOcean control panel, you can create multiple snapshots at all times during the recipes. This will help you in restoring an old version or just going back in time.

How it works…

Let's recap and find out what we did throughout this recipe. In steps 1 through 3, we create a clean hosting server setup using DigitalOcean. Next, we connect the Droplet IP to your domain name in DNS. After login via SSH, we are able to update the server and continue to the process of installing Apache 2 via a third-party repository. Depending on the Ubuntu setup, we need to change the version name, such as precise, trusty, wily, vivid, or xenial, to install the software.

In step 8, we submit a key that will validate the repository before we can start installing the Apache 2 software. In step 9, we use a simple command to update the repository and start installing.

There's more…

If you want to check whether Apache is running fine, use one of the following commands:

service apache2 statusnetstat –anp | grep apache2

Installing NGINX

Throughout the following recipes, we will install the latest NGINX 1.9.x version on a SaaS platform hosted by DigitalOcean. The current NGINX version supports HTTP/2. This recipe will show you how to do it.

Getting ready

For this recipe, you will need to create an account at DigitalOcean https://www.digitalocean.com/. No other prerequisites are required.

Tip

For this cookbook, we will use a commercial SaaS hosting provider to get a production-ready environment. You can use any other solution out there to build on your preferred stack.

How to do it...

For the purpose of this recipe, let's assume that we need to create an NGINX hosting environment. The following steps will guide you through this:

Follow steps 1 until 6 in the previous recipe on how to install Apache.Then, we will update our server to the latest updates available:
apt-get update && apt-get -y upgrade
Next, we will install NGINX using the latest mainline version 1.9.x. Currently, Ubuntu doesn't include the latest NGINX 1.9.x mainline version. The latest change log can be viewed at http://nginx.org/en/CHANGES. Run the following command:
echo "deb http://nginx.org/packages/mainline/ubuntu/ wily nginx" | sudo tee -a /etc/apt/sources.list.d/nginx.listecho "deb-src http://nginx.org/packages/mainline/ubuntu/ wily nginx" | sudo tee -a /etc/apt/sources.list.d/nginx.list
Before we can install NGINX, we need to authorize the package by installing a signed key:
wget http://nginx.org/keys/nginx_signing.key | apt-key add nginx_signing.key
Now, we will update our Ubuntu system using the latest NGINX packages:
apt-get update && apt-get -y install nginx
Once every NGINX package is installed, we can check whether everything is in order by running the following command on the shell:
nginx -v

The output of this command is as follows:

root@mage2cookbook:~# nginx -vnginx version: nginx/1.9.6

If you have followed steps 1 to 6, you will be able to see if the web server is running. Go to your favorite browser and search using your yourdomain.com.

Tip

In the DigitalOcean control panel, you can create multiple snapshots at all times during the recipes. This will help you in restoring an old version or just going back in time, such as switching from Apache to NGINX or back.

How it works…

Let's recap and find out what we did throughout this recipe. In steps 1 through 5, we used the same Droplet to install NGINX. All steps are alike, but instead of installing Apache, we use NGINX instead. The only big difference is that it is an official NGINX repository.

There's more…

The current market share of NGINX is around 15% worldwide compared to 50% of Apache on active sites. Over the last couple of years, NGINX has grown and is commonly used as a stable web server for Magento hosting:

http://news.netcraft.com/archives/2015/03/19/march-2015-web-server-survey.html

If you want to check whether NGINX is running fine, use one of the following commands:

service nginx statusnetstat -anp | grep nginx