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...
- 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:
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.
- 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 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
- 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:deploy composer update php 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
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
- Make sure the
- 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
- You can install the sample data after the installation of Magento with the command
- 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 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 --admin-email=john.doe@example.com --admin-user=admin --language=en_US --currency=USD --timezone=UTC --use-rewrites=1