Overview
Installing Magento on your Ubuntu system requires the installation of some basic technologies such as the facility of a cloud provider (to say, DigitalOcean, AWS, GCP, etc.), Nginx Server, PHP, Varnish, HAProxy, Redis and Firewall (ufw since it is Ubuntu). It takes almost half-day time in order to install all these technologies with proper configuration (so as to install Magento) on your Ubuntu system. But by using a single Terraform shell-script, you can install the whole set up within 7 to 10 mins with minimal human interaction.
What is Magento?
Magento, an Adobe Company is the most trusted and popular e-commerce platform in trend. Holds an unparalleled record when comes to boosting the small and medium businesses as its free Community Edition is the most active developers’ community in the field. Being titled as one of the ‘Leaders’ of the B2B e-commerce, it is the 3rd most popular in the top 10k sites and 5th most popular in India under the e-commerce category according to the BuiltWith internet services company’s survey.
What is Terraform?
Terraform is an open-source infrastructure as a code software developed to provision (such as to build, modify and improve) a data centre infrastructure. It also helps to version the infrastructure securely and efficiently. The other instances which the Terraform manages include; storage, networking, DNS entries, SaaS feature, etc. The planning phase in the Terraform generates an execution plan which works towards building the desired infrastructure. Further, the automation facility of Terraform reduces manual work.
Why do we go with Terraform for Magento installation?
Magento is a popular e-commerce platform used by loads of people across the world, at ADOL we encourage allotting minimal time for Magento installation with effective server configuration using Terraform.
- Reduces the installation time
- Completely automated
- Less-error prone
- Minimal human interaction
- Simple and convenient
What is remote-exec provisioner?
Before diving into the remote-execution provisioner, we need to know what is Provisioner in the first place. Provisioner is something that helps to execute the script or shell commands on a machine (both local and remote machines) in the process of creating and deleting a resource.
The remote-exec provisioner is a kind of provisioner that is used to invoke the created scripts on a remote machine. For this to be done, the remote provisioner must be connected with the resource via SSH.
Preconditions
- To install Terraform with the latest version
- Cloud Provider authentication
- Passwordless SSH keys
- Magneto requirements include; Nginx server, PHP, MySQL and Varnish
Note: Need to know more about the Terraform installation, provider authentication and creating SSH keys steps, visit our blog post on, How to Install and Configure Terraform on Ubuntu 18.04.
Let’s install Magento using Terraform
Step: 1
Before starting the installation steps of Magento with Terraform, you need to know about some configuration files.
terraform.tfvars
This is the file used to set up the values for a personal access token, ssh_fingerprint, private and public keys. Once stored, you can access it from anywhere and at any time you need. It is in this file you store the static values.
provider.tf
You use this file to declare the variables and get user inputs. Once you declared the variable in this file, you will get real-time user input and able to pass the configuration files wherever you need.
To know more, www.terraform.io/docs/configuration/variables.html
testing.tf
This is the file used to set the Droplet requirements such as image, droplet region, ram, storage and CPU cores. Further, it is used to establish a connection with a remote server using a connection attribute. Using the Provisioner attribute, it is possible to upload multiple files on a remote server. Since we are going to install Magento, you need more files. To know more about the provisioner, go to, www.terraform.io/docs/provisioners/index.html
additional.sh
This is the script file used to execute the additional commands on the remote server. Now, execute the files in the remote server which you had uploaded in the previous step. For example, if you install Nginx on a remote server, a mere installation is not enough, you need to restart, stop and carry out several other configurations too. Thus, to execute a shell file or commands you can use this file.
It is possible to execute those commands in the sample.tf file itself but a proper infrastructure and identification is necessary, hence a file called additional.sh has been created.
Step: 2
For installing Magento on a remote server using Terraform, some technologies are required:
- Nginx server
- PHP with extensions
- MySQL
- Varnish
We are going to install the above-mentioned things with Magento using Terraform. The following user inputs are necessary to install Magento. It is possible to install Magento without user inputs. Since the below-provided values are dynamic, you need to rely on the user inputs for this section.
#Droplet inputs Droplet_size Droplet_location Domain_Name Server name Host
#Database inputs dbname dbuser dbpass
#Magento inputs baseurl dbhost admin_firstname admin_lastname admin_email admin_user admin_password Backend_frontname
Note:
- Host: your web root directory name ( /var/www/example.com/public_html)
- baseurl: it is the Magento base URL (http://example.com/)
- dbhost: localhost
- admin_user: (first name + last name)
- Backend_frontname: back-end URL (http://sample.com/admin)
Step: 3
Let’s start the configuration. Configure the terraform.tfvars file and save the static values which you are going to use. This is applicable only when you retain the same token.
Open the file called terraform.tfvars
sudo nano terraform.tfvars
do_token = "213af4c4652737ef1fa24bb11f4638517d729551ca284b12658bb28307b506a4" ssh_fingerprint = "89:ff:cb:c1:20:97:f8:fe:68:77:72:ce:fe:2a:cf:b7" pub_key = "~/.ssh/id_rsa.pub" pvt_key = "~/.ssh/id_rsa"
Step: 4
Open the below file and declare the necessary variables for Magento installation as per your requirements.
sudo nano provider.tf
#Declaration for static files variable "do_token" {} variable "pub_key" {} variable "pvt_key" {} variable "ssh_fingerprint" {}
#Declaration for droplets variable "droplet_size" {} variable "droplet_location" {} variable "domain_Name" {}
#Declaration for database variable "dbname" {} variable "dbuser" {} variable "dbpass" {}
#Declaration for Magento variable "baseurl" {} variable "dbhost" {} variable "admin_firstname" {} variable "admin_lastname" {} variable "admin_email" {} variable "admin_user" {} variable "admin_password" {} variable "backend_frontname" {} provider "digitalocean" { token = "${var.do_token}" }
Step: 5
This is the file used to execute additional commands. Here, in this file, we are going to install the required PHP extensions and Magento extraction as per your requirements. You can also install other technologies in this file.
Note: It is possible to use these commands for our main configuration file but for identification and code formatting purpose, we used a separate file.
sudo nano additional.sh
#!/bin/bash #remove default file in sites-enabled for creating our server block cd /etc/nginx/sites-enabled sudo rm -rf default #Magento installation on remote directory echo "let’s start magento extraction" sudo rm -rf /var/www/$1/public_html/index.html cd /tmp/ cp magento.tar.gz /var/www/$1/public_html cd /var/www/$1/public_html sudo tar xvfz magento.tar.gz -C ./ sudo chmod -R 777 var pub app generated echo "magento extracted successfully in our web root directory" #Required php extensions for running Magento website sudo apt-get install -y php7.2-bcmath sudo apt-get install -y php7.2-cgi sudo apt-get install -y php7.2-cli sudo apt-get install -y php7.2-curl sudo apt-get install -y php7.2-fpm sudo apt-get install -y php7.2-gd sudo apt-get install -y php7.2-imap sudo apt-get install -y php7.2-intl sudo apt-get install -y php7.2-json sudo apt-get install -y php7.2-mbstring sudo apt-get install -y php7.2-mcrypt sudo apt-get install -y php7.2-mysql sudo apt-get install -y php7.2-opcache sudo apt-get install -y php7.2-pgsql sudo apt-get install -y php7.2-snmp sudo apt-get install -y php7.2-soap sudo apt-get install -y php7.2-xsl sudo apt-get install -y php7.2-xml sudo apt-get install -y php7.2-zip echo "Successfully installed the php extensions"