Sep 30, 2022

Customized a Node.js App in Multipass

This is a short tutorial to:

  • Create an Ubuntu VM with cloud-init file
  • Install a running Node.js app with a Nginx as the front-end proxy


Cloud-init is a widely used approach to customize a Linux VM as it boots for the first time. It can be used to install packages and write files, or to configure users and security hardening. 

Cloud-init also works across distributions which means it automatically uses the native package management tool for the distro you select.

#cloud-config
package_upgrade: true
packages:
  - nginx
  - nodejs
  - npm
write_files:
  - owner: www-data:www-data
    path: /etc/nginx/sites-available/default
    content: |
      server {
        listen 80;
        location / {
          proxy_pass http://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection keep-alive;
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
      }
  - owner: webadm:webadm
    path: /home/webadm/myapp/index.js
    content: |
      var express = require('express')
      var app = express()
      var os = require('os');
      app.get('/', function (req, res) {
        res.send('Hello World from host ' + os.hostname() + '!')
      })
      app.listen(3000, function () {
        console.log('Hello world app listening on port 3000!')
      })
runcmd:
  - service nginx restart
  - cd "/home/webadm/myapp"
  - npm init
  - npm install express -y
  - nodejs index.js


Save the YAML as webadmin.yaml and then start creating the VM with Multipass as below:

multipass -n nodejs3k --cloud-init webadmin.yaml

multipass info nodejs3k

Last, open the URL to http://nodejs3k_ip_addr with your browser.


Links: