-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserdata-ubuntu.sh
More file actions
125 lines (104 loc) · 4.28 KB
/
Copy pathuserdata-ubuntu.sh
File metadata and controls
125 lines (104 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# ==============================================================================
# AWS EC2 UserData Script - Ubuntu 22.04 / 24.04 LTS
# Automates Git clone, Node.js 20, PM2 Cluster, .env setup, and Nginx reverse proxy
# ==============================================================================
set -e
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
echo "🚀 [$(date '+%Y-%m-%d %H:%M:%S')] Starting EC2 UserData bootstrap..."
# ------------------------------------------------------------------------------
# 1. Configuration (EDIT THESE VALUES BEFORE PASTING INTO LAUNCH TEMPLATE)
# ------------------------------------------------------------------------------
GIT_REPO_URL="https://github.com/YOUR_GITHUB_USERNAME/YOUR_REPOSITORY.git"
GIT_BRANCH="main"
APP_DIR="/var/www/fileshare-app"
# RDS Database Configuration
DB_HOST="your-rds-endpoint.amazonaws.com"
DB_PORT="3306"
DB_USER="admin"
DB_PASSWORD="your_secure_password"
DB_NAME="fileshare_db"
# Application Secrets & Storage
JWT_SECRET="your_jwt_secret_key"
S3_BUCKET_NAME="your-s3-bucket-name"
AWS_REGION="us-east-1"
PORT="3000"
# ------------------------------------------------------------------------------
# 2. System Update & Dependencies (Node.js 20 LTS, Git, Nginx)
# ------------------------------------------------------------------------------
echo "📦 Updating packages and installing Node.js 20 & Nginx..."
apt-get update -y
apt-get install -y curl git nginx
# Install Node.js 20.x LTS via NodeSource
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
# Install PM2 Process Manager globally
npm install -g pm2
# ------------------------------------------------------------------------------
# 3. Clone Repository via Git
# ------------------------------------------------------------------------------
echo "📂 Cloning repository from Git..."
mkdir -p /var/www
rm -rf "$APP_DIR"
git clone -b "$GIT_BRANCH" "$GIT_REPO_URL" "$APP_DIR"
cd "$APP_DIR"
# ------------------------------------------------------------------------------
# 4. Generate Production .env Configuration
# ------------------------------------------------------------------------------
echo "⚙️ Configuring production .env file..."
cat <<EOF > "$APP_DIR/.env"
PORT=$PORT
NODE_ENV=production
DB_HOST=$DB_HOST
DB_PORT=$DB_PORT
DB_USER=$DB_USER
DB_PASSWORD=$DB_PASSWORD
DB_NAME=$DB_NAME
JWT_SECRET=$JWT_SECRET
S3_BUCKET_NAME=$S3_BUCKET_NAME
AWS_REGION=$AWS_REGION
EOF
# Secure .env file permissions
chmod 600 "$APP_DIR/.env"
# ------------------------------------------------------------------------------
# 5. Install NPM Dependencies, Initialize DB & Start Application via PM2
# ------------------------------------------------------------------------------
echo "📦 Installing NPM dependencies..."
npm install --omit=dev
echo "🗄️ Ensuring database tables exist via init-db.js (preserves existing data)..."
node init-db.js || echo "⚠️ DB init notice"
echo "🚀 Starting application with PM2 Cluster Mode..."
pm2 delete fileshare-app || true
pm2 start ecosystem.config.js
# Configure PM2 to start on system boot
pm2 startup systemd -u root --hp /root || true
pm2 save || true
# ------------------------------------------------------------------------------
# 6. Configure Nginx Reverse Proxy (Port 80 -> Port 3000)
# ------------------------------------------------------------------------------
echo "🌐 Configuring Nginx reverse proxy..."
cat << 'EOF' > /etc/nginx/sites-available/fileshare-app
server {
listen 80;
server_name _;
client_max_body_size 100M;
location / {
proxy_pass http://127.0.0.1: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;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
EOF
# Enable site and remove default
ln -sf /etc/nginx/sites-available/fileshare-app /etc/nginx/sites-enabled/fileshare-app
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl restart nginx
systemctl enable nginx
echo "✅ [$(date '+%Y-%m-%d %H:%M:%S')] UserData bootstrap completed successfully!"