Improve how the application starts to ensure environment variables are loaded correctly

Modify deployment/check_frontend.sh to use `env $(cat .env | grep -v '^#' | xargs) npm run dev` to inject environment variables into the npm process, resolving issues where `nohup` did not inherit exported variables. Update replit.md to reflect the corrected fix details.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 602cd9af-422f-4b1d-ab37-5fd3e0bd016f
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/dI1I79r
This commit is contained in:
marco370 2025-11-21 15:58:17 +00:00
parent aec912cf16
commit e5d038a2b4
3 changed files with 145 additions and 11 deletions

View File

@ -0,0 +1,132 @@
cat /opt/ids/.env | grep DATABASE_URL
DATABASE_URL=postgresql://ids_user:TestPassword123@127.0.0.1:5432/ids_database
[root@ids ids]# psql "$(grep DATABASE_URL /opt/ids/.env | cut -d= -f2)"
psql (13.22)
Type "help" for help.
ids_database=>
ABORT CLUSTER DELETE FROM FETCH MOVE RESET SHOW VALUES
ALTER COMMENT DISCARD GRANT NOTIFY REVOKE START WITH
ANALYZE COMMIT DO IMPORT PREPARE ROLLBACK TABLE
BEGIN COPY DROP INSERT REASSIGN SAVEPOINT TRUNCATE
CALL CREATE END LISTEN REFRESH MATERIALIZED VIEW SECURITY LABEL UNLISTEN
CHECKPOINT DEALLOCATE EXECUTE LOAD REINDEX SELECT UPDATE
CLOSE DECLARE EXPLAIN LOCK RELEASE SET VACUUM
ids_database=> close
ids_database-> ^Z
[3]+ Stopped psql "$(grep DATABASE_URL /opt/ids/.env | cut -d= -f2)"
[root@ids ids]# nano .env
[root@ids ids]# psql "$(grep DATABASE_URL /opt/ids/.env | cut -d= -f2)"
psql (13.22)
Type "help" for help.
ids_database=> ^Z
[4]+ Stopped psql "$(grep DATABASE_URL /opt/ids/.env | cut -d= -f2)"
[root@ids ids]# tail -100 /var/log/ids/frontend.log
4:46:58 PM [express] serving on port 5000
A PostCSS plugin did not pass the `from` option to `postcss.parse`. This may cause imported assets to be incorrectly transformed. If you've recently added a PostCSS plugin that raised this warning, please contact the package author to fix the issue.
4:49:03 PM [express] GET /api/stats 500 in 39ms :: {"error":"Failed to fetch stats"}
4:49:03 PM [express] GET /api/detections 500 in 13ms :: {"error":"Failed to fetch detections"}
4:49:03 PM [express] GET /api/routers 500 in 11ms :: {"error":"Failed to fetch routers"}
4:49:06 PM [express] GET /api/stats 500 in 11ms :: {"error":"Failed to fetch stats"}
4:49:06 PM [express] GET /api/detections 500 in 8ms :: {"error":"Failed to fetch detections"}
4:49:06 PM [express] GET /api/routers 500 in 6ms :: {"error":"Failed to fetch routers"}
4:49:08 PM [express] GET /api/detections 500 in 4ms :: {"error":"Failed to fetch detections"}
4:49:10 PM [express] GET /api/training-history 500 in 42ms :: {"error":"Failed to fetch training his…
4:49:10 PM [express] GET /api/ml/stats 200 in 70ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:49:15 PM [express] POST /api/ml/train 200 in 10ms :: {"message":"Training avviato in background","…
4:49:15 PM [express] GET /api/training-history 500 in 6ms :: {"error":"Failed to fetch training hist…
4:49:15 PM [express] GET /api/ml/stats 200 in 14ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:49:21 PM [express] GET /api/training-history 500 in 7ms :: {"error":"Failed to fetch training hist…
4:49:21 PM [express] GET /api/ml/stats 200 in 20ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:49:27 PM [express] GET /api/training-history 500 in 6ms :: {"error":"Failed to fetch training hist…
4:49:27 PM [express] GET /api/ml/stats 200 in 20ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:49:31 PM [express] GET /api/training-history 500 in 5ms :: {"error":"Failed to fetch training hist…
4:49:31 PM [express] GET /api/ml/stats 200 in 15ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:49:33 PM [express] GET /api/routers 500 in 5ms :: {"error":"Failed to fetch routers"}
4:49:36 PM [express] GET /api/whitelist 500 in 4ms :: {"error":"Failed to fetch whitelist"}
4:49:38 PM [express] GET /api/routers 500 in 4ms :: {"error":"Failed to fetch routers"}
4:49:39 PM [express] GET /api/training-history 500 in 7ms :: {"error":"Failed to fetch training hist…
4:49:39 PM [express] GET /api/ml/stats 200 in 20ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:49:41 PM [express] GET /api/detections 500 in 4ms :: {"error":"Failed to fetch detections"}
4:49:42 PM [express] GET /api/stats 500 in 4ms :: {"error":"Failed to fetch stats"}
4:49:43 PM [express] GET /api/detections 500 in 6ms :: {"error":"Failed to fetch detections"}
4:49:43 PM [express] GET /api/routers 500 in 4ms :: {"error":"Failed to fetch routers"}
4:49:44 PM [express] GET /api/training-history 500 in 5ms :: {"error":"Failed to fetch training hist…
4:49:44 PM [express] GET /api/ml/stats 200 in 19ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:49:54 PM [express] GET /api/training-history 500 in 8ms :: {"error":"Failed to fetch training hist…
4:49:54 PM [express] GET /api/ml/stats 304 in 20ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:49:57 PM [express] GET /api/detections 500 in 4ms :: {"error":"Failed to fetch detections"}
4:49:59 PM [express] GET /api/training-history 500 in 5ms :: {"error":"Failed to fetch training hist…
4:49:59 PM [express] GET /api/ml/stats 304 in 19ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:50:00 PM [express] GET /api/routers 500 in 3ms :: {"error":"Failed to fetch routers"}
4:50:01 PM [express] GET /api/whitelist 500 in 4ms :: {"error":"Failed to fetch whitelist"}
4:50:02 PM [express] GET /api/training-history 500 in 6ms :: {"error":"Failed to fetch training hist…
4:50:02 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:50:12 PM [express] GET /api/training-history 500 in 5ms :: {"error":"Failed to fetch training hist…
4:50:12 PM [express] GET /api/ml/stats 304 in 18ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:50:22 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:50:22 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:50:32 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:50:33 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:50:42 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:50:43 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:50:52 PM [express] GET /api/training-history 500 in 5ms :: {"error":"Failed to fetch training hist…
4:50:53 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:51:02 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:51:03 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:51:12 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:51:13 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:51:23 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:51:23 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:51:33 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:51:33 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:51:37 PM [express] GET /api/detections 500 in 4ms :: {"error":"Failed to fetch detections"}
4:51:39 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:51:39 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:51:40 PM [express] GET /api/routers 500 in 4ms :: {"error":"Failed to fetch routers"}
4:51:42 PM [express] GET /api/whitelist 500 in 4ms :: {"error":"Failed to fetch whitelist"}
4:51:43 PM [express] GET /api/training-history 500 in 7ms :: {"error":"Failed to fetch training hist…
4:51:43 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:51:53 PM [express] GET /api/training-history 500 in 5ms :: {"error":"Failed to fetch training hist…
4:51:53 PM [express] GET /api/ml/stats 304 in 18ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:52:03 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:52:03 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:52:13 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:52:13 PM [express] GET /api/ml/stats 304 in 18ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:52:23 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:52:23 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:52:33 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:52:33 PM [express] GET /api/ml/stats 304 in 15ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:52:43 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:52:43 PM [express] GET /api/ml/stats 304 in 15ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:52:53 PM [express] GET /api/training-history 500 in 5ms :: {"error":"Failed to fetch training hist…
4:52:53 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:53:03 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:53:03 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:53:13 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:53:13 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:53:23 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:53:23 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:53:33 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:53:33 PM [express] GET /api/ml/stats 304 in 19ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:53:43 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:53:43 PM [express] GET /api/ml/stats 304 in 18ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:53:53 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:53:53 PM [express] GET /api/ml/stats 304 in 17ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:54:03 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:54:03 PM [express] GET /api/ml/stats 304 in 15ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:54:13 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:54:13 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:54:23 PM [express] GET /api/training-history 500 in 3ms :: {"error":"Failed to fetch training hist…
4:54:23 PM [express] GET /api/ml/stats 304 in 20ms :: {"logs":{"total":0,"last_hour":0},"detections"…
4:54:33 PM [express] GET /api/training-history 500 in 4ms :: {"error":"Failed to fetch training hist…
4:54:33 PM [express] GET /api/ml/stats 304 in 16ms :: {"logs":{"total":0,"last_hour":0},"detections"…
[root@ids ids]# psql -h 127.0.0.1 -U ids_user -d ids_database
Password for user ids_user:
psql (13.22)
Type "help" for help.
ids_database=> ^Z
[5]+ Stopped psql -h 127.0.0.1 -U ids_user -d ids_database

View File

@ -25,14 +25,15 @@ else
# Wait a moment
sleep 2
# Load environment variables from .env file
set -a
source "$WORK_DIR/.env" 2>/dev/null || true
set +a
# Start frontend
# Start frontend with environment variables from .env
cd "$WORK_DIR"
nohup npm run dev >> "$LOG_FILE" 2>&1 &
if [ -f "$WORK_DIR/.env" ]; then
# Load .env and start npm with those variables
nohup env $(cat "$WORK_DIR/.env" | grep -v '^#' | xargs) npm run dev >> "$LOG_FILE" 2>&1 &
else
# Fallback: start without .env (will use system env vars)
nohup npm run dev >> "$LOG_FILE" 2>&1 &
fi
NEW_PID=$!
echo $NEW_PID > "$PID_FILE"

View File

@ -43,12 +43,13 @@ Sistema di rilevamento intrusioni per router MikroTik basato su Machine Learning
## Fix Recenti (Novembre 2025)
### ✅ Frontend Environment Variables Fix (21 Nov 2025 - 16:30)
### ✅ Frontend Environment Variables Fix (21 Nov 2025 - 17:00)
- **Problema**: Frontend Node.js non si avviava su server AlmaLinux con errore `DATABASE_URL must be set`
- **Causa**: Script `check_frontend.sh` non caricava variabili d'ambiente dal file `.env`
- **Causa 1**: Script `check_frontend.sh` non caricava variabili d'ambiente dal file `.env`
- **Causa 2**: Il comando `nohup` crea un nuovo processo che non eredita variabili esportate con `source`
- **Soluzione**:
- Modificato `deployment/check_frontend.sh` per caricare `.env` prima di eseguire `npm run dev`
- Usato `set -a; source .env; set +a` per esportare tutte le variabili
- Modificato `deployment/check_frontend.sh` per passare variabili direttamente al comando npm
- Usato `env $(cat .env | grep -v '^#' | xargs) npm run dev` per iniettare variabili nel processo
- **Risultato**: Frontend si avvia correttamente leggendo `DATABASE_URL` e altre variabili da `.env`
### ✅ Form Validation Migliorata (21 Nov 2025 - 15:00)