May 10, 2024 - DevOps

Are you prepared to elevate your Laravel application's deployment to new heights with an Azure VM and a simple yet robust CI/CD pipeline? You've come to the perfect resource. In this concise and straightforward guide, we'll outline each step, ensuring a seamless deployment process. Let's get started!

Step 1: Create a Virtual Server in Azure

First things first, log in to your Azure portal and navigate to the Virtual Machines section. Click on “Create” to initiate the process of creating a new virtual machine.

Step 2: Provide Inbound Port Access

During the creation process, ensure to provide inbound port access for ports 80, 443, and optionally 22 for SSH access. This step is crucial for allowing traffic to reach your VM.

Step 3: Set Up Azure DevOps Account

If you haven’t already, create an Azure DevOps account. Go to https://dev.azure.com. This platform will be instrumental in setting up our CI/CD pipeline. Once you’re logged in, proceed to the next step.

Step 4: Create a New Project

Within your Azure DevOps account, create a new project where you’ll manage your code repositories and pipelines.

Step 5: Configure Environment in Pipeline

Navigate to the “Environment” section within your pipeline settings. Create a new environment, specifying a name and selecting “Virtual Machine.” Choose Linux as the OS and copy the provided command.

Step 6: Connect VM to Azure Pipelines

Connect to your VM and access the superuser, use the command: sudo su. Before pasting the command provided by Azure Pipelines, make sure you’ve cloned your GitHub repository into your preferred directory on the VM. Here’s how you can do it:

git clone https://github.com/laravel/quickstart-basic

After cloning the repository, it’s essential to set permissions of the folder and it’s files using chmod -R 777 (folder name) command to allow which allows the folder to have necessary permissions to execure sudo commands without any difficulties.

Once you’ve cloned the repository, proceed to paste the command provided by Azure Pipelines. This command will establish the connection between your VM and Azure Pipelines, enabling seamless integration and deployment.

Step 7: Install Required Dependencies

Since our Laravel app requires PHP 8.3, ensure to install the package and it’s required extensions on your VM.

Step 8: Create CI/CD Pipeline

Now, it’s time to set up your CI/CD pipeline. Navigate to your pipeline settings and create a new pipeline using your preferred repository (e.g., GitHub).

Step 9: Define Pipeline Workflow

Below is a simplified version of the pipeline YAML code:

trigger:
  - main

pool:
  vmImage: ubuntu-latest

variables:
  phpVersion: '8.3'

jobs:
  - deployment: VMDeploy
    displayName: Web deploy
    environment:
      name: 'demo' # Name of the environment in Azure DevOps
      resourceType: VirtualMachine # Specifies that deployment will happen on a virtual machine
      resourceName: 'demo-vm' # Name of the Azure VM where deployment will occur
    strategy:
      runOnce:
        deploy:
          steps:
            - script: |
                sudo bash -c ' # Start of the deployment script
                cd /home/sabin/laravelapp # Change directory to the Laravel app directory
                git pull # Pull the latest changes from the GitHub repository
                apt update # Update package lists for dependencies
                composer install # Install PHP dependencies using Composer
                php artisan migrate # Run database migrations to ensure database structure is up to date
                php artisan serve --port 8000 & --host 0.0.0.0 # Start the Laravel server (Optional: use of "--port" and "--host")
                '
              displayName: 'Continuous Integration and Deployment' # Display name for this deployment step

This YAML script defines a deployment job (VMDeploy) that will run on an Azure VM named ‘demo-vm’ and execute the deployment steps specified. With this setup, php artisan serve will start the Laravel server in the background, ensuring that the deployment workflow remains functional under any conditions.

Optional: You can also specify the port and hostname using the ‘–port’ and ‘–host’ options according to your preference.

Congratulations! You’ve successfully set up a CI/CD pipeline for deploying your Laravel app on an Azure VM. With automation in place, you can focus more on developing your application while ensuring seamless deployments. Happy coding!