Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Magento 2 Development Cookbook
  • Table Of Contents Toc
Magento 2 Development Cookbook

Magento 2 Development Cookbook

By : Bart Delvaux
4.8 (5)
close
close
Magento 2 Development Cookbook

Magento 2 Development Cookbook

4.8 (5)
By: Bart Delvaux

Overview of this book

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.
Table of Contents (13 chapters)
close
close
12
Index

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 higher
  • MySQL 5.6 or higher
  • Apache 2.2 or higher
  • Command line access
  • Composer

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

  1. 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.
  2. 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>
    
  3. 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:
    app
    bin
    CHANGELOG.md
    composer.json
    composer.lock
    CONTRIBUTING.md
    CONTRIBUTOR_LICENSE_AGREEMENT.html
    COPYING.txt
    dev
    .gitignore
    Gruntfile.js
    .htaccess
    .htaccess.sample
    index.php
    lib
    LICENSE_AFL.txt
    LICENSE.txt
    nginx.conf.sample
    package.json
    .php_cs
    php.ini.sample
    pub
    README.md
    setup
    .travis.yml
    update
    var
    vendor
    

    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.

  4. 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:
    How to do it...
  5. Hit the Agree and Setup Magento button and start the environment check.
  6. Click on Next and enter your database information as follows:
    • Database Server Host: The hostname or IP address of the database server
    • Database Server Username: The username of the database account
    • Database Server Password: The password for the account
    • Database Name: The name of the database
    • Table Prefix: Optionally, you can give a prefix for each table
  7. 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.

  8. In the next step, you can configure your time zone, currency, and default language.
  9. 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.
  10. When the installer is ready, you will see the following success message:
    How to do it...
  11. Run the following commands in your Magento installation directory to configure the sample data:
    php bin/magento sampledata:deploy
    composer update
    php bin/magento setup:upgrade
    
  12. 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.
  13. 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 to do it...

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

When working with GIT, we only have to commit the composer.json, composer.lock, and .gitignore files for a working Magento 2 project. When another person does a Git clone of the repository and runs the composer's install command, Magento 2 will be installed with the version that is in the composer.lock file.

The sample data for Magento 2 is now a script that will be executed after the installation of Magento. That script will add products, customers, orders, CMS data, and more configurations to populate the shop.

The shop is installed and the configuration settings (database, encryption key, and so on) are now stored in app/etc/env.php instead of in the app/etc/local.xml file in Magento 1.

There's more...

When installing Magento 2, here are some common issues that can occur and their fixes:

  • When you don't see CSS in your browser, you have to check the following things:
    • Make sure the pub/ folder is writable
    • Run the command php bin/magento setup:static-content:deploy to generate the static content
  • You forget to install the sample data:
    • You can install the sample data after the installation of Magento with the command php bin/magento sampledata:deploy
  • The installation is not responding anymore:
    • This could be caused by an Apache timeout. If this occurs, you can maybe try the command-line installation. This works as follows:

To run the Magento installer from the command line, we can use the command php bin/magento setup:install. We have to add the following required parameters to the command to configure the installation:

  • base-url: The base URL, for example http://magento2.local/
  • db-host: The database host or IP address
  • db-user: The database username
  • db-name: The database name
  • db-password: The database password
  • admin-firstname: The first name of the administrator user
  • admin-lastname: The last name of the admin user
  • admin-email: The e-mail address of the admin user
  • admin-user: The username (login name) of the admin user
  • admin-password: The password for the admin user
  • language: The language of the shop
  • currency: The currency code of the shop
  • timezone: The time zone of the shop
  • use-rewrites: Whether to use the apache rewrites or not
  • use-sample-data: Install the sample data (optional)

Look at the following code for a working example of the install command:

php bin/magento setup:install --base-url=http://magento2.local/ --db-host=localhost --db-user=magento2 --db-name=magento2 --db-password=yourpassword --admin-firstname=John --admin-lastname=Doe [email protected] --admin-user=admin --language=en_US --currency=USD --timezone=UTC --use-rewrites=1 
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Magento 2 Development Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon