If, after configuring the domain on our Magento image, you want to secure your store with an SSL certificate, follow the steps below.
We will install Certbot to install a free certificate from Let’s Encrypt. To complete these steps, we will need to connect via SSH to the Magento server we created and configure it as explained below.
Getting started
If you have configured a domain, go back to Stores > Configuration > General > Web and modify the Base URLs (Secure) value to add your domain with HTTPS:
After saving, the site will load over HTTP. Now it is time to configure the web server to work with HTTPS.
Manual SSL certificate installation
First, we need to install CertBot:
# apt install certbot python3-certbot-nginx -y
Next, we need to edit the Nginx configuration file located at /etc/nginx/sites-enabled/default
# nano /etc/nginx/sites-enabled/default
We need to change this:
server_name ubuntu-2004-image;
To this (adding www is optional):
server_name midominio.es www.midominio.es;
Save the file and restart the Nginx service:
# systemctl restart nginx
Finally, generate the certificate with the following command:
# certbot --authenticator webroot -w /var/www/html/magento2/ --redirect --installer nginx -d midominio.es
If you added www, run this command:
# certbot --authenticator webroot -w /var/www/html/magento2/ --redirect --installer nginx -d midominio.es -d www.midominio.es
Automatic SSL certificate installation
To automate the entire process, we have created a script to run the installation with a single command. Use this script as follows:
# wget https://gitlab.com/clouding.io-tech.support/certbot-ssl-images/raw/master/magento2-nginx-ssl.sh
# chmod u+x magento2-nginx-ssl.sh
To generate the certificate:
# ./magento2-nginx-ssl.sh [domain] [e-mail]
For example:
# ./magento2-nginx-ssl.sh clouding.io no-reply@clouding.io
The email address must be active, and the domain must point to the server.