-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall_service.sh
More file actions
executable file
Β·286 lines (245 loc) Β· 8.34 KB
/
Copy pathinstall_service.sh
File metadata and controls
executable file
Β·286 lines (245 loc) Β· 8.34 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
#!/bin/bash
# WolfServe Service Installer
# Installs WolfServe as a systemd service on Linux
#
# Usage: ./install_service.sh [-y|--yes]
#
# Environment variables (for non-interactive use):
# WOLFSERVE_HOST - Server bind address (default: 0.0.0.0)
# WOLFSERVE_PORT - Server port (default: 3000)
# WOLFSERVE_FPM_PORT - PHP-FPM port (default: 9993)
# WOLFSERVE_SESSION_PATH - PHP session save path (default: /var/lib/php/sessions)
# WOLFSERVE_APACHE_DIR - Apache config directory (default: /etc/apache2 or /etc/httpd)
set -e
INSTALL_DIR="/opt/wolfserve"
SERVICE_NAME="wolfserve"
# Parse arguments
AUTO_YES=false
for arg in "$@"; do
case $arg in
-y|--yes)
AUTO_YES=true
shift
;;
esac
done
# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
echo "β Please run as root (sudo ./install_service.sh)"
exit 1
fi
echo "πΊ WolfServe Service Installer"
echo "=============================="
echo ""
# ============================================
# Configuration (interactive or from env vars)
# ============================================
# Detect default Apache directory
DEFAULT_APACHE_DIR="/etc/apache2"
if [ -d "/etc/httpd" ] && [ ! -d "/etc/apache2" ]; then
DEFAULT_APACHE_DIR="/etc/httpd"
fi
# Set defaults from environment variables
SERVER_HOST="${WOLFSERVE_HOST:-0.0.0.0}"
SERVER_PORT="${WOLFSERVE_PORT:-3000}"
PHP_FPM_PORT="${WOLFSERVE_FPM_PORT:-9993}"
SESSION_SAVE_PATH="${WOLFSERVE_SESSION_PATH:-/var/lib/php/sessions}"
APACHE_CONFIG_DIR="${WOLFSERVE_APACHE_DIR:-$DEFAULT_APACHE_DIR}"
if [ "$AUTO_YES" = false ]; then
echo "π Configuration Options (press Enter to accept defaults)"
echo ""
# Server settings
read -p "Server bind address [$SERVER_HOST]: " INPUT
SERVER_HOST="${INPUT:-$SERVER_HOST}"
read -p "Server port [$SERVER_PORT]: " INPUT
SERVER_PORT="${INPUT:-$SERVER_PORT}"
# PHP settings
read -p "PHP-FPM port [$PHP_FPM_PORT]: " INPUT
PHP_FPM_PORT="${INPUT:-$PHP_FPM_PORT}"
read -p "PHP session save path [$SESSION_SAVE_PATH]: " INPUT
SESSION_SAVE_PATH="${INPUT:-$SESSION_SAVE_PATH}"
# Apache settings
read -p "Apache config directory [$APACHE_CONFIG_DIR]: " INPUT
APACHE_CONFIG_DIR="${INPUT:-$APACHE_CONFIG_DIR}"
fi
echo ""
echo "π Configuration Summary:"
echo " Server: $SERVER_HOST:$SERVER_PORT"
echo " PHP-FPM: 127.0.0.1:$PHP_FPM_PORT"
echo " Sessions: $SESSION_SAVE_PATH"
echo " Apache dir: $APACHE_CONFIG_DIR"
echo ""
if [ "$AUTO_YES" = false ]; then
read -p "Proceed with installation? [Y/n]: " CONFIRM
CONFIRM="${CONFIRM:-Y}"
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
echo "Installation cancelled."
exit 0
fi
fi
echo ""
echo "πΊ Installing WolfServe as a service..."
# 1. Determine Web User
WEB_USER="www-data"
if id "apache" &>/dev/null; then
WEB_USER="apache"
elif id "nginx" &>/dev/null; then
WEB_USER="nginx"
elif id "http" &>/dev/null; then
WEB_USER="http"
fi
if ! id "$WEB_USER" &>/dev/null; then
echo "β οΈ Web user '$WEB_USER' not found. Creating system user 'wolfserve'..."
useradd -r -s /bin/false wolfserve
WEB_USER="wolfserve"
fi
echo "π€ Service will run as user: $WEB_USER"
# 2. Build Checks
if [ ! -f "target/release/wolfserve" ]; then
echo "π¨ wolfserve binary not found. Building..."
if command -v cargo &>/dev/null; then
cargo build --release
else
echo "β cargo not found. Please run ./install.sh first or install Rust."
exit 1
fi
fi
if [ ! -f "wolflib/target/release/libwolflib.so" ]; then
echo "π¨ libwolflib.so not found. Building..."
if [ -f "./build_lib.sh" ]; then
sh ./build_lib.sh
else
cd wolflib && cargo build --release && cd ..
fi
fi
# 3. Create Installation Directory
echo "π Creating install directory at $INSTALL_DIR..."
mkdir -p "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR/public"
# 4. Copy Files
echo "π¦ Copying files..."
cp target/release/wolfserve "$INSTALL_DIR/"
cp wolflib/target/release/libwolflib.so "$INSTALL_DIR/"
# Generate config with user-specified options
echo "π Writing configuration to $INSTALL_DIR/wolfserve.toml..."
cat > "$INSTALL_DIR/wolfserve.toml" <<EOF
# WolfServe Configuration
# Generated by install_service.sh on $(date)
[server]
host = "$SERVER_HOST"
port = $SERVER_PORT
[php]
fpm_address = "127.0.0.1:$PHP_FPM_PORT"
mode = "fpm"
session_save_path = "$SESSION_SAVE_PATH"
[apache]
config_dir = "$APACHE_CONFIG_DIR"
EOF
# Copy public files
# Only copy if source public exists
if [ -d "public" ]; then
cp -r public/* "$INSTALL_DIR/public/"
fi
# 5. Fix paths in public/rust.php if it exists
# We want to replace the hardcoded source path with the new install path
RUST_PHP="$INSTALL_DIR/public/rust.php"
if [ -f "$RUST_PHP" ]; then
echo "π§ Updating libwolflib.so path in $RUST_PHP..."
# Escape slashes for sed
ESCAPED_LIB_PATH=$(echo "$INSTALL_DIR/libwolflib.so" | sed 's/\//\\\//g')
# Use generic regex to find $libPath = '...'; assignment
sed -i "s/\$libPath = '.*';/\$libPath = '$ESCAPED_LIB_PATH';/" "$RUST_PHP"
fi
# Set permissions
chown -R $WEB_USER:$WEB_USER "$INSTALL_DIR"
chmod +x "$INSTALL_DIR/wolfserve"
# 6. Create Systemd Service
echo "βοΈ Creating systemd service /etc/systemd/system/$SERVICE_NAME.service..."
cat > /etc/systemd/system/$SERVICE_NAME.service <<EOF
[Unit]
Description=WolfServe High Performance Rust PHP Server
After=network.target php-fpm.service
[Service]
Type=simple
User=$WEB_USER
Group=$WEB_USER
WorkingDirectory=$INSTALL_DIR
ExecStart=$INSTALL_DIR/wolfserve
Restart=always
Environment=RUST_LOG=info
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
EOF
# 7. Configure PHP-FPM Pool
echo "π Configuring PHP-FPM pool for WolfServe..."
FPM_POOL_CONF=""
# Attempt to find pool directory
if [ -d "/etc/php" ]; then
# Debian/Ubuntu style: /etc/php/X.X/fpm/pool.d/
# Find latest version
PHP_VER=$(ls /etc/php/ | sort -V | tail -n1)
if [ -d "/etc/php/$PHP_VER/fpm/pool.d" ]; then
FPM_POOL_CONF="/etc/php/$PHP_VER/fpm/pool.d/wolfserve.conf"
fi
elif [ -d "/etc/php-fpm.d" ]; then
# RHEL/Fedora style
FPM_POOL_CONF="/etc/php-fpm.d/wolfserve.conf"
fi
if [ -n "$FPM_POOL_CONF" ]; then
echo " Writing pool config to $FPM_POOL_CONF..."
# Create session directory if needed
if [ ! -d "$SESSION_SAVE_PATH" ]; then
echo " Creating session directory: $SESSION_SAVE_PATH"
mkdir -p "$SESSION_SAVE_PATH"
fi
chown "$WEB_USER:$WEB_USER" "$SESSION_SAVE_PATH"
chmod 1733 "$SESSION_SAVE_PATH"
echo " β
Session save path: $SESSION_SAVE_PATH"
cat > "$FPM_POOL_CONF" <<EOF
[wolfserve]
user = $WEB_USER
group = $WEB_USER
listen = 127.0.0.1:$PHP_FPM_PORT
listen.owner = $WEB_USER
listen.group = $WEB_USER
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
php_admin_value[session.save_path] = $SESSION_SAVE_PATH
EOF
echo " β
PHP-FPM pool configured."
else
echo "β οΈ Could not locate PHP-FPM pool directory. Please ensure PHP-FPM listens on 127.0.0.1:$PHP_FPM_PORT manually."
fi
# 8. Reload and Start
echo "π Reloading daemons and starting services..."
systemctl daemon-reload
# Restart PHP-FPM to pick up new pool (handles both Fedora/RHEL and Debian/Ubuntu)
PHP_FPM_SVC=""
# Fedora/RHEL uses 'php-fpm', Debian/Ubuntu uses 'phpX.Y-fpm'
for svc in php-fpm php8.3-fpm php8.2-fpm php8.1-fpm php8.0-fpm php7.4-fpm; do
if systemctl list-unit-files 2>/dev/null | grep -q "^$svc\.service"; then
PHP_FPM_SVC="$svc"
break
fi
done
if [ -n "$PHP_FPM_SVC" ]; then
echo " Restarting $PHP_FPM_SVC..."
systemctl enable "$PHP_FPM_SVC" 2>/dev/null || true
systemctl restart "$PHP_FPM_SVC"
else
echo "β οΈ Could not find PHP-FPM service. Please restart php-fpm manually."
fi
# Enable and start WolfServe
systemctl enable $SERVICE_NAME
systemctl restart $SERVICE_NAME
echo "β
Installation complete!"
echo " Server running on http://$(grep -m 1 'host =' $INSTALL_DIR/wolfserve.toml | cut -d '"' -f 2):$(grep -m 1 'port =' $INSTALL_DIR/wolfserve.toml | cut -d '=' -f 2 | tr -d ' ')"
echo " Manage with: systemctl [start|stop|restart|status] $SERVICE_NAME"
echo ""
echo "β οΈ Don't forget to stop Apache and disable it if running on port 80/443:"
echo " systemctl stop apache2 httpd"
echo " systemctl disable apache2 httpd"