Skip to content

Instantly share code, notes, and snippets.

@KTBsomen
Last active September 17, 2024 08:56
Show Gist options
  • Select an option

  • Save KTBsomen/988866b61c9d3a6022a99a27d1e79edf to your computer and use it in GitHub Desktop.

Select an option

Save KTBsomen/988866b61c9d3a6022a99a27d1e79edf to your computer and use it in GitHub Desktop.
install.sh for NodeJS this will install latest version of NodeJS along with pm2 nodemon npm for Linux and also run.sh for running your servers. You can edit this as your needs.
#!/bin/bash
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to compare version numbers
version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"
}
# Update package lists
sudo apt update
# Install curl if not already installed
if ! command_exists curl; then
echo "Installing curl..."
sudo apt install -y curl
else
echo "curl is already installed."
fi
# Check if Node.js is installed
if command_exists node; then
echo "Node.js is already installed."
current_version=$(node -v | cut -d 'v' -f 2)
echo "Current version: $current_version"
# Get the latest LTS version number
latest_version=$(curl -s https://nodejs.org/dist/index.json | grep -o '"version":"[^"]*"' | head -n 1 | cut -d '"' -f 4 | cut -d 'v' -f 2)
echo "Latest LTS version: $latest_version"
if version_gt $latest_version $current_version; then
echo "A newer version of Node.js is available. Updating..."
# Install n (Node.js version manager)
sudo npm install -g n
# Install and use the latest LTS version
sudo n lts
# Update npm to the latest version
sudo npm install -g npm@latest
else
echo "You have the latest version of Node.js."
fi
else
echo "Node.js is not installed. Installing the latest LTS version..."
# Install n (Node.js version manager)
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
sudo bash n lts
# Install npm
sudo npm install -g npm@latest
fi
# Verify Node.js and npm installation
node --version
npm --version
# Install pm2 globally if not already installed
if ! command_exists pm2; then
echo "Installing pm2..."
sudo npm install -g pm2
else
echo "pm2 is already installed."
fi
# Install nodemon globally if not already installed
if ! command_exists nodemon; then
echo "Installing nodemon..."
sudo npm install -g nodemon
else
echo "nodemon is already installed."
fi
# Add Node.js to PATH (if not already added)
if ! grep -q "export PATH=\$PATH:/usr/bin" ~/.bashrc; then
echo "Adding Node.js to PATH..."
echo 'export PATH=$PATH:/usr/bin' >> ~/.bashrc
source ~/.bashrc
else
echo "Node.js is already in PATH."
fi
echo "Installation and checks complete. Please restart your terminal or run 'source ~/.bashrc' to apply PATH changes."
#!/bin/bash
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check if PM2 is installed
if ! command_exists pm2; then
echo "PM2 is not installed. Installing PM2..."
npm install -g pm2
fi
# Function to start or restart a server
start_or_restart_server() {
local name=$1
shift
local args=("$@")
if pm2 describe $name > /dev/null 2>&1; then
echo "Restarting existing server: $name"
pm2 restart $name "${args[@]}"
else
echo "Starting new server: $name"
pm2 start "${args[@]}" --name "$name"
fi
}
# ----------------
# Server Configurations
# Add or modify server configurations below this line
# Format: start_or_restart_server "name" [PM2 arguments as you would use in command line]
# ----------------
start_or_restart_server "app1" app.js --watch -- --port 3000
start_or_restart_server "app2" npm -- start --port 3001
start_or_restart_server "app3" python3 app.py -- --port 3002
# Add more server configurations here
# Example: start_or_restart_server "another_app" another_app.js -i 2 --env production
# ----------------
# End of Server Configurations
# ----------------
# Save PM2 process list
pm2 save
# Setup PM2 to start on boot with systemd
pm2 startup systemd
echo "PM2 process list saved and set to start on boot."
echo "To complete the setup, run the command suggested by PM2 above (if any)."
echo "You may need to run it with sudo privileges."
# Display running processes
pm2 list
echo "Script execution completed. Your servers are now managed by PM2 and will start on boot."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment