Installing And Configuring Magento 2

August 4, 2022
August 4, 2022
Christian Bafia

About

In this tutorial, we will be installing the latest version of Magento 2 within your JetRails environment with the help of Composer. We will also explain how you can configure your Magento 2 store to use Redis for sessions, backend-cache, and full-page cache.

Download Magento

First, you will need to obtain your access keys so you are authorized to download an instance from Magento via Composer. You can follow Magento’s official tutorial on how you can obtain these credentials.

Once you have these credentials, you can download Magento via Composer. Next, you can access your root directory for Magento within the JetRails environment. In this tutorial that will be /home/jetrails/{{{domain:example.com}}}/html, where the primary domain name is {{{domain:example.com}}}. Now, you can run the following Composer command in order to download the latest version of Magento Open-Source (community):

$ composer create-project --repository=https://repo.magento.com/ magento/project-community-edition .

You will be presented with entering your username. This will be your public key from the credentials you obtained earlier. Your password will be the private key from the credentials that you obtained earlier. You will then be presented with the option to store your credentials so you would not be prompted for them in future runs.

Install Magento

In order to install Magento 2, you will need to know auxiliary information about the services that are running on your deployment. This information can be found in ~/CREDENTIALS. For this tutorial, we will assume the following values are inside the CREDENTIALS file:

AttributeValue
Primary Domain{{{domain:example.com}}}
MySQL Host{{{db_host:localhost}}}
MySQL Database{{{db_name:main_db}}}
MySQL User{{{db_user:main_user}}}
MySQL Password{{{db_pass:appleseed123}}}
Redis Session Host{{{redis_session_host:localhost}}}
Redis Session Port{{{redis_session_port:6379}}}
Redis Session Database{{{redis_session_db:0}}}
Redis Session Password{{{redis_session_pass:session1}}}
Redis Backend Cache Host{{{redis_backend_host:localhost}}}
Redis Backend Cache Port{{{redis_backend_port:6380}}}
Redis Backend Cache Database{{{redis_backend_db:0}}}
Redis Backend Cache Password{{{redis_backend_pass:cache1}}}
Redis FPC Host{{{redis_fpc_host:localhost}}}
Redis FPC Port{{{redis_fpc_port:6381}}}
Redis FPC Database{{{redis_fpc_db:0}}}
Redis FPC Password{{{redis_fpc_pass:fpc1}}}

We will also assume the following information about the admin user:

AttributeValue
Admin Username{{{admin_user:foo}}}
Admin Password{{{admin_pass:bar}}}
Admin Email{{{admin_email:[email protected]}}}
Admin First Name{{{admin_fname:John}}}
Admin Last Name{{{admin_lname:Appleseed}}}

Now that we have all the information that we need, we can move forward with the installation. We can now run the Magento install command:

$ php bin/magento setup:install \
    --base-url=https://{{{domain:example.com}}}/ \
    --db-host={{{db_host:localhost}}} \
    --db-name={{{db_name:main_db}}} \
    --db-user={{{db_user:main_user}}} \
    --db-password={{{db_pass:appleseed123}}} \
    --admin-firstname={{{admin_fname:John}}} \
    --admin-lastname={{{admin_lname:Appleseed}}} \
    --admin-email={{{admin_email:[email protected]}}} \
    --admin-user={{{admin_user:foo}}} \
    --admin-password={{{admin_pass:bar}}} \
    --language=en_US \
    --currency=USD \
    --timezone=America/Chicago \
    --use-rewrites=1 \
    --use-secure=1 \
    --base-url-secure=https://{{{domain:example.com}}}/ \
    --use-secure-admin=1

Note There are many other options that can be used for setting up Magento but we will keep it to a minimum for the sake of this tutorial. The other options that can be used can be found in Magento’s official documentation.

Redis for session cache

With the Magento 2 store now installed, we can now focus on configuring Redis for sessions. These are general Redis session options that we use:

$ php bin/magento setup:config:set \
    --session-save=redis \
    --session-save-redis-host={{{redis_session_host:localhost}}} \
    --session-save-redis-port={{{redis_session_port:6379}}} \
    --session-save-redis-password={{{redis_session_pass:session1}}} \
    --session-save-redis-db={{{redis_session_db:0}}} \
    --session-save-redis-timeout=2.5 \
    --session-save-redis-compression-threshold=2048 \
    --session-save-redis-compression-lib=gzip \
    --session-save-redis-log-level=3 \
    --session-save-redis-max-concurrency=20 \
    --session-save-redis-break-after-frontend=5 \
    --session-save-redis-break-after-adminhtml=30 \
    --session-save-redis-first-lifetime=600 \
    --session-save-redis-bot-first-lifetime=60 \
    --session-save-redis-bot-lifetime=7200 \
    --session-save-redis-disable-locking=0 \
    --session-save-redis-min-lifetime=60 \
    --session-save-redis-max-lifetime=2592000

Redis for backend cache

Now we can also configure Magento to use Redis for backend cache:

$ php bin/magento setup:config:set --cache-backend=redis \
    --cache-backend-redis-server={{{redis_backend_host:localhost}}} \
    --cache-backend-redis-port={{{redis_backend_port:6380}}} \
    --cache-backend-redis-db={{{redis_backend_db:0}}} \
    --cache-backend-redis-password={{{redis_backend_pass:cache1}}}

Redis for full-page cache

Within our JetRails environments, you have the option to use Redis for full-page cache (FPC) or you may use Varnish. If you would like to use Redis for FPC, then you can use the following command:

$ php bin/magento setup:config:set --page-cache=redis \
    --page-cache-redis-server={{{redis_fpc_host:localhost}}} \
    --page-cache-redis-port={{{redis_fpc_port:6381}}} \
    --page-cache-redis-db={{{redis_fpc_db:0}}} \
    --page-cache-redis-password={{{redis_fpc_pass:fpc1}}}

Note: If you are looking to use Varnish for FPC, then you might be interested in our Magento 2 Varnish extension. This extension is available for our customers only and documentation about it can be found here.