Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Magento 2 Development Cookbook

You're reading from   Magento 2 Development Cookbook Over 60 recipes that will tailor and customize your experience with Magento 2

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785882197
Length 304 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Upgrading from Magento 1 FREE CHAPTER 2. Working with Products 3. Theming 4. Creating a Module 5. Databases and Modules 6. Magento Backend 7. Event Handlers and Cronjobs 8. Creating a Shipping Module 9. Creating a Product Slider Widget 10. Performance Optimization 11. Debugging and Unit Testing 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 
You have been reading a chapter from
Magento 2 Development Cookbook
Published in: Dec 2015
Publisher:
ISBN-13: 9781785882197
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image