PESolut / leaf-me

new and final mono repo for the leaf me system: Leaf-Me is a full-stack Uber Eats-style delivery platform designed for dispensaries. The app streamlines the process of placing, preparing, and delivering orders through multiple role-based interfaces
0 stars 0 forks source link

get backend up and deployed #4

Closed PESolut closed 1 month ago

PESolut commented 1 month ago

Deploy the Leaf-Me backend application to a new Amazon EC2 instance. This will involve setting up the server, installing dependencies, configuring the database, and ensuring the application runs correctly in the cloud environment.

Steps:

  1. Launch a new EC2 instance:

    • Choose an Amazon Linux 2 AMI
    • Select an appropriate instance type (e.g., t2.micro for testing)
    • Configure security groups to allow inbound traffic on ports 22 (SSH) and 3000 (or your app's port)
  2. Connect to the EC2 instance via SSH

  3. Update the system and install necessary software:

    • Update the system:
    • Install Node.js and npm:
    • Install Git:
  4. Clone the repository:

  5. Install project dependencies:

  6. Set up environment variables:

    • Create a .env file: nano .env
    • Add the necessary environment variables, referencing the .env file in the local project
  7. confirm connection between backend and db

  8. Start the application:

    • For production: npm start
    • For development with auto-restart: npm run dev
  9. Set up a process manager (PM2) to keep the app running:

    • Install PM2:
    • Start the app with PM2:
    • Set PM2 to start on boot:
  10. Test the deployed application:

    • Ensure all API endpoints are working correctly
    • Test user authentication and database operations
  11. Update documentation:

    • Update the README.md file with deployment information
    • Document any environment-specific configurations

Additional Considerations:

By completing this ticket, the Leaf-Me backend will be successfully deployed and running on an EC2 instance, ready to serve requests from the frontend applications.

PESolut commented 1 month ago

Launching a new EC2 Instance

PESolut commented 1 month ago

Adding elastic IP to leaf-me-backend ec2 instance

PESolut commented 1 month ago

SSH into EC2 instance...

PESolut commented 1 month ago

updating the system

sudo dnf update -y

Amazon Linux 2023 uses dnf (the package manager used by Fedora-based distributions).

PESolut commented 1 month ago

installing necc. software

installing node.js

first to enable the module stream for the latest Node.js LTS vesrion:

dnf module enable nodejs:20

then to install the module stream with

dnf install node.js

ensure node.js is installed by running this command to check the installed version of node on the sys node --version

...

performing above actions

edit: Amazon Linux 2023 doesn't use the amazon-linux-extras like Amazon Linux 2 so we manually added the nodeJS repo to our sys with this command

curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo dnf install -y nodejs

nodeJS 20.18.0 successfully installed

PESolut commented 1 month ago

installing NVM + NPM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
PESolut commented 1 month ago

installling git

sudo dnf install git -y
PESolut commented 1 month ago

Cloned the repo and installed depen.

PESolut commented 1 month ago

ssh into the db get contents of .env DONE

PESolut commented 1 month ago

putting .env into the root of the backend repo

PESolut commented 1 month ago

server is started testing for connection... UNSUCCESS

BLOCKER

need to change inbound port rules for db: 0.0.0.0/24 on port 3000

PESolut commented 1 month ago

connection GOOD, checking for expected return

PESolut commented 1 month ago

BLOCKER

backend is up but error wont return any data... connection with sql db not good... i think its the lack of password in our cn object within dbConfig.js

PESolut commented 1 month ago

connection established and backend returns rel. data upon connecting to end point

PESolut commented 1 month ago

Setting up PM2 for our Node.js application

What is PM2?

PM2 is a production process manager for Node.js applications. It allows you to keep applications alive forever, reload them without downtime, and facilitate common system admin tasks.

Installation

install PM2 globally, run:

npm install pm2@latest -g

Basic PM2 Setup

  1. Navigate to your project directory:

    cd /path/to/your/project
  2. Start your application with PM2:

    pm2 start backend/server.js --name "my-app"

    Replace backend/server.js with the path to your main server file.

  3. Check the status of your processes:

    pm2 list
  4. Monitor your application:

    pm2 monit
  5. To stop your application:

    pm2 stop "my-app"
  6. To restart your application:

    pm2 restart "my-app"

Additional PM2 Commands

PM2 Configuration File (Optional)

For more advanced setups, you can create a ecosystem.config.js file in your project root:

javascript
module.exports = {
apps: [{
name: "my-app",
script: "./backend/server.js",
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}]
}

then start the app using:

pm2 start ecosystem.config.js

Next Steps

Remember to consult the official PM2 documentation for more detailed information and advanced features.

PESolut commented 1 month ago

BLOCKER

app works and connection is open; however when starting with pm2; no data comes back when trying to reach db :(

troubleshooting

testing if our .env is loaded when running as pm2

1) i want to check that our .env is being loaded properly when we start with pm2 so im going to add a console.log upon server run of the port

conclusion:

undefined on console.log of our PW; so we DONOT have our .env when we run using pm2

pm2 and enviroment vars

2) The problem might be related to how environment variables are loaded when using PM2

there's a specific section in the PM2 documentation that discusses environment variables:

https://pm2.keymetrics.io/docs/usage/environment/

While it doesn't directly mention dotenv, it does highlight the importance of properly setting up environment variables when using PM2

attempt solution ( pm2/ commit: ' error + path to env '

1) We've modified the dotenv.config() call to explicitly specify the path to your .env file. This ensures that the environment variables are loaded correctly, even when running with PM2. 2) We've added a basic error handling mechanism using the db.connect() method. This will help you identify any connection issues and log them to the console.

conclusion

no worky

running the app properly with pm2

When starting an application with PM2 without a config file, it doesn't automatically load the .env file in the same way that running the application directly with Node does. This behavior is not explicitly stated in the PM2 documentation, but it can be inferred from the PM2 documentation on environment variables[^1].

PM2 provides several ways to manage environment variables, which implies that the default behavior doesn't include automatic loading of .env files:

  1. Using the --env flag when starting an application
  2. Defining environment variables in an ecosystem file
  3. Using the pm2 set command to set environment variables

The need for these methods suggests that PM2 doesn't automatically load .env files by default, unlike when running a Node.js application directly.

[^1]: PM2 Documentation on Environment Variables: https://pm2.keymetrics.io/docs/usage/environment/

Solution

The issue was resolved by properly loading the .env file when starting the application with PM2. Here's the correct command to start the application:


pm2 start server.js --node-args="--require dotenv/config"```

This command does the following:
1. Uses PM2 to start the application
2. Tells Node.js to require the `dotenv/config` module before running the main application file
3. Ensures that environment variables from the `.env` file are loaded correctly

To use this solution:
1. Make sure you have the `dotenv` package installed (`npm install dotenv` if not already done)
2. Run the command from your project's root directory (where your `.env` file is located)

After running this command, the application should start with PM2 and have access to all environment variables defined in the `.env` file. You can verify this by checking the logs (`pm2 logs`) and confirming that your API endpoints are now returning data as expected.

Remember to restart your PM2 process if you make any changes to your `.env` file:
PESolut commented 1 month ago

Set PM2 to start on boot

To ensure our application starts automatically when the server reboots, we need to configure PM2 to start on system boot. This process involves generating a startup script and saving the current PM2 process list.

Steps:

  1. Generate the startup script:

    pm2 startup

    This command will output a line of code that you need to run with sudo privileges. Copy and run that command.

  2. Save the current PM2 process list:

    pm2 save

    This command saves the current list of processes, so PM2 knows which applications to start on boot.

  3. Verify the setup: Reboot your system and check if your application starts automatically.

    sudo reboot

    After the system comes back online, check the status of your processes:

    
    pm2 list
PESolut commented 1 month ago

app displays approp. data when connecting to dispensary: didn't test user auth; should work though as expected so ill open this ticket again if we have any problems when we got our frontend working