Skip to main content

CI/CD pipeline for my PHP web application

Created
Active
Viewed 391 times
5 replies
2

How can I set up a CI/CD pipeline for my PHP web application, which is hosted in Azure Repos? I'm using an Ubuntu VM as the deployment environment and would like guidance on configuring the pipeline to ensure it's properly set up. Could you provide instructions or recommendations on how to achieve this?

I am utilizing Azure DevOps to set up the CI/CD pipeline.

Previously, I have worked with ASP.NET, but I am new to PHP web application deployment.

5 replies

Sorted by:
79085633
6
  • 62.3k
  • 16
  • 79
  • 92

Deploying a PHP app is usually just a case of copying all the code files to the destination folder on the webserver. And maybe executing any database change scripts if that's relevant. If it's a first-time deployment there might be some webserver configuration to be done too, but it's unclear if that's something you want your pipeline to do, or whether it's in the scope of your question.

Basically - think about what you would do manually to deploy your application. What are the steps you would do? All you then have to do is automate them. It's a bit unclear precisely where you're stuck, from your brief description.

79151352
0
  • 198.6k
  • 55
  • 450
  • 857

The deployment of a PHP app is well described in the posting of user ADyson.

For entering from the working tree into the CI/CD pipeline for a PHP app:

Add a composer configuration to your project (if you have not yet), then install the requirements (if any), dump the autoloader (if any) and finally run composer archive to build the distribution.

If you have any dist tests run them on the distribution. Then if successful and if you have any integrations of the application, deploy the distribution through all stages, and then finally if everything remains green, deploy the application to production.

79247290
4

Here’s a step-by-step guide to setting up a CI/CD pipeline in Azure DevOps for your PHP web application deployed on an Ubuntu VM:


Step 1: Prepare the Ubuntu VM

  1. Ensure VM accessibility:

    • Verify the VM is accessible via SSH from Azure DevOps.

    • If the VM has a public IP, ensure that SSH is enabled. For private IPs, configure the network to allow Azure DevOps to connect.

  2. Install dependencies: On the Ubuntu VM, install necessary software for PHP and web server (e.g., Apache/Nginx):

    sudo apt update sudo apt install -y apache2 php php-cli unzip
    
  3. Install Git: Ensure Git is installed to pull code during the deployment.

    sudo apt install -y git
    
  4. Set up deployment directory: Create a directory where the PHP application will be deployed:

    sudo mkdir -p /var/www/myapp sudo chown -R $USER:$USER /var/www/myapp
    

Step 2: Create a Service Connection in Azure DevOps

  1. Navigate to Azure DevOps Project > Project Settings > Service Connections.

  2. Create a new service connection of type SSH.

  3. Provide:

    • Host name/IP address: Public IP of your Ubuntu VM.

    • Port: Default SSH port (usually 22).

    • Username: User for the VM.

    • Authentication: Use a private key or password for the connection.

  4. Save the service connection.


Step 3: Define the CI Pipeline

  1. Go to your Azure DevOps project and navigate to Pipelines > Create Pipeline.

  2. Choose your repository hosted in Azure Repos.

  3. Select Starter pipeline and define the following YAML (adjust to your application):

    trigger:
      branches:
        include:
          - main  # Adjust to your branch
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
      - task: UsePHP@1
        inputs:
          version: '7.x'  # Adjust for your PHP version
    
      - script: |
          composer install --no-dev
          php -l index.php  # Example linting step
        displayName: 'Install dependencies and lint PHP'
    
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(System.DefaultWorkingDirectory)'
          ArtifactName: 'drop'
          publishLocation: 'Container'
    
    
  4. Save and run the pipeline. This pipeline lints and installs dependencies and prepares an artifact for deployment.


Step 4: Define the CD Pipeline

  1. Create a new pipeline for deployment:

    • Pipeline trigger: Set the pipeline to trigger after the CI pipeline completes successfully.

    • Use the following YAML:

    trigger:
      branches:
        include:
          - main  # Adjust for your branch
    
    pool:
      vmImage: 'ubuntu-latest'
    
    variables:
      artifactName: 'drop'
      deploymentDir: '/var/www/myapp'
    
    steps:
      - download: current
        artifact: $(artifactName)
    
      - task: SSH@0
        inputs:
          sshEndpoint: ''
          runOptions: 'commands'
          commands: |
            sudo rm -rf $(deploymentDir)/*
            sudo unzip $(Pipeline.Workspace)/$(artifactName).zip -d $(deploymentDir)
            sudo chown -R www-data:www-data $(deploymentDir)
            sudo systemctl restart apache2
    

    Adjust the SSH@0 task's inputs:

    • Replace `` with the name of your service connection.

    • Ensure $(Pipeline.Workspace) contains the correct path to your artifacts.

  2. Save and run the pipeline.


Step 5: Test the CI/CD Pipeline

  1. Push changes to your main branch (or the branch defined in the pipeline trigger).

  2. Observe the CI pipeline performing the build and packaging steps.

  3. Verify the CD pipeline deploys the application to the Ubuntu VM.


Additional Recommendations

  • PHP Unit Tests: Add a test phase in the CI pipeline to run automated tests:

    - script: |
        vendor/bin/phpunit --testdox
      displayName: 'Run PHP Unit Tests'
    
  • Environment Variables: Store sensitive values in Azure DevOps pipeline variables or variable groups.

  • Security:

    • Use SSH key-based authentication for secure access to the VM.

    • Set up firewalls to restrict SSH access to trusted IPs.

  • Monitoring: Implement logging and monitoring for your application on the Ubuntu VM.

This setup ensures automated deployment of your PHP web application with a robust CI/CD process.

79359905
0

Well, I think there should be a possibility to map your PHP application to GitHub and connect to Azure at the same time. from there you can outsource your CI/CD pipeline to GitHub which can also automatically deploy your code to azure.

I can look for some resources and share with you.