-
Book Overview & Buying
-
Table Of Contents
Magento 2 Development Cookbook
By :
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.
To install Magento 2, we need the newest tools to run that application. Make sure your webserver has the following stuff installed:
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.
composer create-project --repository-url=https://repo.magento.com magento/project-community-edition <installation_dir>
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
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.

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

php bin/magento sampledata:deploy composer update php bin/magento setup:upgrade
setup:upgrade command will install the sample data, and this also takes some time.
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.
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.
When installing Magento 2, here are some common issues that can occur and their fixes:
pub/ folder is writablephp bin/magento setup:static-content:deploy to generate the static contentphp bin/magento sampledata:deployTo 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 addressdb-user: The database usernamedb-name: The database namedb-password: The database passwordadmin-firstname: The first name of the administrator useradmin-lastname: The last name of the admin useradmin-email: The e-mail address of the admin useradmin-user: The username (login name) of the admin useradmin-password: The password for the admin userlanguage: The language of the shopcurrency: The currency code of the shoptimezone: The time zone of the shopuse-rewrites: Whether to use the apache rewrites or notuse-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
Change the font size
Change margin width
Change background colour