node-ejs-renderer/deploy.sh

67 lines
1.6 KiB
Bash
Raw Permalink Normal View History

2024-06-09 13:55:01 -04:00
#!/bin/bash
# Configuration
SERVER_USER="root"
SERVER_IP="155.254.30.254"
SERVER_DIR="~/node-ejs-renderer"
SERVER_DIR_HOME="~"
LOCAL_DIR="/Users/emkay/Projects/node-ejs-renderer"
NGINX_CONFIG="node-ejs-renderer" # Name of the Nginx config file
DOMAIN_OR_IP="the.mk" # Your domain or IP address
EMAIL="mahesh.kommareddi@the.mk" # Your email for Let's Encrypt
# Commands to run on the remote server
REMOTE_COMMANDS=$(cat <<EOF
# Update system
apt update && apt upgrade -y
# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
export NVM_DIR="\$HOME/.nvm"
[ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh"
# Install Node.js and npm using nvm
nvm install stable
nvm use stable
nvm alias default stable
# Install pm2 globally
npm install -g pm2
# Install Nginx
apt install -y nginx
# Configure Nginx
nginx -t && systemctl restart nginx
# Install Certbot for Let's Encrypt
apt install -y certbot python3-certbot-nginx
# Obtain and install SSL certificate
# certbot --nginx --non-interactive --agree-tos --email $EMAIL --domains $DOMAIN_OR_IP
# Set up cron job for automatic certificate renewal
# (crontab -l 2>/dev/null; echo "0 0,12 * * * root certbot renew --quiet") | crontab -
# Change to the project directory
cd $SERVER_DIR
# Install project dependencies
npm install
# Start the application with pm2
pm2 start server.js
pm2 save
pm2 startup
EOF
)
# Copy project files to the remote server
rsync -avz --exclude 'node_modules' --exclude '.git' $LOCAL_DIR $SERVER_USER@$SERVER_IP:$SERVER_DIR_HOME
# Run the commands on the remote server
ssh $SERVER_USER@$SERVER_IP "$REMOTE_COMMANDS"
echo "Deployment complete!"