Closed PESolut closed 1 month ago
Launching a new EC2 Instance
Adding elastic IP to leaf-me-backend ec2 instance
SSH into EC2 instance...
updating the system
sudo dnf update -y
Amazon Linux 2023 uses dnf (the package manager used by Fedora-based distributions).
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
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
sudo dnf install git -y
Cloned the repo and installed depen.
ssh into the db get contents of .env DONE
putting .env into the root of the backend repo
server is started testing for connection... UNSUCCESS
need to change inbound port rules for db: 0.0.0.0/24 on port 3000
connection GOOD, checking for expected return
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
connection established and backend returns rel. data upon connecting to end point
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.
install PM2 globally, run:
npm install pm2@latest -g
Navigate to your project directory:
cd /path/to/your/project
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.
Check the status of your processes:
pm2 list
Monitor your application:
pm2 monit
To stop your application:
pm2 stop "my-app"
To restart your application:
pm2 restart "my-app"
View logs:
pm2 logs
Set up PM2 to start on system boot:
pm2 startup
Follow the instructions provided by this command.
Save the current PM2 process list:
pm2 save
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
Remember to consult the official PM2 documentation for more detailed information and advanced features.
app works and connection is open; however when starting with pm2; no data comes back when trying to reach db :(
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
undefined on console.log of our PW; so we DONOT have our .env when we run using pm2
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
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.
no worky
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:
--env
flag when starting an applicationpm2 set
command to set environment variablesThe 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/
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:
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.
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.
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.
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
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
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:
Launch a new EC2 instance:
Connect to the EC2 instance via SSH
Update the system and install necessary software:
Clone the repository:
Install project dependencies:
Set up environment variables:
.env
file:nano .env
.env
file in the local projectconfirm connection between backend and db
Start the application:
npm start
npm run dev
Set up a process manager (PM2) to keep the app running:
Test the deployed application:
Update documentation:
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.