magento-store

How To Scale Your Magento Store

Magento is one of the best eCommerce platforms out there, thanks to its high level of flexibility in what you can achieve both in terms of design and functionality. It’s no wonder that most online shop owners choose it when they want to grow their sales and, overall, ensure their business’s success.

But although Magento stores are already great, there are always little tweaks you can make to achieve even more – improve site speed and performance, enhance security, or simplify the checkout process.

In this article, we cover how to scale a Magento store. We feel that with online shopping becoming more and more popular (partially because of the Coronavirus pandemic and the fact that people can’t visit physical stores as often as previously), shop owners should take the necessary steps to optimize their infrastructure. And by following our suggestions, you’ll achieve just that.

 

How to optimize your infrastructure and scale your Magento store

Summary

The following steps will show you how to install and run Magento on an Ubuntu 20.04 LTS based Nginx server.

After showing you how to set up Magento in Ubuntu, we’ll show you what it takes to set up this application using Bunnyshell.

 

Prerequisites

  • An Ubuntu 20.04 VPS with root access enabled
  • 4GB of RAM or higher
  • Nginx
  • MariaDB 10.4
  • Elasticsearch 7.6.x
  • PHP 7.4 with bcmath, ctype, curl, dom, gd, hash, iconv, intl, libxml, mbstring, openssl, pdo_mysql, simplexml, soap, xsl, and zip extensions enabled. Also, for performance reasons, it’s a good idea to install and enable the opcache extension.
  • A valid domain name for accessing the Magento 2 website. Also, a valid SSL certificate is required for accessing the website using HTTPS.

 

Assumptions

For the scope of this article, we’ll assume that your database is already set up and running on a separate server in the same network as the server you’ll be configuring for installing Magento.

Also for the scope of this article, we’ll detail how to install elasticsearch and on the same machine as the Magento installation, but ideally, this service should run on a separate server, as in the case of the database.

 

Getting started

The first thing you should do is connect to your VPS and make sure everything is updated.

 

# ssh [email protected]_Address -p Port_number

# apt-get update

# apt-get upgrade

 

1. Install NGINX

If there’s an Apache web server installed on the server already, stop the Apache service and disable it from starting on server boot:

# systemctl stop apache2

# systemctl disable apache2

Install Nginx with the following command:

# apt-get install nginx

 

2. Install Elasticsearch

Switch to a directory where you can download a file temporarily.

# cd /opt

Let’s download the Elasticsearch package file.

# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.1-amd64.deb

Install it by running this next command:

# dpkg -i elasticsearch-7.6.1-amd64.deb

Start the Elasticsearch service:

# systemctl start elasticsearch

Check the status:

# systemctl status elasticsearch

Check the version and other information about Elasticsearch:

# curl -XGET ‘http://localhost:9200’

If all looks normal up to this point, we can now install PHP.

 

3. Install PHP 7.4

Add the PPA for PHP 7.4:

# add-apt-repository ppa:ondrej/php

Next, update the OS package list and install PHP 7.4 with the following command:

# apt-get update

# apt-get install php7.4

Install the required PHP extensions with the following command:

# apt install php7.4-fpm php7.4-common php7.4-mysql php7.4-gmp php7.4-curl php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-gd php7.4-xml php7.4-cli php7.4-zip php7.4-bcmath

Once the PHP extensions have been installed on the server, install a few other OS packages that are required for the Magento 2 installation:

# apt-get install git curl software-properties-common

Next, open the main PHP configuration file /etc/php/7.4/fpm/php.ini:

# nano /etc/php/7.4/fpm/php.ini

Change the following settings:

# file_uploads = On

# allow_url_fopen = On

# short_open_tag = On

# memory_limit = 256M

# cgi.fix_pathinfo = 0

# zlib.output_compression = On

# upload_max_filesize = 128M

# max_execution_time = 600

# max_input_time = 900

# date.timezone = America/Chicago

Save and close the PHP configuration file.

 

4. Install Magento 2

First, go to the GitHub repository and download the latest version of Magento:

# mkdir -p /var/www/magento2/

# cd /var/www/magento2/

# git clone https://github.com/magento/magento2.git /var/www/magento2/

Next, we’ll need to install Composer to add the necessary Magento components:

# curl -sS https://getcomposer.org/installer | php — –install-dir=/usr/bin –filename=composer

Run these next commands to configure Composer.

# cd /var/www/magento2

# composer install

# bin/magento setup:install –base-url=http://yourdomain.com/ –db-host=localhost –db-name=magentodb –db-user=magento –db-password=strongPassword –admin-firstname=FirstName –admin-lastname=LastName –[email protected] –admin-user=magentoadmin –admin-password=strong-password –language=en_US –currency=USD –timezone=America/Chicago –use-rewrites=1

Don’t forget to replace the domain name, database host, database name, database user, database password, admin username, password, and admin email address accordingly.

Once the installation is complete, you should receive the following output:

# [Progress: 699 / 701]

# Post installation file permissions check…

# For security, remove write permissions from these directories: ‘/var/www/magento2/app/etc’

# [Progress: 700 / 701]

# Write installation date…

# [Progress: 701 / 701]

# [SUCCESS]: Magento installation complete.

# [SUCCESS]: Magento Admin URI: /admin_1k4pc0

# Nothing to import.

Remember the ‘Magento Admin URI’. You will need this info later as that’s the URL that you’ll be using to log in to the Magento backend.

Change the ownership of the magento2 directory to www-data with the following command:

# chown -R www-data:www-data /var/www/magento2/

 

5. Create an Nginx Configuration File

Create an Nginx configuration file:

# nano /etc/nginx/sites-available/magento2

Then add the following as the file’s content:

# upstream fastcgi_backend {

# server unix:/run/php/php7.4-fpm.sock;

# }

# server {

# server_name yourdomain.com;

# listen 80;

# set $MAGE_ROOT /var/www/magento2;

# set $MAGE_MODE developer; # or production

# access_log /var/log/nginx/magento2-access.log;

# error_log /var/log/nginx/magento2-error.log;

# include /var/www/magento2/nginx.conf.sample;

# }

Make sure that you set yourdomain.com to your registered domain name.

Remove the default Nginx configuration file if you don’t use it:

# rm -f /etc/nginx/sites-enabled/default

Enable the newly created Nginx configuration file, then test the Nginx configuration and make sure that there are no errors:

# ln -s /etc/nginx/sites-available/magento2 /etc/nginx/sites-enabled/magento2

# nginx -t

# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

# nginx: configuration file /etc/nginx/nginx.conf test is successful

That’s it! Your web server is now successfully configured.

 

6. Install an SSL Certificate

We’re going to install a free SSL certificate from Let’s Encrypt. Install the required packages by running this command:

# apt-get install certbot python3-certbot-nginx

Then install a new SSL certificate for your domain name, making sure that yourdomain.com is set to your registered domain name:

# certbot –nginx -d yourdomain.com -d www.yourdomain.com

Please select ‘2’ and choose to redirect HTTP traffic to HTTPS:

# – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

# 1: No redirect – Make no further changes to the webserver configuration.

# 2: Redirect – Make all requests redirect to secure HTTPS access. Choose this for

# new sites, or if you’re confident your site works on HTTPS. You can undo this

# change by editing your web server’s configuration.

# – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

# Select the appropriate number [1-2] then [enter] (press ‘c’ to cancel): 2

Use the following Magento 2 CLI commands to update the Magento base-url and the base-url-secure values:

# cd /var/www/magento2/

# php bin/magento setup:store-config:set –base-url=”https://yourdomain.com/”

# php bin/magento setup:store-config:set –base-url-secure=”https://yourdomain.com/”

Edit the Nginx configuration file and enable HTTP/2:

# nano /etc/nginx/sites-enabled/magento2

Replace:

# listen 443 ssl;

With:

# listen 443 ssl http2;

Restart the Nginx service for the changes to take effect:

# systemctl restart cron.service

 

7. Install Cron Job

Magento requires a cron job configuration for many important system functions. Let’s create the following cron job:

# crontab -u www-data -e

Add this to the file:

# * * * * * /usr/bin/php /var/www/magento2/bin/magento cron:run | grep -v “Ran jobs by schedule” >> /var/www/magento2/var/log/magento.cron.log

Save and close the file.

Restart the cron service:

# systemctl restart cron.service

That is it! Magento 2 has been successfully installed on your server.

 

How to set up a Magento store using Bunnyshell

The steps we’ve described are quite time-consuming, right? Especially when you need to follow them multiple times.

So, what if we told you there’s an easier way to do this, using Bunnyshell?

  1. Go to the application section and select the Frameworks tab;
  2. Select Magento;
  3. Select an existing virtual machine or leave it blank to create a new one;
  4. Select your desired cloud provider and your machine location and size;
  5. Give your application a name and create it.

magento-store-6

 

By following these 5 easy steps, you can actually use a machine that’s already set up to run your Laravel application without having to go through all the configuration steps we’ve detailed above. This is where Bunnyshell helps the most – it enables you to automate repetitive tasks so you can focus on more important ones.

After the virtual machine is created, you’re all set to start deploying your application, and no, you won’t need to use an FTP client to upload your files manually. All you have to do is go to Applications and open the application you just created, select Deployments, and click Enable Deployment.

magento-store-5

This will allow you to configure a repository to use as a codebase and select the branch you want to have deployed. Needless to say, this can help configure staging and production environments based on repository branches.

magento-store-2

From here on, you could also set up back-ups. However, since your Magento instance doesn’t need any back-ups because it doesn’t hold any sensitive or persistent data, you can skip enabling this.

 

 

How to scale a Magento store with Bunnyshell

So, all that is fine and simplifies how you deploy your application’s codebase, but we haven’t really talked about scaling, aside from scaling your deployment process.

Now that you have your application and its deployment process, all you have to do to scale into more machines.

First, you need to create a snapshot of your current Virtual Machine you created for your application or selected during the setup process.

After that, you need to go to the OPS section under Snapshots, select the newly created snapshot, and create a new Virtual Machine by selecting the Restore option from that snapshot.

magento-store-3

magento-store-4

 

Conclusion

As you can see, in order to scale your Magento store by setting up Laravel in Ubuntu, you need to complete a lot of steps. However, if you choose to set up Laravel in the cloud with Bunnyshell, it’s a simpler process with lots of automation and optimizations under the hood that makes your life easier and helps take your mind off the DevOps side of development, and makes your eCommerce work better and faster.