Deploy application to EC2

1. Install Git and NodeJS

  • Install Git

    $ sudo apt install -y git
    $ git --version
    
  • Install NodeJS and npm

    $ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
    $ sudo apt install -y nodejs
    $ node -v
    $ npm -v
    
  • Install PM2 to manage the application

    $ sudo npm install -g pm2
    $ pm2 -v
    

2. Install mongosh and connect to Amazon DocumentDB

  • Install mongosh

    $ wget -qO - https://pgp.mongodb.com/server-6.0.asc | sudo tee /usr/share/keyrings/mongodb-server-key.asc
    
    $ echo "deb [signed-by=/usr/share/keyrings/mongodb-server-key.asc] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
    
    $ sudo apt update
    $ sudo apt install -y mongodb-mongosh
    $ mongosh --version
    
  • Check connection with DocumentDB

  • Go to the Cluster interface of Amazon DocumentDB, then select the created cluster choose-cluster

  • Download the global-bundle.pem file to EC2 and connect to DocumentDB in EC2 (replace insertYourPassword in the connection string with the password you set for DocumentDB)

    connect connect2

  • Type the command show dbs, you will see that there are no databases in DocumentDB yet check

3. Clone Repository

$ git clone https://github.com/NguyenNhatHuynh/fullStack-Blog.git
  • Use the ls command to check if the project has been cloned successfully clone-project

  • Navigate to the fullStack-Blog. Then, install dependencies

    $ cd fullStack-Blog
    $ ls
    $ cd server
    $ npm install
    

    clone-project

4. Add environment variables (.env)

  • Create a .env file containing database connection information and application configuration

    $ cd fullStack-Blog
    $ cd server
    $ touch .env
    $ nano .env
    

    env

  • Add environment variablesDownload the .env file and add the environment variables .env .env

    build

5. Run the application

  • Run the application using pm2

    pm2 start npm --name "blog-web" -- run start
    pm2 save
    pm2 startup
    
  • Check if the application is running:

    pm2 list
    

    build

6. Verify deployment

  • Open a browser and access
   http://s3-blog-workshop.s3-website-us-east-1.amazonaws.com/

build