close

Somewhere, in a parallel universe,
the-other-you just clicked on
"Subscribe To Our Monthly Newsletter"

The-other-you seems eager to stay updated with the technological changes.

Please enter your name. Please enter your name.
Please enter your email. Please enter your email.

Nginx Integration In Magento 2 For Multi Websites

Post by|Magento21 July,2020
761 View

What Is Nginx?

Nginx is an open-source web server that offers high performance and serves highly scalable architecture that is very different from Apache. It combines the features of HTTP load balancer, content cache, reverse proxy, and mail proxy.

It is an event-driven approach, which enables the processing of multiple requests at the same time. It requires PHP-fpm to process PHP requests.

There is a very easy approach when we are using an apache server for a single or multi-store in Magento 2. But what if when we are using Nginx server for Magento 2. The following are the steps that show you how to configure Nginx for Multistore, Single Store, and Subdomain.

Before Configuration, Some Basic And Required Steps That Are Necessary.

1. First, make sure you have properly installed the Nginx server and PHP-fpm with Magento 2 because Nginx only works with PHP-fpm. If you want to install Nginx, then follow these steps;

  • Run the command

apt-get -y install Nginx

  • Install and configure php-fpm and php – cli

apt-get -y install php7.0-fpm php7.0-cli

  • Open the php.ini file in an editor and run the following commands

vim /etc/php/7.0/fpm/php.ini
vim /etc/php/7.0/cli/php.ini

  • Make necessary modification in the file and save it in the editor

memory_limit = 2G;
max_execution_time = 1800
zlib.output_compression = on

  • Restart the service of PHP-fpm and Nginx by running the following commands.

systemctl restart php7.0-fpm
service restart nginx

  • Verify if Nginx is properly installed or not by running the following command. It will provide you with a list of responses to ensure that your Nginx has properly installed and service is active or not.

service nginx status

If you need any help or guidelines for the Nginx you can get by running this command as below;
systemctl -h nginx

2. Accept multiple domains in one virtual host file, or use one virtual host file for each website or store. The virtual host configuration files are located in;

/etc/nginx/sites-available.

3. Use and modify nginx.conf. – a sample file for the configuration, if necessary that is provided by Magento 2 by default.

4. Make sure Magento 2 has been installed to its path properly. For example., /var/www/html/magento2

5. Create more than one website or store in Magento 2 other than the default, as per the requirement.

After all this basic configuration now we are ready to configure Nginx for both Multi-websites and Single-Websites in Magento 2. The Multi-Websites concept in Magento 2 can be achieved either by using its subdomain or by creating a subfolder to its root directory.

Multi Websites Using The Concept Of Sub Domain

Steps To Configure Nginx For Multi Websites In Magento 2

Step 1: Create or set up multi websites in Magento 2

1. To create a website

  • Click Stores -> Setting -> All Stores
  • On the Stores page, click Create Website and add following details
    • Name – Enter a name to identify the website.
    • Code – Enter a unique code. For example., if you have a Canada store, you can enter Canada.
    • Sort Order – Enter a numerical sort order; this is an optional field.
  • Click save Web Site and you can add as many as you wish, based on the requirement.

2. To create a store

  • Click Stores -> Setting -> All Stores
  • On the Stores page, click Create Store
    • Website – Click the name of the website with which to associate this store.
    • Name – Enter name of the store.
    • Code – Enter a unique code for the store
    • Root Category – Click and select the name of Root Category for this store.
  • Click save Store and you can add as much as based on websites that you have created.

3. To create a store view

  • Click Stores -> Setting -> All Stores
  • On the Stores page, click Create Store View
    • Store – Click the name of the store with which to associate this store view.
    • Name – Enter a name for the store view for identifying.
    • Code – Enter a unique code for the store view.
    • Status – Select Enabled.
  • Click the save Store view and you can add as much as based on websites and stores that you have created.

Step 2: Create and configure Nginx Virtual hosts file by using the concept of Sub Domain.

Creating and configuring Nginx virtual host will define how we can load our websites on the storefront. You can do this either by defining a single virtual host file for multiple websites or multiple virtual host files per website by customizing code. You have to pass the necessary parameters to configure the virtual host file accordingly.

1. To create one virtual host file for each website

  • Open a text editor and add the following contents to a new file named

/etc/nginx/sites-available/magento:

map $http_host $MAGE_RUN_CODE {
default ”;
canada.site.com canada;
mexico.site.com mexico;
}

server {
listen 80;
server_name site.com canada.site.com mexico.site.com;
set $MAGE_ROOT /var/www/html/magento2;
set $MAGE_MODE developer;
set $MAGE_RUN_TYPE website; or store
include /var/www/html/magento2/nginx.conf;
}

  • Save your changes to the files and exit the text editor.
  • Verify the server configuration: nginx -t
  • If successful, the following message displays

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

If errors display, check the syntax of your virtual host configuration files.

2. To create or configure multiple virtual host file per websites that will be customized according to its website

  • Open a text editor and add the following contents to a new file named

/etc/nginx/sites-available/canada.site.com:

map $http_host $MAGE_RUN_CODE {
canada.site.com canada;
}
server {
listen 80;
server_name canada.site.com;
set $MAGE_ROOT /var/www/html/magento2;
set $MAGE_MODE developer;
set $MAGE_RUN_TYPE website;
#or set $MAGE_RUN_TYPE store;
include /var/www/html/magento2/nginx.conf;
}

Create another file named mexico.site.com in the same directory with the following contents:

map $http_host $MAGE_RUN_CODE {
mexcio.site.com mexico;
}

server {
listen 80;
server_name mexico.site.com;
set $MAGE_ROOT /var/www/html/magento2;
set $MAGE_MODE developer;
set $MAGE_RUN_TYPE website; #or set $MAGE_RUN_TYPE store;
include /var/www/html/magento2/nginx.conf;
}

  • Save your changes to the files and exit the text editor.
  • Verify the server configuration: nginx -t
  • If successful, the following message displays

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

If errors display, check the syntax of your virtual host configuration files.

Step 3: Restart service of Nginx by the following command and verify the site if it is working properly or not

service nginx restart

You can also do this in another alternate way if you have not sites-enabled in your Nginx configuration by creating a virtual host file in a different way for subfolders of the websites

Multi Websites Using The Concept Of Sub Folder

After creating, websites, stores and store views from Magento Admin. We have to create two subfolders for the two websites with its default websites as follows;

Step 1: Create or set up multi websites in Magento 2 by using the concept of Sub Folder.

For example.,Canada

  • Create a folder named Canada within the Magento root Directory and add .htaccess and index.php from its Magento root directory.
  • Replace code in index.php

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

With this,
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = ‘canada’;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = ‘website’;
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

For example., Mexico

  • Create a folder named Mexico within the Magento root Directory and add .htaccess and index.php from its Magento root directory.
  • Replace code in index.php

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

With this,
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = ‘mexico’;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = ‘website’;
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

Step 2: Open CLI and enter the command. This will create your file as per your domain for each multiple websites

sudo vim /etc/nginx/conf.d/site.com.conf

Step 3: Open the file in editor by above command and apply the necessary changes as follows

server{
listen 80;
server_name site.com;
set $MAGE_ROOT /var/www/html/magento2;
set $MAGE_MODE developer;
include /var/www/html/magento2/nginx.conf;

}

And if you have multiple websites in it, then add this in file as follows

location /canada/ {
set $MAGE_ROOT /var/www/vhosts/site.com/httpdocs/pub;
set $MAGE_MODE developer;

try_files $uri $uri/ /canada/index.php?$args;
}

location /mexico/ {
set $MAGE_ROOT /var/www/vhosts/site.com/httpdocs/pub;
set $MAGE_MODE developer;

try_files $uri $uri/ /mexico/index.php?$args;
}

Configure Each Website Domain Virtual File On Nginx

Step 1: Open CLI and enter the command. This will create your file as per your domain for each multiple websites

In our example., we have two websites so we will create two each Nginx virtual host file

sudo vim /etc/nginx/conf.d/canada.site.com.conf and sudo vim /etc/nginx/conf.d/mexico.site.com.conf

Step 2: Open the file in the editor by above command and apply the necessary changes as follows

Canada
server{
listen 80;
server_name canada.site.com;
set $MAGE_ROOT /var/www/html/magento2/canada/;
set $MAGE_MODE developer;
include /var/www/html/magento2/nginx.conf;

}

Mexico
server{
listen 80;
server_name mexico.site.com;
set $MAGE_ROOT /var/www/html/magento2/mexico/;
set $MAGE_MODE developer;
include /var/www/html/magento2/nginx.conf;

}

Additional Note

  • Some additional tasks or changes might be required, like port changing to configure Nginx settings as per the requirement. You may have to contact the hosting provider for the additional changes.
  • Do not change the nginx.config.sample file unnecessary as it is Magento default core file. If you want to change, then copy the content of that file, create nginx.config file, paste the code in it and then make necessary changes for memory or execution time.
  • When Magento 2 using Nginx server, then .htaccess on the root directory is not going to be useful like it is used in Apache server. If you want to change the Mage_mode for the Magento 2 directory, then we have to change in two places, one is in Magento core file /app/etc/env.php and another in Nginx virtual host file, the one we created set $MAGE_MODE developer;
  • Make sure after each change in the Nginx configuration file, restart the Nginx services. It may happen that if you won’t restart Nginx services then your changes will not reflect on the storefront.

Final Takeaway

We hope you have understood the configuration for the Nginx in Magento 2. If you’re a Magento Developer or a Magento Development Agency, then you know that it is not entirely easy to perform this process, we have provided as much as information that helps to integrate easily with proper settings. Both Apache and Nginx have many different qualities. While Apache configuration is easier than Nginx, the latter is way better than Apache in terms of performance and lightweight results.

If you have any questions feel free to contact ZealousWeb and we will provide you with all answers to your questions.

FAQ

What should be done after changing any settings in the Nginx Config file and want to see changes on the website?

You have to restart your Nginx service after you do any change. You can run commands as Service nginx restart

Is it possible to add code in htaccess for speed optimization or by creating a custom maintenance page?

No as when nginx server is in use for any Magento websites, you have to change in its nginx config file for any code if it is for speed optimization or for any maintenance code.

Is varnish cache configuration the same as we are doing in Apache?

 No, it is not the same. You have to download the varnish.vcl file and add it to its required path in Nginx like on /etc/varnish. 

Rename the varnish.vcl file to default.vcl, file and upload it. Make necessary changes to the port by concerning with your Hosting Team or with your Network head in your company. Generally, varnish usually runs on Port 80.

Do we need to update MAGE_MODE for all the websites with its config file in nginx?

Yes you have to change MAGE_MODE while you’re changing “Developer to Production” or vice versa. You must change for all the nginx config files.

Load more