Cách deploy NodeJs + MySql lên EC2 của AWS Call API bằng Posman lên server

Поділитися
Вставка
  • Опубліковано 5 лют 2025
  • Cách deploy NodeJs + MySql lên EC2 của AWS Call API bằng Posman lên server
    Steps to Update Ubuntu, Install MySQL, and Deploy a Node.js App:
    1. Update Ubuntu and Install MySQL
    Run the following commands:
    Update package lists
    sudo apt update
    Install MySQL server
    sudo apt install mysql-server
    2. Change MySQL Authentication Method
    Set up the root user to use mysql_native_password:
    Log into MySQL
    sudo mysql
    Change authentication method and set password
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
    CREATE DATABASE nodeJsStudy;
    SHOW DATABASES;
    USE nodeJsStudy;
    CREATE TABLE `Users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `email` varchar(100) NOT NULL,
    `password` varchar(255) NOT NULL,
    `name` varchar(100) DEFAULT NULL,
    `city` varchar(100) DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `email` (`email`)
    ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
    Exit MySQL
    exit
    4. Install Node.js and npm
    Install Node.js and npm if not already installed:
    Install npm (includes Node.js in most cases)
    sudo apt install npm
    Verify installation
    node -v
    5. Clone Node.js Project
    Clone your project or the provided example project:
    Clone project
    sudo git clone github.com/ldt...
    Navigate to the project directory
    cd NodeJs
    Install dependencies
    sudo npm install
    6. Allow App Port in Firewall
    Allow the port your app uses (e.g., 3000):
    Enable UFW (Uncomplicated Firewall)
    sudo ufw enable
    Allow port 3000
    sudo ufw allow 3000
    7. Run the App
    Start the app with npm:
    Run the app (if using index.js)
    npm start dev
    Alternatively, if the script is defined in package.json, use:
    npm run dev
    call postmance:
    curl --location '3.90.216.80:8081/register' \
    --header 'Content-Type: application/json' \
    --data-raw '{
    "email": "accounttest3@example.com",
    "password": "123456",
    "name": "Account Test",
    "city": "Hanoi"
    }
    '
    curl --location '3.90.216.80:8081/login' \
    --header 'Content-Type: application/json' \
    --data-raw '{
    "email": "accounttest3@example.com",
    "password": "123456"
    }
    '
    curl --location --request GET '3.90.216.80:8081/api/users' \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEzLCJlbWFpbCI6ImFjY291bnR0ZXN0M0BleGFtcGxlLmNvbSIsImlhdCI6MTczNjY5OTA1NywiZXhwIjoxNzM2NzAyNjU3fQ.QqJZmEZgWJDlAbZVHsy00jXqq6fep-Hxsnl-CCGw2zg' \
    --data-raw '{
    "email": "accounttest3@example.com",
    "password": "123456"
    }
    '

КОМЕНТАРІ •