First, we will start installing Magento 2 on Apache; later, we will do the same with NGINX. Throughout this recipe, you can use PHP-FPM or HHVM. For now, we will focus only on PHP-FPM.
Installing Composer is pretty straightforward. For the rest of the recipes, we will be installing Composer in the /usr/local/bin
directory. This way, we can use it system-wide. Run the following command on the shell in the /usr/local/bin
directory:
If you have not installed curl yet, you may need to run the following command on the shell:
To check whether Composer is working fine, run the following command:
You will get the following output:
While we will be using a search-friendly URL in Magento, we need to enable the Apache mod_rewrite module before we can continue. Run the following code on the shell:
Restart your Apache server to complete the new configuration. Run the following code on the shell:
If you see the following output on your screen, everything is correct:
Note
Installing Magento 2 can be done in several ways. First, we will be showing you how to install it using the latest version from the GitHub release branch. Download the complete package and unzip or untar the files in your /var/www/html
directory.
The URL for the latest releases is https://github.com/magento/magento2/releases.
Make sure that your /var/www/html
has no files in it anymore. (Use the rm *
command to clean up.)
Execute either of the following commands:
While extracting your files, make sure to move or copy them in to the /var/www/html
directory.
Now let's set the ownership and permissions:
Now we use Composer to resolve all of our dependencies. Run the following command on the shell:
Tip
If your installation is set up correctly, the installation will continue, using the correct PHP modules. However, if, in some way, your PHP installing is not correct, you may get the following error messages. Update your system and try again:
Your requirements could not be resolved to an installable set of packages.
Problem 1
Problem 2
Problem 3
During the installation process, you will get a notice to create a GitHub OAuth token. The download rate limit is pretty small. Copy the URL from your shell window in your browser, log in at GitHub, or create an account and token:
Copy the generated token in the command prompt of your current shell window and the token will be saved for future reference in /root/.composer/auth.json
. You can continue the rest of the installation.
Next, we will be using the setup wizard to continue the rest of the installation. In Chapter 2, Magento 2 System Tools, we will be using the shell installation method. Go to your favorite browser and run the following URL:
http://mage2cookbook.com/setup
As a result, we will get the following screen:
Now click on Agree and Setup Magento.
Let's start analyzing the readiness of your setup. Click on the Next button:
Magento 2 now will check your hosting setup for the correct PHP version, PHP modules, PHP settings, and file permissions, as shown in the following screenshot:
Continue the installation flow and commit your database credentials in the following screen:
In Step 3, we will set up our store address and administrator address. In Advanced Options, we are able to pick HTTPS, Apache rewrites, encryption key, and where to store the session key:
Step 4 gives you the opportunity to choose your time zone, currency, and language. In Magento 2, there is a brand new option to pick which modules need to be installed. You can easily select them by just clicking the checkbox, as shown in the following screenshot:
In Step 5, you can choose your username and password:
Now we are almost there; the final Step 6 will start installing our Magento 2 environment:
The installation step is really fast in Magento 2 and is done within a minute depending on your server. If you get an error in the console screen, such as a missing database, create one via the shell with the following command:
Installation of Magento 2 can be seen in the following screenshot:
Congratulations, you have successfully installed Magento 2. You will be given administrative information on your login and store address.
For security reasons, you may want to alter the write permissions of your /app/etc
directory to only read permissions with the following command:
Now we will focus on the configuration of Magento 2 on NGINX. We don't need to reinstall Magento 2; we just need to alter the current NGINX setup with the appropriate setting.
Now, let's go to the NGINX configuration directory and update default.conf
in /etc/nginx/conf.d.
Open the default.conf
file and change it with the following settings:
As you can see, we are now using an upstream
and set $MAGE
setting and have removed the fastcgi_pass
.
However, the most important element is the Magento 2 nginx.conf.sample
file, which is in the root directory of your Magento 2 instance. For now, it is easy to include this directory but not advised on a production level. It's best that you copy this file to your NGINX configuration directory and store it there. Don't forget to change the following setting:
Save your configuration and run the following command to check whether your settings are correct. If not, change your configuration:
If your syntax is okay and the test is successful, then restart your NGINX server. Don't forget to stop your Apache server. Run the following command on the shell:
To check whether you are running NGINX, use the following command:
Congratulations, you just finished the installation of Magento 2 on NGINX.