-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
348 lines (289 loc) · 13.5 KB
/
install.sh
File metadata and controls
348 lines (289 loc) · 13.5 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/bash
#===============================================================================
#
# FILE: install.sh
#
# USAGE: ./install.sh
#
# DESCRIPTION: Server Panel Bootstrap Installer
# Prepares a fresh Linux VPS for production use
#
# VERSION: 1.0.0
# AUTHOR: Server Panel
#
#===============================================================================
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color
# Configuration
INSTALLER_PORT=8080
INSTALLER_DIR="/tmp/server-panel-installer"
MIN_PHP_VERSION="8.1"
#===============================================================================
# Helper Functions
#===============================================================================
print_banner() {
clear
echo -e "${PURPLE}"
echo "╔══════════════════════════════════════════════════════════════════════╗"
echo "║ ║"
echo "║ ███████╗███████╗██████╗ ██╗ ██╗███████╗██████╗ ║"
echo "║ ██╔════╝██╔════╝██╔══██╗██║ ██║██╔════╝██╔══██╗ ║"
echo "║ ███████╗█████╗ ██████╔╝██║ ██║█████╗ ██████╔╝ ║"
echo "║ ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██╔══╝ ██╔══██╗ ║"
echo "║ ███████║███████╗██║ ██║ ╚████╔╝ ███████╗██║ ██║ ║"
echo "║ ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝ ║"
echo "║ ║"
echo "║ ██████╗ █████╗ ███╗ ██╗███████╗██╗ ║"
echo "║ ██╔══██╗██╔══██╗████╗ ██║██╔════╝██║ ║"
echo "║ ██████╔╝███████║██╔██╗ ██║█████╗ ██║ ║"
echo "║ ██╔═══╝ ██╔══██║██║╚██╗██║██╔══╝ ██║ ║"
echo "║ ██║ ██║ ██║██║ ╚████║███████╗███████╗ ║"
echo "║ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚══════╝ ║"
echo "║ ║"
echo "║ Production VPS Setup & Security Installer ║"
echo "║ ║"
echo "╚══════════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
echo ""
}
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_step() {
echo -e "${CYAN}[STEP]${NC} $1"
}
check_root() {
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root!"
log_info "Please run: sudo ./install.sh"
exit 1
fi
}
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$NAME
VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
else
OS=$(uname -s)
VER=$(uname -r)
fi
log_info "Detected OS: $OS $VER"
# Check if supported
case "$OS" in
*Ubuntu*|*Debian*)
log_success "Supported operating system detected"
;;
*)
log_warning "This OS may not be fully supported. Proceeding anyway..."
;;
esac
}
get_server_ip() {
# Try multiple methods to get the public IP
SERVER_IP=$(curl -s -4 ifconfig.me 2>/dev/null || \
curl -s -4 icanhazip.com 2>/dev/null || \
curl -s -4 ipecho.net/plain 2>/dev/null || \
hostname -I | awk '{print $1}' 2>/dev/null || \
echo "localhost")
log_info "Server IP: $SERVER_IP"
}
#===============================================================================
# Installation Functions
#===============================================================================
update_system() {
log_step "Updating system packages..."
export DEBIAN_FRONTEND=noninteractive
apt-get update -y > /dev/null 2>&1
apt-get upgrade -y > /dev/null 2>&1
log_success "System packages updated"
}
install_dependencies() {
log_step "Installing base dependencies..."
apt-get install -y \
curl \
wget \
git \
unzip \
software-properties-common \
apt-transport-https \
ca-certificates \
gnupg \
lsb-release \
> /dev/null 2>&1
log_success "Base dependencies installed"
}
install_php() {
log_step "Installing PHP..."
# Detect Ubuntu version
UBUNTU_VERSION=$(lsb_release -rs 2>/dev/null || echo "22.04")
log_info "Ubuntu version: $UBUNTU_VERSION"
# For Ubuntu 24.04+, use native PHP 8.3
if [[ "$UBUNTU_VERSION" == "24.04" ]] || [[ "$UBUNTU_VERSION" > "24" ]]; then
log_info "Using native PHP 8.3 for Ubuntu 24.04..."
PHP_VER="8.3"
else
log_info "Adding PHP repository for older Ubuntu..."
add-apt-repository -y ppa:ondrej/php || true
apt-get update -y
PHP_VER="8.2"
fi
log_info "Installing PHP $PHP_VER packages..."
# Install PHP packages one by one to avoid issues
PACKAGES="php${PHP_VER} php${PHP_VER}-cli php${PHP_VER}-fpm php${PHP_VER}-common php${PHP_VER}-mysql php${PHP_VER}-zip php${PHP_VER}-gd php${PHP_VER}-mbstring php${PHP_VER}-curl php${PHP_VER}-xml php${PHP_VER}-bcmath php${PHP_VER}-intl php${PHP_VER}-readline php${PHP_VER}-opcache"
for pkg in $PACKAGES; do
log_info "Installing $pkg..."
apt-get install -y $pkg
done
# Install redis extension separately
apt-get install -y php${PHP_VER}-redis 2>/dev/null || log_warning "php-redis not available"
# Verify installation
if command -v php &> /dev/null; then
PHP_VERSION=$(php -v | head -n 1 | cut -d " " -f 2 | cut -d "." -f 1,2)
log_success "PHP $PHP_VERSION installed successfully"
else
log_error "PHP installation failed!"
exit 1
fi
}
setup_installer_directory() {
log_step "Setting up installer directory..."
# Create installer directory
mkdir -p "$INSTALLER_DIR"
# Get the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Copy installer files
if [ -d "$SCRIPT_DIR" ]; then
cp -r "$SCRIPT_DIR"/* "$INSTALLER_DIR/" 2>/dev/null || true
cp -r "$SCRIPT_DIR"/.* "$INSTALLER_DIR/" 2>/dev/null || true
fi
# Set permissions
chmod -R 755 "$INSTALLER_DIR"
log_success "Installer directory ready at $INSTALLER_DIR"
}
start_web_installer() {
log_step "Starting secure web installer with HTTPS..."
# Kill any existing servers and free up port 8080
systemctl stop nginx 2>/dev/null || true
pkill -f "php -S" 2>/dev/null || true
pkill -f stunnel 2>/dev/null || true
fuser -k $INSTALLER_PORT/tcp 2>/dev/null || true
sleep 2
# Install stunnel for SSL wrapping
log_info "Installing SSL wrapper..."
apt-get install -y stunnel4 > /dev/null 2>&1
# Generate self-signed SSL certificate
log_info "Generating temporary SSL certificate..."
SSL_DIR="/tmp/server-panel-ssl"
mkdir -p "$SSL_DIR"
openssl req -x509 -nodes -days 1 -newkey rsa:2048 \
-keyout "$SSL_DIR/installer.pem" \
-out "$SSL_DIR/installer.pem" \
-subj "/C=US/ST=State/L=City/O=ServerPanel/CN=$SERVER_IP" \
2>/dev/null
chmod 600 "$SSL_DIR/installer.pem"
# Start PHP built-in server on internal port (runs as root!)
INTERNAL_PORT=8081
cd "$INSTALLER_DIR"
nohup php -S 127.0.0.1:$INTERNAL_PORT -t "$INSTALLER_DIR" > /tmp/server-panel.log 2>&1 &
sleep 2
# Create stunnel config to wrap PHP server with SSL
log_info "Configuring HTTPS proxy..."
cat > /etc/stunnel/server-panel.conf << STUNNEL_EOF
pid = /tmp/stunnel-server-panel.pid
[server-panel]
accept = 0.0.0.0:$INSTALLER_PORT
connect = 127.0.0.1:$INTERNAL_PORT
cert = $SSL_DIR/installer.pem
STUNNEL_EOF
# Start stunnel
stunnel /etc/stunnel/server-panel.conf
# Allow port through firewall if UFW is enabled
ufw allow $INSTALLER_PORT/tcp 2>/dev/null || true
# Verify servers are running
sleep 2
if pgrep -f "php -S" > /dev/null && pgrep -f stunnel > /dev/null; then
log_success "Secure web installer started with HTTPS"
else
log_warning "SSL wrapper failed, using HTTP fallback..."
pkill -f stunnel 2>/dev/null || true
pkill -f "php -S" 2>/dev/null || true
nohup php -S 0.0.0.0:$INSTALLER_PORT -t "$INSTALLER_DIR" > /tmp/server-panel.log 2>&1 &
sleep 2
# Update for HTTP fallback
echo ""
echo -e "${YELLOW}⚠️ Using HTTP - use http://${SERVER_IP}:${INSTALLER_PORT} instead${NC}"
fi
}
print_completion_message() {
echo ""
echo -e "${GREEN}╔══════════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ ║${NC}"
echo -e "${GREEN}║ 🎉 Bootstrap Installation Complete! 🎉 ║${NC}"
echo -e "${GREEN}║ ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${WHITE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e "${CYAN} Continue setup in your browser:${NC}"
echo ""
echo -e "${YELLOW} ┌────────────────────────────────────────────────────────┐${NC}"
echo -e "${YELLOW} │ │${NC}"
echo -e "${YELLOW} │ ${WHITE}https://${SERVER_IP}:${INSTALLER_PORT}${YELLOW} │${NC}"
echo -e "${YELLOW} │ │${NC}"
echo -e "${YELLOW} └────────────────────────────────────────────────────────┘${NC}"
echo ""
echo -e "${WHITE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e "${YELLOW} ⚠️ Your browser will show a security warning (self-signed cert).${NC}"
echo -e "${YELLOW} Click 'Advanced' → 'Proceed' to continue safely.${NC}"
echo ""
echo -e "${PURPLE} Note: After completing the web setup, all installer files${NC}"
echo -e "${PURPLE} will be automatically removed for security.${NC}"
echo ""
}
#===============================================================================
# Main Execution
#===============================================================================
main() {
print_banner
log_info "Starting Server Panel Bootstrap Installation..."
echo ""
# Pre-flight checks
check_root
detect_os
get_server_ip
echo ""
echo -e "${WHITE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
# Installation steps
update_system
install_dependencies
install_php
setup_installer_directory
start_web_installer
# Done!
print_completion_message
}
# Run main function
main "$@"