Deploying application on Linux Instance

Deploy applications on Linux

  1. We use git to clone the source code. First of all, install git with the following command:

    sudo dnf install git
    

    create vpc for linux instance

  2. Check successful git installation with command

    git version
    

    create vpc for linux instance

  3. Clone repository application code

    cd ~ec2-user
    git clone https://github.com/First-Cloud-Journey/000004-EC2.git
    

    create vpc for linux instance

  4. Go to the lab directory 000004-EC2 and check the files

    cd 000004-EC2
    

    create vpc for linux instance

    ls
    

    create vpc for linux instance

  5. NPM stands for Node package manager and is a tool to create and manage Javascript programming libraries for Node.js. Using npm init to initialize the project will generate a sample package.json file.

    npm init
    

    Note: You proceed to configure the application’s information and it is saved in the file package.json

    create vpc for linux instance

  6. Next we do dependencies installation

    • express
    • Dotenv
    • express-handlebars
    • body-parser
    • mysql
    npm install express dotenv express-handlebars body-parser mysql
    

    create vpc for linux instance

    ! How to Fix Vulnerabilities Related to Nodemon If the number of vulnerabilities is greater than 0 after running npm update and npm audit fix, you can try removing and reinstalling nodemon by following these steps:

    • Step 1: Remove nodemon:
    npm uninstall nodemon
    
    • Step 2: Install the latest version of nodemon:
    npm install nodemon@latest --save-dev
    
    • Step 3: Check vulnerabilities again
    npm audit
    

    create vpc for linux instance

    Explanation:

    • nodemon is a development tool that automatically restarts the application when code changes.
    • Older versions of nodemon may include dependencies with security vulnerabilities.
    • Reinstalling nodemon updates its dependencies to safer versions.
  7. Check the installed dependencies. The node_modules folder appears.

    ls
    

    create vpc for linux instance

  8. Create file .env

    touch .env
    

    create vpc for linux instance

  9. Use vi to edit the .env file. We perform the database configuration:

    DB_HOST=localhost
    DB_NAME=awsuser
    DB_USER=root
    DB_PASS=password
    

    Quick Guide to using vi/vim:

    • Open a file: vi <filename>

    • Enter insert mode: Press i

    • Save and exit: Press Esc, then type :wq and press Enter

    • Quit without saving: Press Esc, then type :q! and press Enter

    create vpc for linux instance

In the lab we use the database name awsuser and the database information has been configured in the database security configuration step (password: 123Admin)

  1. Check database configuration

    ls -a
    

    create vpc for linux instance

  2. Start local server

    npm start
    

    create vpc for linux instance

! Fix TypeError: exphbs is not a function (Express Handlebars)

  • Problem: When starting the Express application, the following error appears:
TypeError: exphbs is not a function
  • Cause: Newer versions of express-handlebars changed how the module is imported and initialized. The old syntax is no longer supported.

  • Solution: You can use ‘vi’ or ’nano’ to edit file app.js, replace:

    • const exphbs = require('express-handlebars'); with:
    const { engine } = require('express-handlebars');
    
    • And app.engine('hbs', engine({ extname: '.hbs' })); with:
    app.engine('hbs', engine({ extname: '.hbs' }));
    

create vpc for linux instance

  1. In the EC2 interface

    • Select Instances
    • Select Linux-instance
    • Copy Public IPv4 DNS address
  2. Paste Public DNS IPv4 address into the browser and port 5000. Observe the AWS FCJ Management interface

    create vpc for linux instance

    create vpc for linux instance

  3. In the phpMyAdmin interface, we execute SQL Dummy Data

    • Select awsuser database
    • Select SQL
    • Paste the SQL query code in
    INSERT INTO `user` 
    (`id`, `first_name`,  `last_name`,    `email`,                 `phone`,         `comments`, `status`) VALUES
    (NULL, 'Amanda',      'Nunes',        'anunes@ufc.com',        '012345 678910', '',          'active'),
    (NULL, 'Alexander',   'Volkanovski',  'avolkanovski@ufc.com',  '012345 678910', '',          'active'),
    (NULL, 'Khabib',      'Nurmagomedov', 'knurmagomedov@ufc.com', '012345 678910', '',          'active'),
    (NULL, 'Kamaru',      'Usman',        'kusman@ufc.com',        '012345 678910', '',          'active'),
    (NULL, 'Israel',      'Adesanya',     'iadesanya@ufc.com',     '012345 678910', '',          'active'),
    (NULL, 'Henry',       'Cejudo',       'hcejudo@ufc.com',       '012345 678910', '',          'active'),
    (NULL, 'Valentina',   'Shevchenko',   'vshevchenko@ufc.com',   '012345 678910', '',          'active'),
    (NULL, 'Tyron',       'Woodley',      'twoodley@ufc.com',      '012345 678910', '',          'active'),
    (NULL, 'Rose',        'Namajunas ',   'rnamajunas@ufc.com',    '012345 678910', '',          'active'),
    (NULL, 'Tony',        'Ferguson ',    'tferguson@ufc.com',     '012345 678910', '',          'active'),
    (NULL, 'Jorge',       'Masvidal ',    'jmasvidal@ufc.com',     '012345 678910', '',          'active'),
    (NULL, 'Nate',        'Diaz ',        'ndiaz@ufc.com',         '012345 678910', '',          'active'),
    (NULL, 'Conor',       'McGregor ',    'cmcGregor@ufc.com',     '012345 678910', '',          'active'),
    (NULL, 'Cris',        'Cyborg ',      'ccyborg@ufc.com',       '012345 678910', '',          'active'),
    (NULL, 'Tecia',       'Torres ',      'ttorres@ufc.com',       '012345 678910', '',          'active'),
    (NULL, 'Ronda',       'Rousey ',      'rrousey@ufc.com',       '012345 678910', '',          'active'),
    (NULL, 'Holly',       'Holm ',        'hholm@ufc.com',         '012345 678910', '',          'active'),
    (NULL, 'Joanna',      'Jedrzejczyk ', 'jjedrzejczyk@ufc.com',  '012345 678910', '',          'active')
    
    • Select Format
    • Select Go

    create vpc for linux instance

    create vpc for linux instance

  4. Refresh the application interface

create vpc for linux instance

  1. View the user

create vpc for linux instance

  1. Edit users

create vpc for linux instance

  1. Add users

    create vpc for linux instance

  2. Search for users

    create vpc for linux instance

  3. Database after inserting the item

    create vpc for linux instance

  4. The interface of the instance when starting the local server

    create vpc for linux instance