Skip to content

Post api implemented with express and mysql using MVC Architecture.

Notifications You must be signed in to change notification settings

harsh6768/deploy-in-ec2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

express-api-mysql

❤️ Star ❤️ the repo to support the project or 😄Follow Me.Thanks!

  1. Clone the repository

    git clone https://github.com/harsh6768/express-api-mysql.git
    
  2. Install all the dependency

    npm install
    

:::::::Deploy Node.js Project to AMAZON EC2::::::

  1. Create EC2 Instance in AWS

  2. Run these command on your terminal where pem file exist

    chmod 400 ec2-1.pem  //here ec2-1.pem is file name. you can give any name to file
    
  3. Run below command to connect with ec2 instance

    ssh -i ec2-1.pem ubuntu@ec2-18-191-201-10.us-east-2.compute.amazonaws.co
    

---->ec2-1.pem // pem file

---->ubuntu //by default username

---->ec2-18-191-201-10.us-east-2.compute.amazonaws.co //DNS Public domain name

Or just click the connect button to get all the necessary information to connect with the EC2 in your system

You will something like this ...follow these commands to connect with ec2.

:::::::Install MySQL in AMAZON EC2::::::

  1. Install apt in EC2 ubuntu

    https://low-orbit.net/how-to-install-yum-on-ubuntu

  2. update ubuntu

     sudo apt-get update
    
  3. Install mysql server

    sudo apt-get install mysql-server
    
  4. Run below command to run sql queries

    sudo mysql --user=root mysql
    
  5. Show list of databases

    show databases;
    
  6. Use database :

    use database_name;
    
  7. Show list of tables

    show tables;
    
  8. Update root password after selecting mysql database.

    update user set authentication_string='new-password' where user='root';
    
  9. Flush privileges

    flush privileges;
    exit
    
  10. Restart mysql

    sudo service mysql restart

  11. Now you can use mysql using root password

    sudo mysql -u root -p

Install MYSQL using yum command if apt-get command not found.

follow below docs.

https://dev.mysql.com/doc/mysql-repo-excerpt/5.6/en/linux-installation-yum-repo.html

Now Clone any node.js project in ec2 instance and install all the dependency but before that you need to install node and npm in your ec2 instance

In db.js file you should provide the valid details in mysql.createConnection() method

   const Bluebird=require('bluebird');
   const mysql=require('mysql');

   const connection= mysql.createConnection({
       host : 'localhost',
       user : 'root',           //by default user name
       password :'',            //put password that you have created while installing the mysql in ec2
       database : 'node'       // provide database name ,created in ec2
    });

   connection.connect((err)=>{
       if(err) throw err;
       console.log('MySQL Connected ...');
   });

   global.db=Bluebird.promisifyAll(connection);
   module.exports=db;

Now set custom port in ec2 instance in which your express server is running after selecting security group from the left navigation view.

You can see we have Custom TCP rule in port 3002. You can set any port in which you are running your express server.

Now run your project . If you have installed mysql successfully then server will run successfully.

If you get error

Client does not support authentication protocol requested by server; consider upgrading MySQL client

Then you need to change the root plugin in mysql

Run below command

 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

You are good to go . Run your server.

http://18.191.201.10:3002/getPosts will return all the post in the database

here 18.191.201.10 is the IPv4 Public IP of EC2

Install NODEJS if there is no APT-GET COMMAND found OR the easy way to install the nodejs and DOCKER INTO THE EC2

1. Install Docker

https://gist.github.com/npearce/6f3c7826c7499587f00957fee62f8ee9

Install Nodejs using ap

   sudo apt install nodejs

   sudo apt install npm

2. Install Nodejs using NVM

https://github.com/nvm-sh/nvm

 nvm install --lts               //install using nodejs

PHP MYADMIN installation to access MYSQL database .

   sudo apt install phpmyadmin

Configure phpmyadmin in ubuntu ec2

Edit apache2.conf file

 sudo nano /etc/apache2/apache2.conf
    
  or 
    
 sudo vi /etc/apache2/apache2.conf

After that, add the following line into apache2.conf file and save it:

   Include /etc/phpmyadmin/apache.conf

Enable MySQL root Login for phpmyadmin ubuntu aws

Note that, By default root cannot login as root user through phpMyAdmin. So, open your ssh terminal and type the following command to enable/allow mysql root login access for phpmyadmin on ubuntu aws web server:

       sudo mysql -u root -p

       
       ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Note that, Replace password by the root password you entered while installing MySql.

After that type following query to changes into effect:

      FLUSH PRIVILEGES;

Restart Apache Web Server

     sudo service apache2 restart

Access phpmyadmin Amazon ec2

Now you can login as root from phpMyAdmin. So, open your browser and type the below url with your ec2 server ip:

 http://[SERVER_PUBLIC_IP]/phpmyadmin

You can follow the bellow for better understanding and for step by step commands that you have to follow

https://www.tutsmake.com/how-to-install-phpmyadmin-amazon-ec2-ubuntu/