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
In this guide, we will walk through the process of migrating an existing Magento installation from Apache to NGINX on Ubuntu 18.04 LTS.
In order to follow this guide you will need the following:
In order to begin this guide you need to log into your server as a non-root sudo enabled user.
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.
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.
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.
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
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.
One of the most critical gaps in traditional Large Language Models (LLMs) is that they…
Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…
What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…
Wouldn’t it be wonderful to wake up one day with a desire to explore AI…
Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…
Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…