How to Install Magento 2 with Nginx and Letsencrypt on Ubuntu 18.04
Magento is a widely used Open Source e-commerce software and content management system for e-commerce websites based on the PHP Zend Framework. It uses MySQL or MariaDB as database backend. The Magento development has been started in 2008 by Varien.
In this tutorial, I will show you how to install Magento 2 with Nginx, PHP 7.1 FPM, and MySQL as the database. I will use ubuntu 18.04 (Bionic Beaver) as server operating system. If you don’t have a minimal server setup yet, please see this tutorial to get a proper base setup.
Prerequisites
- Ubuntu 18.04
- 2GB or more RAM memory
- Root privileges
What we will do
- Install Nginx on Ubuntu 18.04
- Install and Configure PHP-FPM 7.1
- Install and Configure MySQL Server
- Install and Configure Magento 2
- Install PHP Composer
- Download Magento 2
- Install Magento Components
- Generate SSL Letsencrypt
- Configure Nginx Virtual Host for Magento
- Magento Post-Installation
- Testing
Step 1 – Install Nginx on Ubuntu 18.04 LTS
In this tutorial, we will be using the Nginx web server for our Magento installation.
Log in to the server, update the repository, and upgrade all packages.
sudo apt update
sudo apt upgrade
Now install the Nginx web server using the apt command below.
sudo apt install nginx -y
After the installation is complete, start the Nginx service and enable it to launch every time at system boot.
systemctl start nginx
systemctl enable nginx
Nginx web server has been installed, check it using netstat command and make sure the HTTP port 80 is on the ‘LISTEN’ state. Another way is by using curl command as below.
netstat -plntu
curl -I localhost
Step 2 – Install and Configure PHP-FPM 7.1
After the Nginx web server installation, we will install PHP 7.1 on the server as Magento does not support PHP 7.2 yet. We will install PHP-FPM with all extensions needed by Magento 2.
List of PHP extensions needed for Magento 2 installation:
- bc-math
- ctype
- curl
- dom
- gd, ImageMagick 6.3.7 (or later) or both
- intl
- mbstring
- mcrypt
- hash
- openssl
- PDO/MySQL
- SimpleXML
- soap
- spl
- libxml
- xsl
- zip
- json
- iconv
For this guide, we will install PHP-FPM packages from the PPA repository. We will be using the ‘ondrej/php’ repository.
Install the ‘software-properties-common’ package and add the ‘ondrej/php’ repository using commands below.
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
Now install PHP-FPM 7.1 with all extensions needed.
sudo apt install php7.1-fpm php7.1-mcrypt php7.1-curl php7.1-cli php7.1-mysql php7.1-gd php7.1-xsl php7.1-json php7.1-intl php-pear php7.1-dev php7.1-common php7.1-mbstring php7.1-zip php7.1-soap php7.1-bcmath -y
After the installation is complete, check the PHP version and installed extensions using PHP commands.
php -v
php -me
Next, we will configure the php.ini file for the PHP-FPM and PHP-CLI.
Edit the php.ini files using vim.
vim /etc/php/7.1/fpm/php.ini
vim /etc/php/7.1/cli/php.ini
Change the value of those lines as below.
memory_limit = 512M max_execution_time = 180 zlib.output_compression = On
Save and exit.
Now restart the PHP-fpm service and enable it to launch every time at system boot.
systemctl restart php7.1-fpm
systemctl enable php7.1-fpm
The PHP-FPM 7.1 installation and configuration has been completed, check the service using netstat command.
netstat -pl | grep php
And you will get the PHP-fpm socks file as below.
Step 3 – Install and Configure MySQL Server
The Magento software requires MySQL 5.6.x, and the Magento 2.1.2 or later require the MySQL 5.7.x. In this tutorial, we will install latest MySQL server 5.8 on the Ubuntu 18.04 system.
Install MySQL 5.8 using the apt command below.
sudo apt install mysql-server mysql-client -y
After the installation is complete, start the MySQL service and enable it to launch every time at system boot.
systemctl start mysql
systemctl enable mysql
Now we will configure the MySQL root password using ‘mysql_secure_installation’ command.
mysql_secure_installation
In this MySQL 5.8 version, there is a security improvement for the MySQL password policy. You need to choose the password policy – 0 for the LOW policy, 1 for the MEDIUM policy, and 2 for a STRONG password policy.
For this guide, we will be using the ‘MEDIUM’ password policy, and it’s recommended to use the ‘STRONG’ password policy on the production server.
Choose number ‘1’ and press Enter, then type your new MySQL ‘root’ password.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
The MySQL root password has been set up.
Next, we will create a new database for our Magento installation. We will create a new database named ‘magentodb’ with user ‘magentouser’ and the password is ‘[email protected]#’.
Login to the MySQL shell using the root user.
mysql -u root -p
Now run MySQL queries below to create the database and user.
create database magentodb;
create user [email protected] identified by ‘[email protected]#’;
grant all privileges on magentodb.* to [email protected] identified by ‘[email protected]#’;
flush privileges;
The MySQL server installation and configuration for the Magento installation has been completed.
Step 4 – Install and Configure Magento 2
In this step, we will install Magento 2.2.4 latest version from Github repository. We will install the PHP composer for installing the Magento components, download Magento from Github repository, configure Nginx virtual host for Magento, and install Magento using the web-based post installation.
– Install PHP Composer
Install PHP Composer on ubuntu 18.04 using the apt command below.
sudo apt install composer -y
After the installation is complete, check the composer version installed on the system.
composer -V
The latest version PHP Composer has been installed.
– Download Magento 2
Go to the ‘/var/www’ directory and download the Magento archive source code from Github using wget command.
cd /var/www/
wget https://github.com/magento/magento2/archive/2.2.4.tar.gz
Now extract the Magento archive file and rename the directory to ‘magento2’.
tar -xf 2.2.4.tar.gz
mv magento2-2.2.4/ magento2/
The Magento source code has been downloaded, and the ‘/var/www/magento2’ directory will be the web root for the Magento site.
– Install Magento Components
Install Magento components using the PHP composer. Go to the ‘magento2’ directory and install all the PHP components needed by Magento using the ‘composer’ command.
cd /var/www/magento2
composer install -v
After the installation is complete, you will get the result as shown below.
– Generate SSL Letsencrypt
We will secure our Magento installation using SSL from Letsencrypt. Install the Letsencrypt using the apt command below.
sudo apt install letsencrypt -y
After the installation is complete, stop the nginx service.
systemctl stop nginx
Now generate the SSL certificates for the domain name using certbot command as below.
certbot certonly –standalone -d magento.hakase-labs.pw
Type your email address, accept the Letsencrypt TOS, then type ‘N’ for email sharing.
When it’s complete, you will get the result as below.
The Letsencrypt SSL certificate files have been generated to the ‘/etc/letsencrypt/live’ directory.
– Configure Nginx Virtual Host
Go to the ‘/etc/nginx/sites-available’ directory and create new virtual host file ‘magento’ using vim.
cd /etc/nginx/sites-available/
vim magento
Paste the following configuration there.
upstream fastcgi_backend { server unix:/run/php/php7.1-fpm.sock; } server { listen 80; listen [::]:80; server_name magento.hakase-labs.pw; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name magento.hakase-labs.pw; ssl on; ssl_certificate /etc/letsencrypt/live/magento.hakase-labs.pw/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/magento.hakase-labs.pw/privkey.pem; set $MAGE_ROOT /var/www/magento2; set $MAGE_MODE developer; include /var/www/magento2/nginx.conf.sample; }
Save the configuration and exit.
Now activate the virtual host by creating the symbolic link for the Magento virtual host file to the ‘sites-enabled’ directory.
ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/
Test nginx configuration file and make sure there is no error.
nginx -t
Now restart the PHP-FPM and Nginx service.
systemctl restart php7.1-fpm
systemctl restart nginx
And change the owner of Magento web-root directory to the ‘www-data’ user and group.
chown -R www-data:www-data /var/www/magento2/
The nginx virtual host for Magento has been added.
– Magento Post-Installation
Open the web browser and type the Magento URL. Mine is:
https://magento.hakase-labs.pw/
For the ‘Terms and Agreement’, click the ‘Agree and Setup Magento’ button.
Now check all system and server configuration for Magento ‘Readlines Check’, and make sure it ‘passes’ all system and configuration check as below.
Now click ‘Next’ button.
Database configuration for Magento – type all our database info and click ‘Next’.
For the web configuration, change the ‘store address’ to HTTPS, ‘Magento Admin Address’ with your admin path URL and uncheck the ‘Apache Rewrites’, because we’re using the Nginx web server.
Click the ‘Next’ button again.
Leave the ‘Customize Your Store’ as default and click ‘Next’.
Type your admin user and password, then click ‘Next’.
And click ‘Install Now’ button to install Magento.
And when installation is complete, you will get the ‘success’ result as below.
Now we need to disable write access for the ‘/var/www/magento2/app/etc’ directory. Run the command below.
sudo chmod -w /var/www/magento2/app/etc
Magento has been installed on to the Ubuntu 18.04 Bionic Beaver server.
Step 5 – Testing
Type the Magento URL home address and make sure you get the Magento default home page. Mine is: https://magento.hakase-labs.pw/
Now login to the Magento admin dashboard by visiting the URL defined during the installation ‘admin_hakase’. Mine is:
https://magento.hakase-labs.pw/admin_hakase/
Type the username and password, then click the ‘Sign in’ button.
And you will get the Magento admin dashboard as below.
Magento 2 has been installed on Ubuntu 18.04 with Nginx web server, MySQL database, and PHP-FPM 7.1.
Additional:
If you get an error about a missing Magento indexer cronjob, then you can solve it by generating the crontab script using the command below.:
cd /var/www/magento2
sudo -u www-data php bin/magento cron:install –force
Now check the list crontab script for the ‘www-data’ user.
crontab -u www-data -l