🔥 Checkout our new Azure Developer page at azure.com/developer (opens new window).
📺 Watch the video : How to build and run a MEAN-based application in Azure (opens new window).
💡 Learn more : Sample MEAN application GitHub repository (opens new window)
# 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.
- 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)
- 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!' \ --generate-ssh-keys 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)
- 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
- Use the console of the VM (We work from the SSH command line from the last section)
- 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
- 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
- Update the package database so that it includes MongoDB with this command
sudo apt-get update
- Install MongoDB with this command
sudo apt install mongodb-org
- 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.
- 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 -
- Install Node.js with the following command
sudo apt-get install nodejs
- And check that you are now running Node.js with this command
# 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).
- If you are still SSH-ed into the VM, exit to the Cloud Shell by typing exit
- 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.
- 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 firstname.lastname@example.org:./
- SSH into the VM again by using this command
- Navigate to the books folder in the application files by executing cd ~/mslearn-build-a-web-app-with-mean-on-a-linux-vm/Books
- Execute npm install to install the packages that we need to run the application
- 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)