Migrating a Magento Installation from Apache2 to NGINX on Ubuntu 18.04 LTS

Magento is one of the worlds leading e-commerce CMS solutions and is used by over 200K websites. What makes Magento such an appealing e-commerce CMS is that Magento offers a free, community edition of their platform. Anyone with some Linux knowledge and access to a modest virtual machine can get their online store up and running in no time. Magento runs on a Linux stack giving wide flexibility on the open source components that are used to power the store. The typical first choice for a Linux web server is usually Apache which makes for a stable and dependable choice. However, there are other options that can offer greater performance with lower system resource usage. One popular such alternative is NGINX.

In this guide, we will walk through the process of migrating an existing Magento installation from Apache to NGINX on Ubuntu 18.04 LTS.

Prerequisites

In order to follow this guide you will need the following:

  • An Ubuntu 18.04 server.
  • A Magento installation.
  • A non-root sudo user account on the Magento server.

In order to begin this guide you need to log into your server as a non-root sudo enabled user.

Put Magento in Maintenance Mode

During this guide, we will be stopping and starting the web servers which will make your Magento store appear and disappear for any user accessing it. This is not a great experience for your clients we will avoid this issue by putting Magento into maintenance mode. When Magento is in maintenance mode any visitor will see a holding page and will not be able to interact with your store.

You put Magento into maintenance mode by running the following command:

sudo php /path/to/magento2/bin/magento maintenance:enable --ip=

The --ip= option will allow you to access your store from your IP whilst all other IP’s will see the maintenance page. This will allow you to view and check that the store is working whilst still maintaining maintenance mode for visitors.

Now that Magento is in maintenance mode we can install NGINX.

Step 2 – Installation

In this step, we will install NGINX and the PHP-FPM packages. NGINX uses PHP-FPM which is a performant re-implementation of standard PHP. If you are already using PHP-FPM with Apache you can remove it from the apt-get install command but leaving it in place will not cause a problem.

We should stop Apache here or the installation will encounter an error when APT attempts to start NGINX while Apache is running. The following commands will stop Apache running and disable it from stating on boot:

sudo systemctl stop apache2.service
sudo systemctl disable apache2.service

Run the following command to install NGINX and PHP-FPM:

sudo apt-get install nginx php-fpm

NGINX is now installed and ready for configuration.

Installation NGINX and PHP-FPM

In this step, we will configure NGINX to serve only an HTTP Magento instance. If your Magento instance uses HTTPS then skip ahead to the next section which covers migrating an HTTPS Magento instance.

NGINX works like Apache in that it has two directories /etc/nginx/sites-available and /etc/nginx/sites-enabled that contain the web server configuration to serve your store. We will first place the configuration file into /etc/nginx/sites-available and then create a symlink to that file in /etc/nginx/sites-enabled which will allow NGINX to start serving your site.

We will create and edit the site configuration file using a text editor. I will use nano throughout this guide but you can use whichever you are most comfortable with:

sudo nano /etc/nginx/sites-available/magento.conf

The contents of this file should look like the following basic example (remember you need to change exmaple.com and /var/www/magento2 to match your setup):

server {
   listen 80;
   server_name ;
   set $MAGE_ROOT /path/to/magento2;
   include /path/to/magento2/nginx.conf.sample;
}

upstream fastcgi_backend {
   server  unix:/var/run/php/php7.1-fpm.sock;
}

The above configuration assumes that you are using PHP 7.1 (the default) on your server. If you are using a different version then you will need to edit the line server unix:/var/run/php/php7.1-fpm.sock; to match the version of PHP and PHP-FPM you are using. If you run:

ls /var/run/php/

You will be able to see the name of the PHP-FPM socket that you need to use.

The following line:

include /path/to/magento2/nginx.conf.sample;

causes NGINX to load additional configuration contained in the file /path/to/magento2/nginx.conf.sample. This file is supplied by Magento and contains very important additional configuration such as blocking access to confidential files, setting compression etc. If you don’t have a copy of this file you can download one from these locations:

You will need to include this file for this NGINX configuration file to serve your store.

Now that the configuration file is in place we need to enable it by creating a symlink from sites-enabled with the following command:

sudo ln -s /etc/nginx/sites-available/magento.conf /etc/nginx/sites-enabled/

Then reload NGINX:

sudo systemctl reload nginx.service

NGINX should now be serving your store. You should visit your store in a browser to check that it is working normally. If your site does not render correctly after the migration it is quite likely because of stale cache and indexed data. Flushing the cache and re-indexing the site with these two commands will resolve this issue:

sudo php /path/to/magento2/bin/magento cache:flush
sudo php /path/to/magento2/bin/magento indexer:reindex

The site should now be working normally. You should visit both the home page and the admin pages to ensure they are working as expected. The following command will disable maintenance mode and resume normal site operation:

sudo php /path/to/magento2/bin/magento maintenance:disable

Your Magento instance is now migrated to using NGINX as its webserver.

Migrating An HTTPS Magento Instance

In this step, we will configure NGINX to serve an HTTPS enabled Magento instance. NGINX works like Apache in that it has two directories /etc/nginx/sites-available and /etc/nginx/sites-enabled that contain the website configuration. We will first place the configuration file into /etc/nginx/sites-available and then create a symlink to that file in /etc/nginx/sites-enabled which will enable NGINX to start serving your store.

We will create and edit the site configuration file using a text editor. I will use nano throughout this guide but you can use whichever you are most comfortable with:

sudo nano /etc/nginx/sites-available/magento.conf

The following file will serve an HTTPS site. The first server block will automatically redirect any visitors arriving on HTTP to the HTTPS site:

server {
    listen 80;
    server_name ;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name ;

    ssl on;
    ssl_certificate /path/to/fullchain.pem;
    ssl_certificate_key /path/to/privkey.pem;

    set $MAGE_ROOT /path/to/magento2;
    include /path/to/magento2/nginx.conf.sample;
}

upstream fastcgi_backend {
        server  unix:/run/php/php7.1-fpm.sock;
}

The above configuration assumes that you are using PHP 7.1 (the default) on your server. If you are using a different version then you will need to edit the line server unix:/var/run/php/php7.1-fpm.sock; to match the version of PHP and PHP-FPM you are using. If you run:

ls /var/run/php/

You will be able to see the name of the PHP-FPM socket that you need to use.

The following line:

include /path/to/magento2/nginx.conf.sample;

causes NGINX to load additional configuration contained in the file /path/to/magento2/nginx.conf.sample. This file is supplied by Magento and contains very important additional configuration such as blocking access to confidential files, setting compression, etc. If you don’t have a copy of this file you can download one from these locations:

You will need to include this file for this NGINX configuration file to serve your store.

Now that the configuration file is in place we need to enable it by creating a symlink from sites-enabled with the following command:

sudo ln -s /etc/nginx/sites-available/magento.conf /etc/nginx/sites-enabled/

Then reload NGINX:

sudo systemctl reload nginx.service

NGINX should now be serving your store. You should visit your store in a browser to check that it is working normally. If your site does not render correctly after the migration it is quite likely because of stale cache and indexed data. Flushing the cache and re-indexing the site with these two commands will resolve this issue:

sudo php /path/to/magento2/bin/magento cache:flush
sudo php /path/to/magento2/bin/magento indexer:reindex

The site should now be working normally. You should visit both the home page and the admin pages to ensure they are working as expected. The following command will disable maintenance mode and resume normal site operation:

sudo php /path/to/magento2/bin/magento maintenance:disable

Conclusion

Your site should now be running normally and taking advantage of the increased performance and reduced resource requirements of NGINX. If you need additional information on configuration and tuning NGINX their documentation can be found on their website here.

The post Migrating a Magento Installation from Apache2 to NGINX on Ubuntu 18.04 LTS appeared first on Low End Box.

How to Replace Apache with NGINX on Ubuntu 18.04

NGINX is the modern web server founded by computer software engineer Igor Sysoev in the year 2004. NGINX is used by the most busiest and high traffic website. NGINX works out of box with the most major web stacks including LEMP (Linux, NGINX, MySQL, PHP) stack. This tutorial assumes that your website is hosted with the Apache web server and you want to migrate to NGINX . The process of migration includes replacing the Apache web server with the NGINX without loosing the website data with less downtime.

Why Replace Apache with NGINX ?

There are several reasons to replace Apache with NGINX are:

  • NGINX is the fastest web server that supports concurrent connections and supports high traffic website load.
  • NGINX consumes less RAM and CPU compared to Apache and it is resource friendly.
  • NGINX improves performance of website by supporting inbuilt cache system for faster access for website static contents like Images, CSS, JavaScript, etc.

What is the major difference between Apache and NGINX?

These are the major difference between Apache and NGINX are:

  • The main configuration files for Apache and NGINX are located at /etc/apache2/apache2.conf and /etc/nginx/nginx.conf respectively.
  • NGINX uses server block but Apache uses virtual host.
  • NGINX and Apache both are using same default root directory /var/www/html.
  • NGINX has inbuilt cache system but Apache don’t have any inbuilt cache system.

Pre Requirements

Before starting the tutorial you will need:

  • You will need a Ubuntu 18.04 VPS with minimum 1GB of RAM for smooth operations.
  • The Apache web server must be previously installed on your VPS.

Step-1: Remove the Apache Web Server

Before installing the NGINX you will remove the Apache web server to avoids conflict between them.

First of all you will stop the Apache service before removing the Apache web server. This enables us to remove the Apache without any issues.

$ sudo systemctl stop apache2

After stopping the Apache you will remove the startup Apache entries from systemctl. This enables us to remove the startup entries so that Apache services won’t be automatically started during boot time.

$ sudo systemctl disable apache2

When Apache services are successfully stopped and startup entries are also remove then it time to remove the Apache web server packages from the system.

$ sudo apt remove apache2

Above command will remove only apache2 packages on but Apache related dependencies are kept on with system. So it is essential to remove those unwanted dependencies to free your space. This can be run by given command.

$ sudo apt autoremove

Now, the Apache web server has successfully removed. The installation of NGINX is described in the next step.

Step-2: Install the NGINX Web Server

Let’s begin with the installation of NGINX on Ubuntu. The Ubuntu default repository contains all the packages of NGINX. Installation is straight forward so you have to install it without any hassle using apt package manager.

First remove and flush the old apt repository cache then update the repository to load latest packages information and perform a full upgrade to upgrade all the installed packages.

$ sudo apt clean all && sudo apt update && sudo apt dist-upgrade

After updating the repository it is the right time to install the updated NGINX packages.

$ sudo apt install nginx

When NGINX has successfully installed then Let’s begin with the next step that will guide you firewall configuration for NGINX web server.

Step-3: Configure UFW Firewall

The NGINX web server requires HTTP Port that is Port No. 80 and HTTPS port that is Port No. 443 to successfully work with firewall. So it is essential to keep this port open for that purpose so that NGINX works flawlessly. The UFW (Unified Firewall) is the default firewall for Ubuntu 18.04 Linux distribution. Hence, you will add firewall rules to allow HTTP and HTTPS ports.

By Default there is no rules are added to UFW firewall so it is so easy to add those rules. You are required to add the HTTP and HTTPS port rules to UFW firewall this can be done by simple commands. The NGINX Full rules contains both the HTTP and HTTP ports and this will allow these ports to be kept open by the UFW firewall.

$ sudo ufw allow "Nginx Full"

After adding the firewall rules its time to check the rules which had been added or updated using these rules using status command.

$ sudo ufw status

The above command show given sample output.

Status: active

To Action From
-- ------ ----
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
22/tcp LIMIT Anywhere
Nginx Full ALLOW Anywhere
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
22/tcp (v6) LIMIT Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)

Above output shows that you have successfully added the firewall rules and you ready to move forward to configure the NGINX web server that is described in next step.

Step-4: Understanding the Configuration File of NGINX Web Server Compared to Apache Web Server

The configuration of NGINX are almost same like Apache web server but the structure and syntax is different referred to configuration files. This Difference between the can be understand by the given sample configuration file of Apache and NGINX

Sample Apache Configuration file is located at /etc/apache2/sites-available/example.com.conf

ServerName example.com
ServerAlias www.example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/html/



Require all granted
AllowOverride None

Sample NGINX Configuration file is located at /etc/nginx/sites-available/example.com.conf

server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;

location / {
try_files $uri $uri/ =404;
}
}

If you look carefully from the both the configuration files you will find that Apache configuration file are expressed in virtual host and NGINX configuration file are expressed in server block. After understanding the difference between the configuration file of Apache and NGINX. Now, you ready to configure the rest of the NGINX configuration files that is described in next step.

Step-5: Configure NGINX Web Server

The NGINX has same capabilities like Apache web server but it has faster support of concurrent connections. The configuration file of NGINX uses the server blocks in configuration. You have to configure it wheres the same location of document root where all your static web assets like HTML, CSS, JavaScript and Images are stored.

Note: In this guide we throughout assume that your document root is /var/www/html and default domain name is example.com

In Ubuntu, the NGINX Server Blocks are located at sites-available and sites-enabled directory inside the NGINX configuration directory. You will edit the server blocks files located in /etc/nginx/sites-available/ and you will be create one for enabling the server blocks for your domain. This method is highly recommended because it allows you to host more than one website and at different domains and files locations on your Ubuntu.

$ sudo nano /etc/nginx/sites-available/example.com.conf

Add the given lines and don’t forget to replace example.com and www.example.com with your base domain name and subdomain to enable server blocks for NGINX.

server { 
listen 80; 
server_name example.com www.example.com; 
root /var/www/html; 

location / { 
try_files $uri $uri/ =404; 
} 
}

When you will completely add all of these lines then hit Ctrl + O to save and Ctrl + X to exit from nano text editor.

In NGINX server blocks configuration files you will create the symbolic link using soft links between sites-available and sites-enabled directory. Soft links allows you whenever you will make changes to server blocks configuration file located in sites-available directory and it will immediately replicated to sites-enabled directory.

$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf

You will check for correct syntax and to avoid any error present in NGINX configuration files. This command will also tells you where you have errors are present.

$ sudo nginx -t

When all syntax is correct then it will shows you Syntax OK as a output. If any thing goes wrong please re-check NGINX server blocks files. When all steps are completed then restart the services for making changes in the effect.

$ sudo systemctl restart nginx

After all things are ready then you will change the permission for default NGINX user www-data to enable read, write and execute permissions for default web root directory.

$ sudo chown www-data:www-data /var/www/html

To verify whether the www-data user and groups are owned the default web root directory by running the given long listing command

$ ll /var/www/html

After running this command the output shows www-data user and group is owned by the default web root directory /var/www/html . This means default NGINX user www-data will able to read, write and execute the default web root directory.

Conclusion

Lastly, you have successfully replaced the Apache to NGINX. Now you will ready to use the NGINX for your web property to enable fast access to web assets and low memory foot-printing. In the end, the NGINX can be used for various proposes and it can be used for both static and dynamic websites. For more information regarding the NGINX refer the man pages available in Ubuntu.

The post How to Replace Apache with NGINX on Ubuntu 18.04 appeared first on Low End Box.