-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·120 lines (102 loc) · 4.18 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·120 lines (102 loc) · 4.18 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
#!/bin/bash
# Configuration
SERVICE_NAME="webber"
BINARY_NAME="webber"
INSTALL_DIR="/usr/local/bin"
CONFIG_DIR="/etc/webber"
STATIC_SERVE_DIR="/var/www/webber" # This is where the web server will serve files from
STATIC_SOURCE_DIR="$CONFIG_DIR/static" # This is where the original static files will be stored canonically
USER="webber"
GROUP="webber"
# Ensure script is run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo)"
exit 1
fi
# Create user and group
if ! id "$USER" >/dev/null 2>&1; then
useradd -r -s /bin/false "$USER"
fi
# Build Go binary
echo "Building Webber web server."
go build -o "$BINARY_NAME" main.go
if [ $? -ne 0 ]; then
echo "Build failed"
exit 1
fi
# Create directories
mkdir -p "$CONFIG_DIR" || { echo "Failed to create config directory"; exit 1; }
mkdir -p "$STATIC_SERVE_DIR" || { echo "Failed to create static serve directory"; exit 1; }
mkdir -p "$STATIC_SOURCE_DIR" || { echo "Failed to create static source directory"; exit 1; }
# Set ownership and permissions for serving directory
chown "$USER:$GROUP" "$STATIC_SERVE_DIR"
chmod 755 "$STATIC_SERVE_DIR"
# Copy binary and config
cp "$BINARY_NAME" "$INSTALL_DIR/" || { echo "Failed to copy binary"; exit 1; }
cp config.json "$CONFIG_DIR/" || { echo "Failed to copy config"; exit 1; }
chown "$USER:$GROUP" "$CONFIG_DIR/config.json"
chmod 644 "$CONFIG_DIR/config.json"
# Copy static files from the current directory's 'static' folder
# to their canonical source location in /etc/webber/static/
if [ -d "static" ]; then
echo "Copying static files to $STATIC_SOURCE_DIR..."
# Use rsync for more robust copying, especially if files already exist
rsync -av static/ "$STATIC_SOURCE_DIR/" || { echo "Failed to copy static source files"; exit 1; }
chown -R "$USER:$GROUP" "$STATIC_SOURCE_DIR"
chmod -R 755 "$STATIC_SOURCE_DIR"
fi
# Create symlinks from the static source directory to the serving directory
echo "Creating symlinks from $STATIC_SOURCE_DIR to $STATIC_SERVE_DIR..."
# Remove any existing files/symlinks in the STATIC_SERVE_DIR to ensure a clean slate for symlinks
# This is crucial to prevent the "same file" error if previous attempts left files behind.
rm -rf "$STATIC_SERVE_DIR"/* # Remove all contents, but not the directory itself
# Now, create the symlinks from the canonical source to the serving directory
for file in "$STATIC_SOURCE_DIR"/*; do
if [ -e "$file" ]; then # Check if file exists to prevent errors with empty directories
ln -sf "$file" "$STATIC_SERVE_DIR/" || { echo "Failed to create symlink for $(basename "$file")"; exit 1; }
fi
done
# Generate self-signed certificates if not provided
if [ ! -f "$CONFIG_DIR/cert.pem" ] || [ ! -f "$CONFIG_DIR/key.pem" ]; then
echo "Generating self-signed TLS certificates..."
openssl req -x509 -newkey rsa:4096 -keyout "$CONFIG_DIR/key.pem" -out "$CONFIG_DIR/cert.pem" -days 365 -nodes -subj "/CN=localhost"
fi
chown "$USER:$GROUP" "$CONFIG_DIR/cert.pem" "$CONFIG_DIR/key.pem"
chmod 600 "$CONFIG_DIR/cert.pem" "$CONFIG_DIR/key.pem"
# Grant CAP_NET_BIND_SERVICE capability to the binary
# Corrected typo: 'cap_net_bind_bind_service' -> 'cap_net_bind_service'
setcap 'cap_net_bind_service=+ep' "$INSTALL_DIR/$BINARY_NAME"
# Create systemd service file with better restart policy
cat > /etc/systemd/system/"$SERVICE_NAME".service <<EOF
[Unit]
Description=Webber Web Server
After=network.target
[Service]
ExecStart=$INSTALL_DIR/$BINARY_NAME
WorkingDirectory=$CONFIG_DIR
User=$USER
Group=$GROUP
Restart=always
RestartSec=5
ExecStartPre=/bin/sh -c 'echo Starting Webber > /tmp/webber.log'
Environment=DEBUG=1
[Install]
WantedBy=multi-user.target
EOF
# Enable and start service
echo "Reloading systemd daemon..."
systemctl daemon-reload
echo "Enabling and starting $SERVICE_NAME service..."
systemctl enable "$SERVICE_NAME"
echo "Starting $SERVICE_NAME service..."
systemctl start "$SERVICE_NAME"
# Check service status
echo "Checking service status..."
if systemctl is-active "$SERVICE_NAME" >/dev/null; then
echo "Installation complete. Service $SERVICE_NAME is running."
echo "Access at https://localhost:443"
else
echo "Service failed to start. Check logs with 'journalctl -u webber' or /tmp/webber.log"
exit 1
fi
echo "Webber web server installed successfully."