Loading codes...
Node Js › Deploy Node Js On Aws Ngnix
# Install nvm using curl
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
# add nvm to path
source ~/.bashrc
# install node js using nvm
nvm install 22
# use node js
nvm use 22
# install pm2 to manage node js application start
sudo npm install -g pm2
# install nginx
sudo apt install -y nginx
# enable nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# path to your node js application
# cd /path/to/application
pm2 start index.js --name "mynodeapp" or pm2 start server.js --name "mynodeapp"
# Create a new Nginx server block
sudo nano /etc/nginx/sites-available/mynodeapp
# Add the following configuration
server {
listen 80;
server_name your_server_ip_or_domain;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
# Enable the configuration
sudo ln -s /etc/nginx/sites-available/mynodeapp /etc/nginx/sites-enabled
# Test the Nginx configuration
sudo nginx -t
# Restart Nginx
sudo systemctl reload nginx
# Optional
# Install mysql
sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation
# Create New User and Assign Grant Access
CREATE USER 'new_user'@'localhost'IDENTIFIED WITH mysql_native_password BY 'User21@#$';
GRANT ALL PRIVILEGES ON * . * TO 'new_user'@'localhost';
flush PRIVILEGES;
# Drop User
DROP USER 'new_user'@'localhost';