4. Connect to Linux virtual machine using SSH
In this section of the tutorial, use SSH in a terminal to connect to your virtual machine. SSH is a common tool provided with many modern shells, including the Azure Cloud Shell.
Get your IP address
Get your IP address using the Azure CLI command, az vm list-ip-addresses:
az vm list-ip-addresses \ --resource-group rg-demo-vm-eastus \ --name demo-vmThe results include your public IP address:
[ { "virtualMachine": { "name": "demo-vm", "network": { "privateIpAddresses": [ "YOUR-VM-PRIVATE-IP" ], "publicIpAddresses": [ { "id": "/subscriptions/YOUR-SUBSCRIPTION-ID/resourceGroups/YOUR-RESOURCE-GROUP-NAME/providers/Microsoft.Network/publicIPAddresses/YOUR-RESOURCE-NAME-ip", "ipAddress": "YOUR-VM-PUBLIC-IP", "ipAllocationMethod": "Static", "name": "YOUR-RESOURCE-NAME-ip", "resourceGroup": "YOUR-RESOURCE-GROUP-NAME", "zone": "1" } ] }, "resourceGroup": "YOUR-RESOURCE-GROUP-NAME" } } ]
Connect with SSH and change web app
Use the same terminal or shell window as with previous steps.
Connect to your remote virtual machine with the following command.
Replace
YOUR-VM-PUBLIC-IPwith your own virtual machine's public IP.ssh azureuser@YOUR-VM-PUBLIC-IPThis process assumes that your SSH client can find your SSH keys, created as part of your VM creation and placed on your local machine. If you are asked if you want to continue connecting, answer
yes. When the connection is complete, the terminal prompt should change to indicate the remote virtual machine.If you are asked if you are sure you want to connect, answer
yoryesto continue.Use the following command to understand where you are on the virtual machine. You should be at the azureuser root:
/home/azureuser.pwdThe response should be
/home/azureuser.Your web app is in the subdirectory,
myapp. Change to themyappdirectory and list the contents:cd myapp && ls -lYou should see contents like, representing the GitHub repository cloned into the virtual machine and the npm package files:
-rw-r--r-- 1 root root 891 Nov 11 20:23 cloud-init-github.txt -rw-r--r-- 1 root root 1347 Nov 11 20:23 index-logging.js -rw-r--r-- 1 root root 282 Nov 11 20:23 index.js drwxr-xr-x 190 root root 4096 Nov 11 20:23 node_modules -rw-r--r-- 1 root root 84115 Nov 11 20:23 package-lock.json -rw-r--r-- 1 root root 329 Nov 11 20:23 package.json -rw-r--r-- 1 root root 697 Nov 11 20:23 readme.md
Install Monitoring SDK
In the SSH terminal which is connected to your virtual machine, install the Azure SDK client library for Application Insights.
sudo npm install --save applicationinsightsWait until the command completes before continuing.
Add Monitoring instrumentation key
In the SSH terminal, which is connected to your virtual machine, use the Nano editor to open the
package.jsonfile.sudo nano package.jsonAdd a
APPINSIGHTS_INSTRUMENTATIONKEYenvironment variable to the beginning of your Start script. In the following example, replaceREPLACE-WITH-YOUR-KEYwith your instrumentation key value."start": "APPINSIGHTS_INSTRUMENTATIONKEY=REPLACE-WITH-YOUR-KEY pm2 start index.js --watch --log /var/log/pm2.log"Still in the SSH terminal, save the file in the Nano editor with control + X.
In the Nano editor, enter Y to save, when prompted.
In the Nano editor, accept the file name when prompted.
Stop PM2, which is a production process manager for Node.js applications, with the following commands:
sudo npm run-script stopThe Azure client library is now in your node_modules directory and the key is passed into the app as an environment variable. The next step is to add the required code to
index.js.Restart the app with PM2 to pick up the next environment variable.
sudo npm start