Zealousweb
  • Discover ZealousWeb
  • Services
  • Technologies
  • Industries
  • Insights
Nginx Integration In Magento 2

Nginx Integration In Magento 2 For Multi Websites

December 24, 2019Posted By: Ankit Dobariya
Magento2Nginx Integration In Magento 2

Introduction

With evolving technology, it becomes challenging for developers to stay well-versed with every update and integrate it into their processes. If you are a Magento developer, you might have encountered a similar experience.

Hence, in this guide, we will simplify your Nginx integration in Magento 2 for multiple websites. We will walk you through its benefits and share all the necessary steps to ensure you have clarity on fundamental parts. Let’s get started!

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 that enables the processing of multiple requests at the same time. It requires PHP-fpm to process PHP requests

Steps For Configuring Nginx For Single, Multistore, And SubDomain

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

However, before Configuration, some fundamental And required steps 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 modifications 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 been properly installed and whether the service is active or not.

service nginx status

If you need any help or guidelines for the Nginx, you can get them 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, you are now 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.

Nginx for Multi-websites and Single-Websites in Magento

Benefits Of Nginx For Magento 2 Multi Websites

There are several benefits of using Ngnix as a web server for Magento 2 multi-websites. The following are the primary advantages:

  • High Performance: Nginx is known for its lightweight and efficient architecture, enabling it to handle high traffic loads and deliver fast response times
  • Scalability: Nginx’s event-driven, non-blocking model allows it to handle many concurrent connections, making it suitable for scaling Magento 2 across multiple websites.
  • Reverse Proxy And Load Balancing: Nginx can act as a reverse proxy and distribute incoming traffic across multiple backend servers, ensuring optimal resource utilization and improved reliability.
  • Caching: Nginx provides built-in caching capabilities, allowing for faster content delivery and reduced server load.
  • SSL/TLS Termination: Nginx simplifies SSL/TLS termination, enabling secure communication between clients and servers.
  • URL Rewriting: Nginx offers powerful URL rewriting capabilities, facilitating clean and SEO-friendly URLs for each Magento 2 website.

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

A. To Create A Website

  • Click Stores -> Setting -> All Stores
  • On the Stores page, click Create Website and add the following detailsName
    • 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.

B. 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

C. 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 the 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 the code. You have to pass the necessary parameters to configure the virtual host file accordingly.

A. 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.

B. To Create Or Configure Multiple Virtual Host Files

The following are the steps for creating or configuring multiple virtual host files as per website that will be customized according to your 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 Nginx Service

The following is the Command to restart the service of Nginx and verify whether the site is working properly or not:

service nginx restart

You can also do this in another alternate way if you don’t have 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 their default websites as follows:

Step 1: Create Or Set Up Multi Websites In Magento 2

The following are the steps to set up and create multi websites in Magento using the Sub Folder concept:

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: File Creation As Per Domain

The following is the command for creating your file according to your domain for each multiple websites.

Open CLI And Enter The Command:

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

Step 3: Open The File In The Editor By The 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: Create Your File As Per Domain

The following is the command to create your file according to the domain for multiple websites:

In our example., we have two websites, so we will create two each Nginx virtual host file. Open CLI And Enter The Command:

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; }

Pointers For Effective Troubleshooting In Magento 2 Multi Websites

In this section, we will share crucial pointers for effective troubleshooting of common issues and debugging Nginx integration in Magento 2 multiple websites.

  • Check Nginx error logs for any error messages or warnings related to the multi-website configuration.
  • Verify the Nginx configuration files for syntax errors or misconfigurations, such as incorrect server blocks or missing directives.
  • Ensure the correct permissions are set for files and directories accessed by Nginx.
  • Investigate any issues with SSL/TLS certificates, including expiry or misconfiguration.
  • Debug and monitor network connections and request/response flows using tools like Wireshark or ngrep.
  • Analyze Magento 2 logs to identify any errors or issues specific to the multi-website setup.
  • Test individual website configurations separately to isolate and identify any conflicts or specific issues.

Testing And Verifying Nginx Integration In Multi-Website Environment

Testing and Verifying Nginx Integration in Multi-Website

In this section, we will share pivotal information to test and verify Nginx integration in multiple websites environment for Magento 2.

  • Verify that the Nginx server blocks are properly configured for each website in the multi-website setup. This ensures correct routing and separation of website traffic.
  • Test the SSL/TLS configuration to ensure secure communication between clients and the Nginx server, including proper certificate installation and encryption settings.
  • Validate that the Nginx server correctly routes requests to the appropriate website based on the server name specified in the configuration.
  • Verify that Nginx effectively delivers static files and media content, such as images and videos, for each website in the multi-website environment, If load balancing is implemented, test the Nginx configuration to ensure even distribution of traffic across backend servers for each website.
  • Test Redirects and Rewrite Rules- Verify that redirects and rewrite rules are correctly implemented in the Nginx configuration. This ensures proper handling of URLs and website redirection.

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 additional changes.
  • Do not change the nginx.config.sample file is unnecessary as it is Magento’s 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 uses the Nginx server, then .htaccess on the root directory is not going to be useful like it is used in the Apache server. If you want to change the Mage_mode for the Magento 2 directory, then we have to change it in two places: one is in the Magento core file /app/etc/env.php and another in the 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. If you don’t restart Nginx services, your changes will not reflect on the storefront.

Conclusion

We hope you have understood the configuration for the Nginx in Magento 2. 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’re a Magento Developer or a Magento Development Agency, then you know that it is not entirely easy to perform this process. Moreover, it is not a cup of everyone’s tea to digest all the information we have provided and integrate it easily with proper settings.

Hence, it is wiser to partner with an agency providing Magento Development Services. It will save you considerable time and human resources. And you will get your work carried out by Magento experts.

At ZealousWeb, we have a team of Magento professionals experienced in Nginx integration in Magento 2 for multiple websites. You can bank on our expertise and enjoy premier-quality output.

Moreover, if you have any questions or want a quotation for our services, feel free to contact us at hello@zealousweb.com.

FAQ

What Should Be Done After Changing Any Settings In The Nginx Config File?

Is It Possible To Add Code In htaccess For Speed Optimization?

 Is Varnish Cache Configuration The Same As We Are Doing In Apache?

Do We Need To Update MAGE_MODE For All The Websites With Its Config File In Nginx?