Deploying a React Application on a Linux VM via Azure DevOps

I’ll walk through the process of deploying a React application to a Linux virtual machine using Azure DevOps. At the end of this process, your React application will be automatically deployed to your Linux VM every time you push to the main branch.

Overview:

The purpose of this step-by-step guide is to demonstrate a complete DevOps workflow for deploying a React application on an Azure Linux Virtual Machine using Nginx as a web server and then integrating it with Azure DevOps for continuous integration and deployment (CI/CD).

This workflow teaches:

  • Cloud Deployment—How to Host a Web App on Azure.
  • DevOps Automation—How to Use Azure DevOps for CI/CD.
  • Web Server Configuration—Setting up Nginx for a React app.
  • How to change the script and run it locally.
  • End-to-End Development—From local development to production deployment.

Step-by-Step Guide:

Step 1: Create an Azure Linux Virtual Machine

What is it?
A virtual machine (VM) is like a computer in the cloud. We’ll use it to host our React app using Nginx, a web server.

How to do it:

  1. Go to the Azure Portal and sign in.
  1. In the search bar, type “Virtual machines” and click it.
Screenshot – Step 1: Create an Azure Linux Virtual Machine: In the search bar, type "Virtual machines" and click it.
  1. Click + Create – Azure Virtual Machine.
Screenshot – Step 1: Create an Azure Linux Virtual Machine: Click + Create - Azure Virtual Machine.
  1. Fill in the required fields:
  • Subscription: Choose your subscription.
  • Resource Group: Create a new one (e.g., MyReactAppGroup).
  • Virtual Machine Name: e.g., ReactVM.
  • Region: Choose a region close to you.
  • Image: Select Ubuntu 20.04 LTS.
  • Size: Choose something like Standard B1s (good for dev/testing).
  • Authentication type: Choose Password or SSH (more secure).
  • Username/Password: Set a username and password.
  1. Leave most other settings as default.
  1. Click Review + Create > Create.
  1. Wait for it to deploy (takes 1–2 minutes).
Screenshot – Step 1: Create an Azure Linux Virtual Machine: Wait for it to deploy (takes 1–2 minutes).

Step 2: Connect to the Virtual Machine

What is it?
You’ll connect to your Azure VM with VS Code. (Visual Studio Code)

What You Need:

ToolDescription
VS CodeInstalled on your local machine
Remote – SSH ExtensionA VS Code extension
SSH access to your VMIP address, username, and password or SSH key

How to do it:

  1. Once the VM is running, go to the VM Overview page.
  1. Copy the public IP address (you’ll need it in the next step).
Screenshot – Step 2: Connect to the Virtual Machine: Copy the public IP address (you'll need it in the next step).
2.1: Install VS Code

If not already installed:
Download from https://code.visualstudio.com/.

2.2: Install Remote – SSH Extension
  1. Download and install VS Code.
  1. Open VS Code.
  1. Click on the Extensions icon. (left sidebar)
  1. Search for Remote—SSH.
  1. Click Install.

This lets you connect to remote Linux machines like your Azure VM.

2.3: Set Up the SSH Connection

You need to know:

  1. Your VM’s public IP.
  1. Your username. (e.g., azureuser)
  1. Your authentication method. (password or SSH key)
2.4: Set Up the VM at Vs Code
  1. Open VS Code.
Screenshot – Step 2: Connect to the Virtual Machine: Open VS Code.
  1. Press Ctrl+Shift+P in VS Code, type Remote-SSH: Connect to Host…
Screenshot – Step 2: Connect to the Virtual Machine: Press Ctrl+Shift+P in VS Code, type Remote-SSH: Connect to Host…
  1. Click + Add New SSH Host. (e.g., ssh azureuser@)
Screenshot – Step 2: Connect to the Virtual Machine: Click + Add New SSH Host. (e.g., ssh azureuser@)
Screenshot – Step 2: Connect to the Virtual Machine: Click + Add New SSH Host. (e.g., ssh azureuser@) (2)
  1. It will ask where to save the config; choose the default. (usually ~/.ssh/config)
Screenshot – Step 2: Connect to the Virtual Machine: It will ask where to save the config; choose the default. (usually…
  1. On the right side, it will open a pop-up box. Click to Connect.
Screenshot – Step 2: Connect to the Virtual Machine: On the right side, it will open a pop-up box. Click to Connect.
  1. Ask the platform to choose Linux.
Screenshot – Step 2: Connect to the Virtual Machine: Ask the platform to choose Linux.
  1. It will try to connect—VS Code asks for your VM password.
Screenshot – Step 2: Connect to the Virtual Machine: It will try to connect—VS Code asks for your VM password.
  1. Now VM is connected successfully at VS Code.
  1. To check, open your terminal inside VS Code; you will see something like username@VM Name: ~$
Screenshot – Step 2: Connect to the Virtual Machine: To check, open your terminal inside VS Code; you will see something like…

Step 3: Install Necessary Software Inside the Vm

  1. Inside the VM, run this command:
sudo apt update.

sudo apt install nodejs npm nginx -y.
Screenshot – Step 3: Install Necessary Software Inside the Vm: Inside the VM, run this command
Screenshot – Step 3: Install Necessary Software Inside the Vm: Inside the VM, run this command (2)

This installs:

  • Node.js and npm: To build your React app.
  • Nginx: To serve the website.

Step 4: Create a sample React app

  1. Still inside your VM, run this command:
npx create-react-app myapp

Once run this command, It will ask if it’s ok to proceed; give yes (y) to proceed

Screenshot – Step 4: Create a sample React app: Once run this command, It will ask if it's ok to proceed; give yes (y) to…

Next, run this command:

cd myapp

npm run build
Screenshot – Step 4: Create a sample React app: Next, run this command

This creates a build folder with the static files for your app.

Step 5: Serve React app using NGINX

  1. Copy the React build to a public directory:

Run the command:

sudo cp -r build/ /var/www/myapp
  1. Create a new NGINX config file:

Run the command:

sudo nano /etc/nginx/sites-available/myapp
Screenshot – Step 5: Serve React app using NGINX: Run the command

It will open another Linux window; paste this script.

server {
    listen 80;
    server_name _;

    root /var/www/myapp;
    index index.html;

    location / {
        try_files $uri /index.html;
    }
}
Screenshot – Step 5: Serve React app using NGINX: It will open another Linux window; paste this script.

To save this script: Ctrl + O > Enter > Ctrl + X

  1. Enable the config and restart NGINX:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled

sudo nginx -t

sudo systemctl restart nginx
Screenshot – Step 5: Serve React app using NGINX: Enable the config and restart NGINX
  1. Now copy your VM IP and paste it in your browser. It will show the Nginx server.
Screenshot – Step 5: Serve React app using NGINX: Now copy your VM IP and paste it in your browser. It will show the Nginx server.
  1. Replace the default Nginx web page with your React app.
  1. Copy the React app build to the Nginx web root.

Make sure you’re in your Linux VM, inside your React app folder (usually ~/myapp). Run this command:

cd ~/myapp

npm run build
Screenshot – Step 5: Serve React app using NGINX: Make sure you’re in your Linux VM, inside your React app folder (usually…
  1. Copy the build folder contents to Nginx’s web root.
sudo rm -rf /var/www/html/*

sudo cp -r build/* /var/www/html/
  1. Restart the Nginx server.
sudo systemctl restart nginx
Screenshot – Step 5: Serve React app using NGINX: Restart the Nginx server.
  1. Refresh the browser, and now you can see the React app.
Screenshot – Step 5: Serve React app using NGINX: Refresh the browser, and now you can see the React app.

Step 6: How to change the script inside the VM and test it locally

  1. In VS Code, go to File > Open Folder.
Screenshot – Step 6: How to change the script inside the VM…: In VS Code, go to File > Open Folder.
  1. It will react to the folder path and name. Click it > Click OK.
Screenshot – Step 6: How to change the script inside the VM…: It will react to the folder path and name. Click it > Click OK.
Screenshot – Step 6: How to change the script inside the VM…: It will react to the folder path and name. Click it > Click OK. (2)
  1. It will ask for the VM password; put in the password and click Enter.
Screenshot – Step 6: How to change the script inside the VM…: It will ask for the VM password; put in the password and click Enter.
  1. Now you will see the React application previously created.
Screenshot – Step 6: How to change the script inside the VM…: Now you will see the React application previously created.
  1. Inside the src file, select the App.js and remove the old script.
Screenshot – Step 6: How to change the script inside the VM…: Inside the src file, select the App.js and remove the old script.
  1. Paste the script given below:
import React from 'react';

function App() {
  return (
    <div>
      <h1>Hello from React!</h1>
    </div>
  );
}

export default App
Screenshot – Step 6: How to change the script inside the VM…: Paste the script given below
  1. Once you’ve saved the file, again run this command:
npm run build

sudo cp -r build/ /var/www/myapp

sudo systemctl restart nginx

npm start
Screenshot – Step 6: How to change the script inside the VM…: Once you've saved the file, again run this command
Screenshot – Step 6: How to change the script inside the VM…: Once you've saved the file, again run this command (2)
Screenshot – Step 6: How to change the script inside the VM…: Once you've saved the file, again run this command (3)

Now run your React app locally in the VM. It will run at localhost:3000 inside the VM.

Step 7: Deploy to Azure DevOps

  1. Go to https://dev.azure.com.
  1. Click the organization; if not already, please create it.
  1. Create your project (e.g., Demo-Linux) > Click Create.
Screenshot – Step 7: Deploy to Azure DevOps: Create your project (e.g., Demo-Linux) > Click Create.
  1. Go to your project.
  1. Click Repos > Files
  1. Copy the URL; you will need this later.
Screenshot – Step 7: Deploy to Azure DevOps: Copy the URL; you will need this later.

Step 8: Set up the PAT (Personal Access Token)

What is a Personal Access Token (PAT)?

A Personal Access Token (PAT) in Azure DevOps is a security token that you can use as an alternative to a password when authenticating to Azure DevOps services.

If Azure asks for a username and password, the password is not your login password.

You need to generate a personal access token.

  1. To create the Personal Access Token (PAT)
  • Click your profile (top right corner) ? Security
Screenshot – Step 8: Set up the PAT (Personal Access Token): Click your profile (top right corner) ? Security
  • Click New Token
Screenshot – Step 8: Set up the PAT (Personal Access Token): Click New Token
  • Set:

Name: (e.g., Demo)

Scope: Code > Read & Write

  • Click Create
Screenshot – Step 8: Set up the PAT (Personal Access Token): Click Create
  • Copy the token (you won’t see it again)
  • Click the Close
Screenshot – Step 8: Set up the PAT (Personal Access Token): Click the Close

When Git asks for a password, paste the PAT

Step 9: Set up the Git in your VM

  1. Run these commands one-by-one:
git init

git add .

git remote add origin https://dev.azure.com/your-org/your-project/_git/your-repo

git branch -M main

git push -u origin main

Once you run this command, git push -u origin main, it will ask for a password. Don’t paste your Azure login password and VM password. Copy and paste your already created PAT.

Screenshot – Step 9: Set up the Git in your VM: Once you run this command, git push -u origin main, it will ask for a password.…
  1. Now successfully push into the repository.
Screenshot – Step 9: Set up the Git in your VM: Now successfully push into the repository.
Screenshot – Step 9: Set up the Git in your VM: Now successfully push into the repository. (2)

Step 10: Set Up SSH Connection to the VM

Before you can copy files to the VM, you must allow Azure DevOps to connect via SSH.

  1. In Azure DevOps, go to Project Settings (bottom left corner)
Screenshot – Step 10: Set Up SSH Connection to the VM: In Azure DevOps, go to Project Settings (bottom left corner)
  1. Click Service Connection.
Screenshot – Step 10: Set Up SSH Connection to the VM: Click Service Connection.
  1. Click Create service connection.
Screenshot – Step 10: Set Up SSH Connection to the VM: Click Create service connection.
  1. Search SSH > Choose SSH ? Click Next.
Screenshot – Step 10: Set Up SSH Connection to the VM: Search SSH > Choose SSH ? Click Next.
  1. Now fill in:
  • Host—Your VM’s public IP (e.g., 52.123.45.67)
  • Port – 22
  • Username—The username you used to create the VM
  • Password—Choose whichever you used to connect to the VM
  • Connection Name—e.g., Demo Service
Screenshot – Step 10: Set Up SSH Connection to the VM: Connection Name—e.g., Demo Service
  1. Now the service connection is created successfully.
Screenshot – Step 10: Set Up SSH Connection to the VM: Now the service connection is created successfully.

Step 11: Create the Release Pipeline

  1. Go to Azure DevOps ? Click the Repos.
  1. In the top right, Click set up build.
Screenshot – Step 11: Create the Release Pipeline: In the top right, Click set up build.
  1. Click the Starter pipeline and paste the YAML file given below:
Screenshot – Step 11: Create the Release Pipeline: Click the Starter pipeline and paste the YAML file given below
trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

variables:
  artifactName: 'reactbuild'
  vmUser: '20.80.82.189'
  sshServiceConnection: 'Linux'
  vmTargetPath: '/var/www/html'

stages:
  - stage: Build
    displayName: 'Build React App'
    jobs:
      - job: BuildJob
        displayName: 'Install & Build React'
        steps:
          - task: NodeTool@0
            inputs:
              versionSpec: '18.x'
            displayName: 'Install Node.js'

          - script: |
              npm install
              npm run build
            displayName: 'Build React App'

          - task: CopyFiles@2
            inputs:
              SourceFolder: '$(System.DefaultWorkingDirectory)/build'
              Contents: '**'
              TargetFolder: '$(Build.ArtifactStagingDirectory)'
            displayName: 'Prepare files for artifact'

          - task: PublishBuildArtifacts@1
            inputs:
              PathtoPublish: '$(Build.ArtifactStagingDirectory)'
              ArtifactName: 'reactbuild'
              publishLocation: 'Container'
            displayName: 'Publish Artifact'

  - stage: Deploy
    displayName: 'Deploy to Azure Linux VM'
    dependsOn: Build
    jobs:
      - deployment: DeployToVM
        environment: 'Production'
        strategy:
          runOnce:
            deploy:
              steps:
                - download: current
                  artifact: reactbuild  # ? FIXED: This ensures the correct artifact is downloaded

                - task: CopyFilesOverSSH@0
                  inputs:
                    sshEndpoint: 'Linux'
                    sourceFolder: '$(Pipeline.Workspace)/reactbuild'
                    contents: '**'
                    targetFolder: '/home/azureuser/reactdeploy'
                    readyTimeout: '20000'
                  displayName: 'Copy React Build to Linux VM'

                - task: SSH@0
                  inputs:
                    sshEndpoint: 'Linux'
                    runOptions: 'inline'
                    inline: |
                      sudo rm -rf /var/www/html/*
                      sudo cp -r /home/azureuser/reactdeploy/* /var/www/html/
                      sudo chown -R www-data:www-data /var/www/html
                      sudo systemctl restart nginx
                    readyTimeout: '20000'
                  displayName: 'Restart Nginx'

Please change the your VM Ip and Service connection name in the YAML file

  1. Click Save and run > Save and run.
Screenshot – Step 11: Create the Release Pipeline: Click Save and run > Save and run.
Screenshot – Step 11: Create the Release Pipeline: Click Save and run > Save and run. (2)
  1. Once Build is completed, it automatically deploys. Before deploying, it asks permission to give a permit option. It will take 2–3 minutes to deploy.
Screenshot – Step 11: Create the Release Pipeline: Once Build is completed, it automatically deploys. Before deploying, it asks…
  1. Now build and deploy is completed.
Screenshot – Step 11: Create the Release Pipeline: Now build and deploy is completed.
  1. Now go to the browser and enter the IP. It will show the React page again, not “Hello from React!”
Screenshot – Step 11: Create the Release Pipeline: Now go to the browser and enter the IP. It will show the React page again, not…

Step 12: To fix this “Hello from React!” (Push git commit)

  1. Go to VS Code, and inside the VM, follow these commands:
git add .

git commit -m "Updated homepage message"

git pull origin main –rebase

git push origin main

Now git commit is successfully completed.

Screenshot – Step 12: To fix this “Hello from React!” (Push…: Now git commit is successfully completed.
  1. Rerun the build and deploy.
  1. Go to the pipeline. At the top right, click the Run new button, and then click Run.
Screenshot – Step 12: To fix this “Hello from React!” (Push…: Go to the pipeline. At the top right, click the Run new button, and then click…
Screenshot – Step 12: To fix this “Hello from React!” (Push…: Go to the pipeline. At the top right, click the Run new button, and then click… (2)
  1. Now build and deploy is successfully completed.
Screenshot – Step 12: To fix this “Hello from React!” (Push…: Now build and deploy is successfully completed.
  1. Now refresh the browser; it will show “Hello from React!”
Screenshot – Step 12: To fix this “Hello from React!” (Push…: Now refresh the browser; it will show “Hello from React!”

Get new Microsoft 365 and Azure guides from Azure365Pro first in Google.

Kannakrishnan Arumugasamy
Kannakrishnan Arumugasamy
As a passionate and skilled Microsoft Cloud Engineer, I specialize in architecting, deploying, and managing cloud-based solutions on the Azure platform. With a background in data science and a deep interest in cloud technologies, I focus on building scalable, secure, and efficient cloud environments to support business transformation.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here