In this exercise, we will deploy a simple web application using a Platform as a Service (PaaS) provider. This hands-on activity will help you understand the deployment process and the benefits of using PaaS for web applications.

Objectives

  • Understand the steps involved in deploying a web application using a PaaS provider.
  • Gain practical experience with a popular PaaS platform.
  • Learn how to manage and scale a web application on a PaaS platform.

Prerequisites

  • Basic understanding of web applications.
  • Familiarity with Git and command-line interface (CLI).
  • An account with a PaaS provider (e.g., Heroku, AWS Elastic Beanstalk, Google App Engine).

Step-by-Step Guide

Step 1: Set Up Your Development Environment

  1. Install Git: Ensure that Git is installed on your machine. You can download it from git-scm.com.

  2. Install Heroku CLI: For this exercise, we will use Heroku as our PaaS provider. Download and install the Heroku CLI from Heroku CLI.

Step 2: Create a Simple Web Application

We will create a simple Node.js application for this exercise. Follow the steps below:

  1. Initialize a New Node.js Project:

    mkdir my-web-app
    cd my-web-app
    npm init -y
    
  2. Install Express:

    npm install express
    
  3. Create index.js File:

    const express = require('express');
    const app = express();
    const port = process.env.PORT || 3000;
    
    app.get('/', (req, res) => {
        res.send('Hello, World!');
    });
    
    app.listen(port, () => {
        console.log(`App running on port ${port}`);
    });
    
  4. Update package.json: Add a start script in the scripts section of your package.json file:

    "scripts": {
        "start": "node index.js"
    }
    

Step 3: Deploy the Application to Heroku

  1. Log In to Heroku:

    heroku login
    
  2. Create a New Heroku App:

    heroku create
    
  3. Initialize a Git Repository:

    git init
    git add .
    git commit -m "Initial commit"
    
  4. Deploy to Heroku:

    git push heroku master
    
  5. Open the Deployed Application:

    heroku open
    

Step 4: Manage and Scale Your Application

  1. View Logs:

    heroku logs --tail
    
  2. Scale the Application:

    heroku ps:scale web=1
    

Practical Exercise: Deploying a Web Application

Exercise Instructions

  1. Create a New Web Application:

    • Follow the steps in the guide to create a simple Node.js web application.
    • Ensure your application runs locally before proceeding to deployment.
  2. Deploy the Application to Heroku:

    • Use the Heroku CLI to create a new app and deploy your web application.
    • Verify that your application is accessible via the Heroku URL.
  3. Manage and Scale the Application:

    • Use Heroku CLI commands to view logs and scale your application.
    • Experiment with scaling the application up and down.

Exercise Solution

  1. Creating the Web Application:

    • Follow the code snippets and commands provided in the guide to set up your Node.js application.
  2. Deploying to Heroku:

    • Ensure you have committed your code to a Git repository.
    • Use git push heroku master to deploy your application.
    • Open the application using heroku open to verify deployment.
  3. Managing and Scaling:

    • Use heroku logs --tail to monitor your application logs.
    • Scale your application using heroku ps:scale web=1.

Common Mistakes and Tips

  • Missing Dependencies: Ensure all dependencies are listed in package.json and installed using npm install.
  • Port Configuration: Use process.env.PORT to allow Heroku to assign the port dynamically.
  • Git Configuration: Ensure your local repository is correctly set up and connected to Heroku.

Conclusion

In this exercise, you have successfully deployed a simple web application using Heroku, a popular PaaS provider. You have learned how to set up a development environment, create a web application, deploy it to a PaaS platform, and manage and scale the application. This practical experience will be valuable as you explore more complex applications and other PaaS providers.

© Copyright 2024. All rights reserved