-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvps-setup.sh
More file actions
146 lines (120 loc) · 4.27 KB
/
vps-setup.sh
File metadata and controls
146 lines (120 loc) · 4.27 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# Echoit VPS Setup Script
# This script helps set up Echoit on a fresh Ubuntu/Debian VPS
# Exit on error
set -e
echo "=== Echoit VPS Setup Script ==="
echo "This script will install Java, Node.js, Nginx, and set up Echoit."
# Update system
echo "Updating system packages..."
sudo apt update
sudo apt upgrade -y
# Install Java
echo "Installing Java..."
sudo apt install -y openjdk-17-jdk
# Install Node.js
echo "Installing Node.js..."
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
# Install Nginx
echo "Installing Nginx..."
sudo apt install -y nginx
# Install PM2
echo "Installing PM2..."
sudo npm install -g pm2
# Install Maven
echo "Installing Maven..."
sudo apt install -y maven
# Clone repository (uncomment and modify if using Git)
# echo "Cloning repository..."
# git clone https://github.com/yourusername/echoit.git
# cd echoit
# Build backend
echo "Building Java backend..."
mvn clean package
# Build frontend
echo "Building frontend..."
cd ui
npm install
npm run build
cd ..
# Set up Nginx
echo "Setting up Nginx..."
# Ensure the default site is removed to avoid conflicts
if [ -e /etc/nginx/sites-enabled/default ]; then
sudo rm /etc/nginx/sites-enabled/default
echo "Removed default Nginx site configuration."
fi
# Create the echoit configuration file with the correct content
echo "Creating /etc/nginx/sites-available/echoit..."
cat <<EOF | sudo tee /etc/nginx/sites-available/echoit
server {
listen 80;
server_name _; # Catch-all for HTTP requests
# Backend API
location /api/ {
proxy_pass http://localhost:8080/;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
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;
proxy_cache_bypass \$http_upgrade;
}
# Frontend
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_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;
proxy_cache_bypass \$http_upgrade;
}
# Additional security headers (still good to have)
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options SAMEORIGIN;
add_header X-XSS-Protection "1; mode=block";
}
EOF
# Create the symbolic link to enable the echoit site
sudo ln -sf /etc/nginx/sites-available/echoit /etc/nginx/sites-enabled/echoit
sudo nginx -t
if [ $? -eq 0 ]; then
sudo systemctl restart nginx
echo "Nginx configured and restarted successfully."
else
echo "Nginx configuration test failed. Please check /etc/nginx/nginx.conf and /etc/nginx/sites-available/echoit."
exit 1
fi
# Set up SSL with Let's Encrypt (uncomment if needed)
# echo "Setting up SSL with Let's Encrypt..."
# sudo apt install -y certbot python3-certbot-nginx
# sudo certbot --nginx -d your-actual-domain.com
# Start backend with PM2
echo "Starting backend with PM2..."
# Ensure all dependencies are in the classpath
CLASSPATH="target/echoit-1.0-SNAPSHOT.jar:$(mvn dependency:build-classpath -DincludeScope=runtime -Dmdep.outputFile=/dev/stdout -q)"
# pm2 start --name echoit-backend java -- -cp "$CLASSPATH" echoit.App
pm2 start --name echoit-backend java -- -jar target/echoit-1.0.0.jar
# Start frontend with PM2
echo "Starting frontend with PM2..."
cd ui
pm2 start npm --name echoit-frontend -- start
cd ..
# Save PM2 configuration
pm2 save
# Set up PM2 to start on boot
echo "Setting up PM2 to start on boot..."
pm2 startup
# Follow the instructions printed by the above command
echo "=== Setup Complete ==="
echo "Echoit is now running on your VPS!"
echo "Backend API: http://localhost:8080 (Internal - accessed via Nginx)"
echo "Frontend: http://your_lightsail_public_ip (Access via your instance's IP address)"
echo "You can access your application using your Lightsail instance's public IP address in your browser."
# echo "Visit https://your-actual-domain.com to access your application."