Deploying PHP on Azure with Azure DevOps

In this article, we will see how to deploy a PHP 8.0.2 application on Azure App Service and configure continuous integration with Azure DevOps. We will also add integration with SonarCloud to ensure that the code meets quality standards.

1. What is App Service
2. What is Azure DevOps
3. SonarCloud
4. Steps to create an App Service for PHP on Azure
5. Steps for deployment with Azure DevOps
6. Integration with SonarCloud
7. Bibliography

App Service y Azure DevOps

O2O Digital Strategy - Despliegue de PHP en Azure con AzureDevops

1.What is App Service

App Service is a managed service for deploying and scaling .NET, .NET Core, Node.js, Java, Python, or PHP applications in containers or running on Windows or Linux.
O2O Digital Strategy - Despliegue de PHP en Azure con AzureDevops

2. What is Azure DevOps

Azure DevOps is a project management platform. It provides an integrated set of services and tools to manage software projects, from planning and development through testing and deployment. It helps developers implement continuous integration.
O2O Digital Strategy - Despliegue de PHP en Azure con AzureDevops

3. SonarCloud

SonarCloud is a cloud-based static code analysis service that helps developers improve the quality and security of their code. It offers automatic code analysis in 25 different programming languages, including Java, Python, JavaScript, C/C++, and PHP.

4. Steps to create an App Service for PHP in Azure

I. Create a New Resource

The first step is to access the Azure marketplace, search for “App Service,” and select “Web App” from the available options.

O2O Digital Strategy - Despliegue de PHP en Azure con AzureDevops
Vista del marketplace.

II. Basic Configuration

Complete the basic information for our AppService: subscription, resource group, runtime, region, plan…

O2O Digital Strategy - Despliegue de PHP en Azure con AzureDevops
Vista del panel de configuración.

III. Database Creation (Optional)

If needed, you can also create the database during the AppService creation process.

IV. Creation and Deployment of the AppService

Once the AppService is created and deployed in Azure, we will have our working environment ready to deploy our code.

5. Steps for deployment with Azure DevOps:

I. Create a New Project

O2O Digital Strategy - Despliegue de PHP en Azure con AzureDevops
Modal de creación de nuevo proyecto.

II. Clone the Repository

Once the project and initial repository are created, the next step is to clone it to a local environment.

III. Create a Build Pipeline

A pipeline is an automated process that compiles the web application’s source code and generates a package. In the creation wizard, we will first select the source of our repository. In our case, Azure Repos Git:

O2O Digital Strategy - Despliegue de PHP en Azure con AzureDevops
Ventana de creación de un Pipeline.

IV. Pipeline Configuration

The corresponding language in this case is “PHP as Linux Web App on Azure:

O2O Digital Strategy - Despliegue de PHP en Azure con AzureDevops
Ventana de creación de un Pipeline.

V. Subscription Selection

Next, we select the subscription where we want to deploy and the Web App.

VI. YAML Customization

A YAML file with the pipeline configuration will be generated, which we can customize according to our needs.

Example of YAML where PHP 8.2.0 and the necessary modules are installed on the instance raised by Azure DevOps, the build is performed, certificates are downloaded, and the upload to the App Service is done:

# PHP as Linux Web App on Azure
# Build, package and deploy your PHP project to Azure Linux Web App.
# Add steps that run tests and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/php trigger:
– develop variables:
# Azure Resource Manager connection created during pipeline creation
azureSubscription: ‘subscription-id’
# Web app name
webAppName: ‘webAppName’# Agent VM image name
vmImageName: ‘ubuntu-22.04’# Environment name
environmentName: ‘environmentName’# Root folder under which your composer.json file is available.
rootFolder: $(System.DefaultWorkingDirectory)

stages:
– stage: Build

displayName: Build stage
variables:
phpVersion: ‘8.2.0’

jobs:
– job: BuildJob

pool:
vmImage: $(vmImageName)

steps:
– script:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.2 -y
sudo apt install php8.2-cli php8.2-common php8.2-imap php8.2-redis php8.2-xml php8.2-zip php8.2-mbstring
sudo update-alternatives –set php /usr/bin/php$(phpVersion)
sudo update-alternatives –set phar /usr/bin/phar$(phpVersion)
sudo update-alternatives –set phpdbg /usr/bin/phpdbg$(phpVersion)
sudo update-alternatives –set php-cgi /usr/bin/php-cgi$(phpVersion)
sudo update-alternatives –set phar.phar /usr/bin/phar.phar$(phpVersion)
php -version

workingDirectory: $(rootFolder)
displayName: ‘Use PHP version $(phpVersion)’

– script: composer install –no-interaction –prefer-dist
workingDirectory: $(rootFolder)
displayName: ‘Composer install’

– task: DownloadSecureFile@1
name: jwtCertificatePrivate
displayName: ‘Download JWT private certificate’

inputs:
secureFile: ‘private.pem’
– task: DownloadSecureFile@1
name: jwtCertificatePublic
displayName: ‘Download JWT public certificate’

inputs:
secureFile: ‘public.pem’
– task: CopyFiles@2

inputs:
sourceFolder: “$(Agent.TempDirectory)”

contents: |
public.pem
private.pem
targetFolder: $(rootFolder)/config/jwt/
displayName: “Import JWT certificate”
– task: ArchiveFiles@2
displayName: ‘Archive files’

inputs:
rootFolderOrFile: ‘$(rootFolder)’
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true

– upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
displayName: ‘Upload package’
artifact: drop

– stage: Deploy
displayName: ‘Deploy Web App’
dependsOn: Build
condition: succeeded()

jobs:
– deployment: DeploymentJob

pool:
vmImage: $(vmImageName)
environment: $(environmentName)

strategy:
runOnce:
deploy:
steps:
– task: AzureWebApp@1
displayName: ‘Deploy Azure Web App : webAppName’

inputs:
azureSubscription: $(azureSubscription)
appName: $(webAppName)
package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip

VII. Pipeline Execution

Run the pipeline and check that it has been deployed correctly in our AppService.

Integration with SonarCloud

To perform the analysis with SonarCloud, we first need to configure the integration of SonarCloud in Azure DevOps. To do this, we will create a new task in the build phase of the Azure DevOps pipeline. The task should have the following configuration:

  1. Task Name: “Run SonarCloud Analysis”.
  2. Task Type: “SonarCloud”.
  3. SonarCloud Analysis Token: This token can be generated in SonarCloud under the “Tokens” section.
  4. SonarCloud Configuration: In this section, you must specify the connection parameters to SonarCloud, such as the server, project key, and access token.

Example configuration in YAML:

– task: SonarCloudPrepare@1inputs:

SonarCloud: ‘GT PS’

organization: ‘organization’

scannerMode: ‘CLI’

configMode: ‘manual’

cliProjectKey: ‘project-key’

cliProjectName: ‘project-Name’

cliSources: ‘.’

– task: SonarCloudAnalyze@1

– task: SonarCloudPublish@1

inputs:

pollingTimeoutSec: ‘300’

Once the task is configured, it will add the configuration to our corresponding pipeline YAML.

We run our pipeline, which will call the SonarCloud service and generate a report with the results of the code quality analysis.

In summary, with Azure DevOps and SonarCloud, we can deploy PHP applications easily and with the assurance of code quality standards.

Bibliography

https://learn.microsoft.com/es-es/azure/devops/user-guide/services?view=azure-devops

https://en.wikipedia.org/wiki/Microsoft_Azure

https://es.wikipedia.org/wiki/PHP

https://docs.sonarsource.com/sonarcloud/getting-started/azure-devops/

 

 

Compartir artículo