DevOps Engineering Operate and automate hosts16 / 21

SECTION 05 · 30 MIN READ & EXPLORE

Provision the application host

Install a Node application using an explicit Bash provisioning script.

HANDS-ON LAB

Provision a disposable host

Inspect and run the setup script on your lab VM, then test the Node service in the foreground.

Your mission

  • Inspect and run the setup script on your lab VM, then test the Node service in the foreground.

Download and extract the archive. Work inside 09-sysadmin; upload its contents to the root of a GitLab project when a pipeline is involved.

Application Provisioning (The Setup)

Objective: We need to set up the environment. Instead of running commands one by one, we will use a Bash Provisioning Script. This simulates how tools like Ansible work under the hood.

Create a file named provision.sh inside your container and paste the following content.

#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status

echo "--- 1. Updating Repositories ---"
apt-get update -y

echo "--- 2. Installing Nginx and Node.js ---"
apt-get install -y nginx nodejs curl

echo "--- 3. Setting up the Application Directory ---"
mkdir -p /opt/node-app

echo "--- 4. Creating the Node.js Server File ---"
cat > /opt/node-app/server.js <<EOF
const http = require('http');
const fs = require('fs');
const path = require('path');

const LOG_FILE = path.join(__dirname, 'access.log');
const PORT = 3000;

const server = http.createServer((req, res) => {
    // 1. Log the request to a file
    const logEntry = \`\${new Date().toISOString()} - Request received from \${req.socket.remoteAddress}\n\`;
    
    fs.appendFile(LOG_FILE, logEntry, (err) => {
        if (err) console.error('Error writing to log:', err);
    });

    // 2. Send Response
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello from the SysAdmin Lab!\n');
});

server.listen(PORT, () => {
    console.log(\`Server running on port \${PORT}\`);
});
EOF

echo "--- Setup Complete! ---"
echo "You can test the app manually with: node /opt/node-app/server.js"

Run the Setup:

chmod +x provision.sh
./provision.sh

How to know it worked

  • Node responds on localhost:3000.
  • You can identify every system change made by the script.
Download lesson files

Explore the files

setup.sh

#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status

echo "--- 1. Updating Repositories ---"
apt-get update -y

echo "--- 2. Installing Nginx and Node.js ---"
apt-get install -y nginx nodejs curl

echo "--- 3. Setting up the Application Directory ---"
mkdir -p /opt/node-app
# Set permissions so our specific user can write to it later
# (For simplicity in this script, we keep root, but in prod use a dedicated user)

echo "--- 4. Creating the Node.js Server File ---"
cat >/opt/node-app/server.js <<EOF
const http = require('http');
const fs = require('fs');
const path = require('path');

const LOG_FILE = path.join(__dirname, 'access.log');
const PORT = 3000;

const server = http.createServer((req, res) => {
    // 1. Log the request to a file
    const logEntry = \`\${new Date().toISOString()} - Request received from \${req.socket.remoteAddress}\n\`;
    
    fs.appendFile(LOG_FILE, logEntry, (err) => {
        if (err) console.error('Error writing to log:', err);
    });

    // 2. Send Response
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello from the SysAdmin Lab!\n');
});

server.listen(PORT, () => {
    console.log(\`Server running on port \${PORT}\`);
});
EOF

echo "--- Setup Complete! ---"
echo "You can test the app manually with: node /opt/node-app/server.js"
CHECK YOUR UNDERSTANDING

Pause and predict

What happens if you rerun this setup script after changing server.js manually?

Try your own explanation first. These notes stay on this page and are not saved or graded.

Show explanation

The script writes server.js again. Reproducibility requires deciding which source owns the configuration.

A LITTLE CLEARER?

Take a moment to explain the idea in your own words.

Progress is saved in this browser.

DevOps Engineering

Course overview ↗

01Introduction to DevOps

  1. What is DevOps?20m
  2. DevOps Roles20m