Magento on Azure: Automating Your E-commerce Platform with Azure DevOps

July 12, 2022by Dhawal

Introduction:

Deploying Magento on Azure is an effective way to harness cloud infrastructure for e-commerce needs, offering flexibility, scalability, and robust performance. With Azure DevOps CI/CD pipelines, you can automate deployments to both Azure Virtual Machines (VMs) and Azure Kubernetes Service (AKS), allowing for streamlined updates, reduced manual intervention, and scalable operations. In this guide, we’ll walk through the steps for deploying Magento on an Azure VM as well as on AKS, covering the pipeline configuration in Azure DevOps for both environments.

Step 1: Set Up Your Azure DevOps Account

Start by ensuring access to Azure DevOps. Sign up for free with your Microsoft or GitHub credentials if you haven’t already.

Step 2: Create an Organization and Project in Azure DevOps

To get started, create an organization in Azure DevOps:

  1. Go to the Azure DevOps portal and click on “New Organization”.

  2. Configure your organization and set up a new project titled “Magento-Azure-Deployment”. This project will host your CI/CD pipelines, repositories, and configurations.

Step 3: Set Up a Repository for Magento Code

If you don’t already have a repository, create or import one for Magento:

  1. From your project, go to “Repos” and create a new repository, naming it “Magento-Repo”.

  2. Push your Magento source code into this repository.

  3. Alternatively, if you’re migrating from another Git-based service (like GitHub), import your existing Magento repository.

Deploying Magento on Azure VM

Let’s start with steps to deploy Magento on an Azure Virtual Machine (VM).

Step 4: Create an Azure Virtual Machine

 

  1. In the Azure portal, navigate to Create a Resource and search for Virtual Machine.

  2. Select Ubuntu or CentOS as the OS for Magento.

  3. Configure the VM with appropriate resources (e.g., 4vCPUs, 8GB RAM) for Magento’s requirements.

  4. Once the VM is set up, SSH into it to verify connectivity and install necessary dependencies, like PHP, MySQL, and Nginx (or Apache).

Step 5: Create a CI/CD Pipeline for Azure VM Deployment

Now, set up a pipeline in Azure DevOps for deploying Magento to the VM:

  1. Go to Pipelines in Azure DevOps and click “Create Pipeline”.

  2. Select your code repository (e.g., Magento-Repo).

  3. Choose the Starter Pipeline template to create a basic azure-pipelines.yml file.

  4. Modify the azure-pipelines.yml file as follows:

 

trigger:
  - main  # Replace with your branch name

pool:
  vmImage: 'ubuntu-latest'

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.x'
      addToPath: true

  # Install dependencies on VM
  - script: |
      sudo apt-get update
      sudo apt-get install -y php php-fpm php-mysql nginx mysql-server git unzip
    displayName: 'Install Dependencies on VM'

  # Deploy files to Azure VM using SSH
  - task: CopyFilesOverSSH@0
    inputs:
      sshEndpoint: 'MagentoAzureVM'  # Pre-configured service connection
      sourceFolder: '$(Build.SourcesDirectory)/magento'
      targetFolder: '/var/www/html/'  # Directory on Azure VM

  # Run Magento installation
  - script: |
      ssh user@AzureVM 'cd /var/www/html && php bin/magento setup:install \
      --base-url=http://your-domain.com \
      --db-host=localhost \
      --db-name=magento \
      --db-user=root \
      --db-password=yourpassword \
      --admin-firstname=admin \
      --admin-lastname=user \
      --admin-email=admin@example.com \
      --admin-user=admin \
      --admin-password=admin123 \
      --language=en_US \
      --currency=USD \
      --timezone=America/Chicago \
      --use-rewrites=1'
    displayName: 'Deploy Magento on Azure VM'

Step 6: Set Up SSH Service Connection

In Azure DevOps, navigate to Project Settings > Service Connections. Create a new SSH connection for your VM, providing the VM’s IP, port, username, and private key. This enables the pipeline to securely connect to the Azure VM for deployments.

Deploying Magento on Azure Kubernetes Service (AKS)

For a scalable, containerized deployment, you can deploy Magento on Azure Kubernetes Service (AKS).

Step 7: Create an AKS Cluster

 

  1. In the Azure portal, go to Create a Resource and search for Kubernetes Service.

  2. Configure the cluster with a name, resource group, and appropriate VM node sizes.

  3. Enable Container Monitoring and configure Node Pools for optimal scaling.

Step 8: Prepare Magento Docker Image

Magento needs to be containerized for AKS deployment. If you don’t already have a Docker image for Magento, you can create one:

  1. Create a Dockerfile for Magento that includes PHP, Nginx, MySQL client, and other required services.

  2. Build and push your Docker image to Azure Container Registry (ACR) or Docker Hub.

For example:

# Dockerfile for Magento
FROM php:7.4-fpm

# Install necessary dependencies
RUN apt-get update && apt-get install -y \
    nginx \
    php-mysql \
    php-intl \
    php-xsl \
    php-soap \
    php-zip \
    php-mbstring

# Set up Magento installation and other commands

Step 9: Create a CI/CD Pipeline for AKS Deployment

Now, configure the Azure DevOps pipeline to deploy Magento on AKS.

  1. Go to Pipelines and create a new pipeline.

  2. Use or modify your azure-pipelines.yml file to include deployment steps for AKS.

 

trigger:
  - main

pool:
  vmImage: 'ubuntu-latest'

steps:
  # Build and push the Magento Docker image to ACR
  - task: Docker@2
    inputs:
      containerRegistry: 'myContainerRegistryServiceConnection'
      repository: 'my-magento'
      command: 'buildAndPush'
      Dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
      tags: |
        $(Build.BuildId)

  # Deploy to AKS using Kubernetes manifest
  - task: KubernetesManifest@0
    inputs:
      action: 'deploy'
      kubernetesServiceConnection: 'myAKSConnection'
      namespace: 'default'
      manifests: '$(Pipeline.Workspace)/manifests/magento-deployment.yaml'
      containers: |
        my-magento:$(Build.BuildId)

Step 10: Trigger and Run the Pipeline

Once you have everything configured, go back to Pipelines and run your pipeline by clicking “Run Pipeline”.

The pipeline will start by installing dependencies, transferring Magento files to your Azure VM or AKS cluster, and executing the necessary commands to deploy Magento.

Step 11: Monitor and Manage Deployment

Azure DevOps provides detailed logs for each step of the pipeline. You can monitor the progress of your deployment directly from the Pipelines dashboard.

Once the deployment is complete, you can navigate to your VM’s public IP or domain (for VM deployment) or the Kubernetes service endpoint (for AKS deployment) to access your Magento site.

Step 12: Post-Deployment Configurations

After Magento is deployed, consider performing these additional configurations:

  1. Configure SSL for secure access to your Magento store.

  2. Set up Magento cron jobs for tasks like reindexing and cache cleaning.

  3. Monitor resource usage (CPU, memory) on the VM or AKS cluster to ensure optimal performance.

 

Conclusion

By automating Magento deployment on both Azure VM and AKS using Azure DevOps CI/CD, you’re prepared for any scaling requirements. Whether you need a simple VM setup or a highly scalable Kubernetes deployment, this guide enables efficient, consistent deployments with minimal manual effort. This approach not only streamlines your workflow but ensures your Magento e-commerce platform is always ready for growth and performance optimization.