优化install.sh

This commit is contained in:
CN-JS-HuiBai
2026-04-04 22:15:38 +08:00
parent 415334ad73
commit 4f04227976

View File

@@ -6,6 +6,7 @@
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
@@ -25,18 +26,37 @@ USER_HOME=$(getent passwd "$REAL_USER" | cut -d: -f6)
echo -e "Project Directory: ${GREEN}$PROJECT_DIR${NC}"
echo -e "Running User: ${GREEN}$REAL_USER${NC}"
# 3. Check for dependencies
if ! command -v node &> /dev/null; then
echo -e "${RED}Node.js is not installed. Please install Node.js first.${NC}"
# 3. Check for mandatory files
if [ ! -f "server/index.js" ]; then
echo -e "${RED}Error: server/index.js not found. Please run this script from the project root.${NC}"
exit 1
fi
if ! command -v npm &> /dev/null; then
echo -e "${RED}NPM is not installed. Please install NPM first.${NC}"
# 4. Check for dependencies
echo -e "${BLUE}Checking dependencies...${NC}"
check_dep() {
if ! command -v "$1" &> /dev/null; then
echo -e "${RED}$1 is not installed. Please install $1 first.${NC}"
exit 1
fi
}
check_dep node
check_dep npm
# 5. Check for .env file
if [ ! -f ".env" ]; then
echo -e "${YELLOW}Warning: .env file not found.${NC}"
if [ -f ".env.example" ]; then
echo -e "Creating .env from .env.example..."
cp .env.example .env
chown "$REAL_USER":"$REAL_USER" .env
echo -e "${GREEN}Created .env file. Please ensure values are correct.${NC}"
else
echo -e "${RED}Error: .env.example not found. Configuration missing.${NC}"
fi
fi
# 4. Install NPM dependencies
# 6. Install NPM dependencies
echo -e "${BLUE}Installing dependencies...${NC}"
# Run npm install as the real user to avoid permission issues in node_modules
sudo -u "$REAL_USER" npm install
@@ -46,7 +66,7 @@ if [ $? -ne 0 ]; then
exit 1
fi
# 5. Create Systemd Service File
# 7. Create Systemd Service File
SERVICE_FILE="/etc/systemd/system/data-wall.service"
NODE_PATH=$(command -v node)
@@ -55,7 +75,8 @@ echo -e "${BLUE}Creating systemd service at $SERVICE_FILE...${NC}"
cat <<EOF > "$SERVICE_FILE"
[Unit]
Description=Data Visualization Display Wall
After=network.target mysql.service
After=network.target mysql.service redis-server.service valkey-server.service
Wants=mysql.service
[Service]
Type=simple
@@ -67,28 +88,37 @@ RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=data-wall
# Pass environment via .env file injection for flexibility
# Pass environment via .env file injection
EnvironmentFile=-$PROJECT_DIR/.env
Environment=NODE_ENV=production
# Security Hardening
CapabilityBoundingSet=
NoNewPrivileges=true
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
# 6. Reload Systemd and Start
echo -e "${BLUE}Reloading systemd and starting service...${NC}"
# 8. Reload Systemd and Start
echo -e "${BLUE}Reloading systemd and restarting service...${NC}"
systemctl daemon-reload
systemctl enable data-wall
systemctl stop data-wall # Stop if already running
systemctl start data-wall
systemctl restart data-wall
# 7. Check Status
# 9. Check Status
echo -e "${BLUE}Checking service status...${NC}"
sleep 2
if systemctl is-active --quiet data-wall; then
echo -e "${GREEN}SUCCESS: Service is now running.${NC}"
echo -e "You can access the dashboard at http://localhost:3000"
echo -e "View logs with: ${BLUE}journalctl -u data-wall -f${NC}"
PORT=$(grep "^PORT=" .env | cut -d'=' -f2)
PORT=${PORT:-3000}
echo -e "Dashboard URL: ${YELLOW}http://localhost:${PORT}${NC}"
echo -e "View logs: ${BLUE}journalctl -u data-wall -f${NC}"
else
echo -e "${RED}FAILED: Service failed to start. Check logs with 'journalctl -u data-wall -xe'${NC}"
echo -e "${RED}FAILED: Service failed to start.${NC}"
echo -e "Check logs with: ${BLUE}journalctl -u data-wall -xe${NC}"
fi
echo -e "${BLUE}================================================${NC}"