# Set up a MEAN-based web application on a new Azure Linux virtual machine

A MEAN application stack is one that is based on MongoDB, Express, AngularJS and Node.js. This is an attractive technology stack, because it is very performant, cost-effective and can run almost anywhere. The technologies in a MEAN-based application can all be free of cost as they are open source. However, you can also choose to pay to get support contracts for these technologies.

Creating a MEAN-based application and running it in Azure is surprisingly simple. Let's take a look at how to do it:

# 1. Create a Virtual Machine in Azure with Linux

We're going to run the MEAN-based application on a VM in Azure that runs Ubuntu. This will show that this type of application can run on many types of Operating Systems, including Ubuntu.

  1. We'll create the VM in Azure using the Azure Cloud Shell. You can open it in the Azure portal, or open it full-screen by going to https://shell.azure.com. Alternatively, you can also do all of in the Azure CLI locally, if you have it installed (opens new window)
  2. In the Cloud Shell, execute the following commands to create the VM and open up the public port so that we can SSH into it
az vm create \
    --resource-group AzureTipsAndTricks \
    --name tipsandtrickVM \
    --image UbuntuLTS \
    --admin-username azuremichael \
    --admin-password 'my1stFakePassword!' \
az vm open-port --port 3300 --resource-group AzureTipsAndTricks --name tipsandtrickVM
az vm open-port --port 80 --resource-group AzureTipsAndTricks --name tipsandtrickVM

When the VM is created, you'll see something like the image below. Copy the publicIpAddress value. We'll need that to connect to the VM.

(Results of VM creation in the Azure Cloud Shell)

  1. You can now SSH into the VM. You need the username, the public IP address and your password. Execute the SSH command like in the image below

(SSH into the VM in the Azure Cloud Shell)

# 2. Install the MEAN stack on the Virtual Machine

We're now ready to install all of the software that we need in the MEAN stack. This means that we need to install MongoDB and Node.js. the E in MEAN stands for Express, which is a web server framework for Node.js that handles request routing. We'll get this as a Node.js package when we create the application later on. The same goes for the A in MEAN; AngularJS. This is not something that we need to install, but rather a JavaScript framework that we will get by referencing it in the application. So let's install MongoDB and Node.js on the VM.

  1. Use the console of the VM (We work from the SSH command line from the last section)
  2. First, we need to import the encryption key for the MongoDB repository. This allows the Ubuntu package manager to verify that the packages you install are coming from MongoDB Inc
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E52529D4
  1. Now, we need to register the MongoDB repository, so that the package manager can find it
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
  1. Update the package database so that it includes MongoDB with this command
sudo apt-get update
  1. Install MongoDB with this command
sudo apt install mongodb-org
  1. And start the MongoDB service, so that we can use it in our application
sudo service mongod start

That's it! We have a MongoDB database sever running on the VM. Let's continue with Node.js.

  1. In the SSH command prompt, first, register the Node.js repository with the package manager, so that we can find and install it
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
  1. Install Node.js with the following command
sudo apt-get install nodejs
  1. And check that you are now running Node.js with this command
node -v

# 3. Create the application

Now for the easy part: creating the application. We'll use a simple Node.js application that manages books that we store in MongoDB. We'll get the application files from a public GitHub repository (opens new window).

  1. If you are still SSH-ed into the VM, exit to the Cloud Shell by typing exit
  2. Get the application files by executing git clone:
git clone https://github.com/MicrosoftDocs/mslearn-build-a-web-app-with-mean-on-a-linux-vm

# 4. Run the application

We now have a complete application that we can run. Let's copy it to the VM and run it.

  1. Copy the files to the VM. You'll use your VM username, public IP address and password again. You can use this command:
scp -r mslearn-build-a-web-app-with-mean-on-a-linux-vm azuremichael@
  1. SSH into the VM again by using this command
ssh azuremichael@
  1. Navigate to the books folder in the application files by executing cd ~/mslearn-build-a-web-app-with-mean-on-a-linux-vm/Books
  2. Execute npm install to install the packages that we need to run the application
  3. Finally, run the application by executing the command below
sudo node server.js

The application is now running and listening for requests on the public IP address of the VM. Navigate to this address in a browser to open the application. Try it out by adding and deleting a couple of books.

(Run the application in a browser)

# Conclusion

MEAN-based applications are extremely portable and relatively easy to create. As you've seen, you don't need a lot of plumbing. Just a simple HTML + JavaScript application with routes and a database. And a MEAN-based app is very portable as we've seen. You can create some files and simply copy them over and run them. This makes the MEAN stack very powerful. Go and check it out!