Skip to content

Commit 6f417e5

Browse files
committed
Enhance AppAPI configuration script to improve Apache vhost detection and proxy setup for ExApps
1 parent 1d21aea commit 6f417e5

File tree

1 file changed

+91
-37
lines changed

1 file changed

+91
-37
lines changed

addons/appapi.sh

Lines changed: 91 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,46 @@ then
110110
fi
111111

112112
# Remove Apache proxy configuration for ExApps
113-
if [ -f "$SITES_AVAILABLE/$NCDOMAIN.conf" ] 2>/dev/null
113+
# Find Apache vhost config - check expected location first
114+
VHOST_CONF=""
115+
if [ -f "$SITES_AVAILABLE/$NCDOMAIN.conf" ]
116+
then
117+
VHOST_CONF="$SITES_AVAILABLE/$NCDOMAIN.conf"
118+
else
119+
# Try to find in enabled sites
120+
if [ -d "/etc/apache2/sites-enabled" ]
121+
then
122+
# Look for config containing ExApps-HaRP marker
123+
for conf_file in /etc/apache2/sites-enabled/*.conf
124+
do
125+
if [ -f "$conf_file" ] && grep -q "#ExApps-HaRP" "$conf_file"
126+
then
127+
VHOST_CONF="$conf_file"
128+
break
129+
fi
130+
done
131+
fi
132+
fi
133+
134+
# Remove Include directive from vhost if found
135+
if [ -n "$VHOST_CONF" ] && [ -f "$VHOST_CONF" ]
114136
then
115-
if grep -q "#ExApps-HaRP-start" "$SITES_AVAILABLE/$NCDOMAIN.conf"
137+
if grep -q "#ExApps-HaRP" "$VHOST_CONF"
116138
then
117-
print_text_in_color "$ICyan" "Removing Apache ExApps proxy configuration..."
118-
sed -i "/#ExApps-HaRP-start/,/#ExApps-HaRP-end/d" "$SITES_AVAILABLE/$NCDOMAIN.conf"
139+
print_text_in_color "$ICyan" "Removing Apache ExApps proxy configuration from: $VHOST_CONF"
140+
sed -i "/#ExApps-HaRP/d" "$VHOST_CONF"
141+
sed -i "\|Include /etc/apache2/exapps-harp.conf|d" "$VHOST_CONF"
119142
systemctl restart apache2
120143
fi
121144
fi
122145

146+
# Remove separate ExApps config file
147+
if [ -f "/etc/apache2/exapps-harp.conf" ]
148+
then
149+
print_text_in_color "$ICyan" "Removing ExApps Apache config file..."
150+
rm -f /etc/apache2/exapps-harp.conf
151+
fi
152+
123153
# Optionally remove HaRP data
124154
if [ -d "/var/lib/appapi-harp" ]
125155
then
@@ -342,60 +372,84 @@ Check logs: docker logs $HARP_CONTAINER_NAME"
342372
# Configure Apache reverse proxy for /exapps/
343373
print_text_in_color "$ICyan" "Configuring Apache reverse proxy for ExApps..."
344374

345-
# Check if Apache vhost exists
375+
# Enable proxy modules
376+
a2enmod proxy proxy_http proxy_wstunnel &>/dev/null
377+
378+
# Create separate ExApps config file
379+
EXAPPS_CONF="/etc/apache2/exapps-harp.conf"
380+
cat << APACHE_EXAPPS_CONF > "$EXAPPS_CONF"
381+
# AppAPI ExApps Reverse Proxy Configuration
382+
# This file is managed by the Nextcloud VM AppAPI configuration script
383+
# Do not modify manually - changes may be overwritten
384+
385+
ProxyPass /exapps/ http://127.0.0.1:8780/exapps/
386+
ProxyPassReverse /exapps/ http://127.0.0.1:8780/exapps/
387+
APACHE_EXAPPS_CONF
388+
389+
# Find Apache vhost config - check expected location first
390+
VHOST_CONF=""
346391
if [ -f "$SITES_AVAILABLE/$NCDOMAIN.conf" ]
347392
then
348-
# Check if /exapps/ proxy already configured
349-
if ! grep -q "ProxyPass /exapps/" "$SITES_AVAILABLE/$NCDOMAIN.conf"
393+
VHOST_CONF="$SITES_AVAILABLE/$NCDOMAIN.conf"
394+
else
395+
# Try to find in enabled sites
396+
if [ -d "/etc/apache2/sites-enabled" ]
350397
then
351-
# Enable proxy modules
352-
a2enmod proxy proxy_http proxy_wstunnel &>/dev/null
353-
354-
# Add proxy configuration for ExApps
355-
cat << APACHE_EXAPPS_CONF > /tmp/exapps_proxy.conf
356-
#ExApps-HaRP-start - Please don't remove or change this line
357-
ProxyPass /exapps/ http://127.0.0.1:8780/exapps/
358-
ProxyPassReverse /exapps/ http://127.0.0.1:8780/exapps/
359-
#ExApps-HaRP-end - Please don't remove or change this line
360-
APACHE_EXAPPS_CONF
361-
362-
# Insert after <VirtualHost *:443>
363-
if grep -q "<VirtualHost \*:443>" "$SITES_AVAILABLE/$NCDOMAIN.conf"
398+
# Look for config containing VirtualHost and DocumentRoot pointing to Nextcloud
399+
for conf_file in /etc/apache2/sites-enabled/*.conf
400+
do
401+
if [ -f "$conf_file" ] && grep -q "VirtualHost" "$conf_file" && grep -q "$NCPATH" "$conf_file" 2>/dev/null
402+
then
403+
VHOST_CONF="$conf_file"
404+
break
405+
fi
406+
done
407+
fi
408+
fi
409+
410+
# Configure Apache if we found a vhost
411+
if [ -n "$VHOST_CONF" ]
412+
then
413+
# Check if Include for ExApps config already exists
414+
if ! grep -q "Include.*exapps-harp.conf" "$VHOST_CONF"
415+
then
416+
# Add Include directive after <VirtualHost *:443>
417+
if grep -q "<VirtualHost \*:443>" "$VHOST_CONF"
364418
then
365-
sed -i '/<VirtualHost \*:443>/r /tmp/exapps_proxy.conf' "$SITES_AVAILABLE/$NCDOMAIN.conf"
419+
sed -i "/<VirtualHost \*:443>/a\ #ExApps-HaRP - Please don't remove or change this line\n Include $EXAPPS_CONF" "$VHOST_CONF"
366420
else
367421
# Try port 80 if no 443
368-
sed -i '/<VirtualHost \*:80>/r /tmp/exapps_proxy.conf' "$SITES_AVAILABLE/$NCDOMAIN.conf"
422+
sed -i "/<VirtualHost \*:80>/a\ #ExApps-HaRP - Please don't remove or change this line\n Include $EXAPPS_CONF" "$VHOST_CONF"
369423
fi
370-
rm -f /tmp/exapps_proxy.conf
371424

372425
# Restart Apache
373426
if ! systemctl restart apache2
374427
then
375428
msg_box "Failed to restart Apache. Restoring config..."
376-
sed -i "/#ExApps-HaRP-start/,/#ExApps-HaRP-end/d" "$SITES_AVAILABLE/$NCDOMAIN.conf"
429+
sed -i "/#ExApps-HaRP/d" "$VHOST_CONF"
430+
sed -i "\|Include $EXAPPS_CONF|d" "$VHOST_CONF"
431+
rm -f "$EXAPPS_CONF"
377432
systemctl restart apache2
378433
docker rm -f "$HARP_CONTAINER_NAME"
379434
exit 1
380435
fi
381436

382-
print_text_in_color "$IGreen" "Apache proxy configured successfully!"
437+
print_text_in_color "$IGreen" "Apache proxy configured successfully in: $VHOST_CONF"
383438
else
384439
print_text_in_color "$ICyan" "ExApps proxy already configured in Apache."
385440
fi
386441
else
387-
msg_box "Warning: Apache vhost file not found at $SITES_AVAILABLE/$NCDOMAIN.conf
388-
389-
You need to manually configure your reverse proxy to forward /exapps/ to http://127.0.0.1:8780
390-
391-
Example for NGINX:
392-
location /exapps/ {
393-
proxy_pass http://127.0.0.1:8780;
394-
proxy_set_header Host \$host;
395-
proxy_set_header X-Real-IP \$remote_addr;
396-
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
397-
proxy_set_header X-Forwarded-Proto \$scheme;
398-
}
442+
msg_box "Warning: Could not find Apache vhost configuration for Nextcloud.
443+
444+
Required Apache modules have been enabled and ExApps proxy configuration
445+
has been created at: $EXAPPS_CONF
446+
447+
You need to manually include this file in your Apache vhost:
448+
Include $EXAPPS_CONF
449+
450+
Or manually add the proxy configuration:
451+
ProxyPass /exapps/ http://127.0.0.1:8780/exapps/
452+
ProxyPassReverse /exapps/ http://127.0.0.1:8780/exapps/
399453
400454
Press OK to continue with daemon registration."
401455
fi

0 commit comments

Comments
 (0)