transmission-rss-manager/modules/service-setup-module.sh

83 lines
2.4 KiB
Bash

#!/bin/bash
# Service setup module for Transmission RSS Manager Installation
# Setup systemd service
function setup_service() {
echo -e "${YELLOW}Setting up systemd service...${NC}"
# Create systemd service file
cat > /etc/systemd/system/$SERVICE_NAME.service << EOF
[Unit]
Description=Transmission RSS Manager
After=network.target
Wants=network-online.target
[Service]
Type=simple
User=$USER
WorkingDirectory=$INSTALL_DIR
ExecStart=/usr/bin/node $INSTALL_DIR/server.js
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
Environment=PORT=$PORT
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
EOF
# Create nginx configuration for proxy
echo -e "${YELLOW}Setting up Nginx reverse proxy...${NC}"
# Check if default nginx file exists, back it up if it does
if [ -f /etc/nginx/sites-enabled/default ]; then
mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.bak
echo "Backed up default nginx configuration."
fi
# Create nginx configuration
cat > /etc/nginx/sites-available/$SERVICE_NAME << EOF
server {
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.1:$PORT;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
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;
}
}
EOF
# Create symbolic link to enable the site
ln -sf /etc/nginx/sites-available/$SERVICE_NAME /etc/nginx/sites-enabled/
# Test nginx configuration
nginx -t
if [ $? -eq 0 ]; then
# Reload nginx
systemctl reload nginx
echo -e "${GREEN}Nginx configuration has been set up successfully.${NC}"
else
echo -e "${RED}Nginx configuration test failed. Please check the configuration manually.${NC}"
echo -e "${YELLOW}You may need to correct the configuration before the web interface will be accessible.${NC}"
fi
# Reload systemd
systemctl daemon-reload
# Enable the service to start on boot
systemctl enable $SERVICE_NAME
echo -e "${GREEN}Systemd service has been created and enabled.${NC}"
echo -e "${YELLOW}The service will start automatically after installation.${NC}"
}