Magento E-Commerce Platform installation with Apache2 and Let's Encrypt on Ubuntu 20.04

Magento is a free and open-source e-commerce web application that allows you to create a fully functional eCommerce store in minutes. It is written in PHP and combines powerful features with flexibility and a user-friendly interface. It is one of the most popular solutions for self-hosted online stores due to its simplicity and powerful admin panel. It comes with a rich set of features including, Site Management, SEO, Catalog Management, Product and Catalog Browsing, Order Management, Checkout, Promotions and Conversion Tools, and much more.

In this tutorial, we will show you how to install the Magento E-commerce platform with Apache and Let's Encrypt SSL on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04 with 4 GB of RAM.
  • A valid domain name pointed with your server.
  • A root password is configured on your server.

Install LAMP Server

Magento runs on the webserver, written in PHP, and uses MariaDB as a database. So you will need to install a LAMP stack in your server.

First, install the Apache web server and MariaDB server with the following command:

apt-get install apache2 mariadb-server mariadb-client -y

The latest version of Magento is only compatible with PHP 7.1.3+ and 7.2.x. So you will need to install the supported PHP versions with required extensions in your server.

By default, Ubuntu 20.04 ships with PHP version 7.4. So you will need to add the Ondrej PPA in your system in order to install other PHP versions.

You can add the Ondrej PHP PPA with the following command:

apt-get install software-properties-common -y
add-apt-repository ppa:ondrej/php

Next, update the repository and install PHP with other required extensions using the following command:

apt-get install php7.2 libapache2-mod-php7.2 php7.2-bcmath php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-intl php7.2-mysql php7.2-cli php7.2-ldap php7.2-zip php7.2-curl wget curl unzip -y

Once you are finished, you can proceed to the next step.

Configure MariaDB Database

By default, the MariaDB is not secured. So it is a good idea to secure and set the MariaDB root password. You can do it with the following command:

mysql_secure_installation

Answer all the questions as shown below:

Enter current password for root (enter for none): 
Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

Once the MariaDB is secured, log in to MariaDB shell:

mysql -u root -p

Provide your MariaDB root password then create a database and user for Magento:

MariaDB [(none)]> CREATE DATABASE magentodb;
MariaDB [(none)]> CREATE USER 'magento'@'localhost' IDENTIFIED BY 'password';

Next, grant all the privileges to Magento database with the following command:

MariaDB [(none)]> GRANT ALL ON magentodb.* TO 'magento'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

Next, flush the privileges and exit from the MariaDB shell using the following command:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Once you are done, you can proceed to the next step.

Download Magento

At the time of writing this tutorial, the latest version of Magento is 2.3.5. You can download it from the Magento official download page.

Once downloaded, extract the downloaded file to the Apache web root directory with the following command:

mkdir /var/www/html/magento
tar -xvjf magento-ce* -C /var/www/html/magento/

Next, give proper ownership and permission to the magento directory:

chown -R www-data:www-data /var/www/html/magento/
chmod -R 755 /var/www/html/magento/

Once you are done, you can proceed to the next step.

Configure Apache for Magento

Next, create a new Apache virtual host configuration file to serve the Magento website.

nano /etc/apache2/sites-available/magento.conf

Add the following lines:

<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot /var/www/html/magento/
     ServerName magento.linuxbuz.com
     <Directory /var/www/html/magento/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>
     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file then enable the Magento virtual host and Apache rewrite module with the following command:

a2ensite magento.conf
a2enmod rewrite

Finally, restart the Apache service to implement the changes:

systemctl restart apache2

At this point, the Apache web server is configured to serve Magento.

Secure Magento with Let's Encrypt SSL

It is always a good idea to secure your website with Let's Encrypt free SSL. First, install the Certbot client in your server to download and configure Let's Encrypt SSL for your website.

apt-get install certbot python3-certbot-apache -y

Once the Certbot is installed, run the following command to download and install Let's Encrypt SSL for your website:

certbot --apache -d magento.linuxbuz.com

You will be asked to provide your valid email and accept the term of service as shown below:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for magento.linuxbuz.com
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/magento-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/magento-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/magento-le-ssl.conf

Next, you will be asked to choose whether or not to redirect HTTP traffic to HTTPS:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Type 2 and hit Enter to finish the installation.

Redirecting vhost in /etc/apache2/sites-enabled/magento.conf to ssl vhost in /etc/apache2/sites-available/magento-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://magento.linuxbuz.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=magento.linuxbuz.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/magento.linuxbuz.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/magento.linuxbuz.com/privkey.pem
   Your cert will expire on 2020-08-11. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Access Magento Website

At this point, your Magento website is secured with Let's Encrypt SSL.

Now, open your web browser and type the URL https://magento.linuxbuz.com. You will be redirected to the Magento web-based installation wizard:

Magento setup

Click on the Agree and Setup Magento button. You should see the Magento Readiness screen:

Readiness check

Click on the Start Readiness Check button. Once the Readiness check has been completed, you should see the following screen:

All prerequisites are met

Click on the Next button. You should see the Database setup screen:

Configure the database

Provide your Magento database name, database username, password and click on the Next button. You should see the Magento web configuration wizard:

web configuration

Provide your Magento Store and Admin address, Enable HTTPS and click on the Next button. You should see the Store customization screen:

Custom store settings

Set your preferred Time Zone, Currency, Language and click on the Next button. You should see the admin user creation screen:

Create an admin user

Provide your admin username, email, password and click on the Next button. You should see the following screen:

Install now

Click on the Install Now button to start the installation. Once the installation has been completed successfully, you should see the following screen:

Installation successful

Click on the Magento admin address. You should see the Magento admin page:

Magento Login

Provide your Magento admin username, password and click on the Sign in button. You should see the Magento dashboard in the following screen:

Magento Dashboard

You can also access the Magento store using the URL https://magento.linuxbuz.com. You should see the following screen:

Magento start page

Conclusion

Congratulations! you have successfully installed Magento with Let's Encrypt SSL on Ubuntu 20.04. You can now deploy your own online store easily. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

0 Comment(s)

Add comment