> For the complete documentation index, see [llms.txt](https://docs.devonyx.fr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.devonyx.fr/getting-started/deploiement-and-infrastructure.md).

# Déploiement & Infrastructure

### Cible de déploiement

DashFarm est déployé. sur un **VPS Debian 12** avec la stack suivante :&#x20;

**Nginx :** reverse proxy + terminaison TLS

**Node.js + PM2 :** exécution et supervision de l'API

**MySQL :** base de données (locale)

**Certbot :** certificats Let's Encrypt

**UFW :** pare-feu

#### Specs recommandées

| Ressource | Minimum                  | Recommandé |
| --------- | ------------------------ | ---------- |
| CPU       | 1 vCPU                   | 2 vCPU     |
| RAM       | 2 GB                     | 4 GB       |
| Disque    | 20 GB SSD                | 50 GB SSD  |
| OS        | Debian 12 / Ubuntu 22.04 | Debian 12  |

### ÉTAPE 1 : Préparer le serveur

```bash
# Mise à jour
sudo apt update && sudo apt upgrade -y

# Outils de base
sudo apt install -y culr git build-essential

# Node.js 22 LTS via NodeSource
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs

# MySQL
sudo apt install -y mariadb-server
sudo mysql_secure_installation

# NGINX 
sudo apt install -y nginx

# PM2 (en global)
sudo npm install -g pm2
```

### ÉTAPE 2 : Configurer le pare-feu UFW

```bash
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp            #SSH
sudo ufw allow 80/tcp            #HHTP (pour Let's Encrypt)
sudo ufw allow 443/tcp           #HTTPS
sudo ufw enable
sudo ufw status verbose
```

> Le port 3000 (node) **n'est pas exposé :** Nginx fait le proxy en interne.
>
> Le port 3306 (MySQL) **nest pas exposé** non plus : accès uniquement en localhost.

### ÉTAPE 3 : Préparer la base de données

```bash
sudo mariadb
```

```sql
CREATE DATABASE agriculture_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'dashfarm'@'localhost' IDENTIFIED BY 'mot_de_passe_solide';
GRANT ALL PRIVILEGES ON agriculture_db.* TO 'dashfarm'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

```bash
mysql -u dashfarm -p agriculture_db < BDD/schema.sql
mysql -u dashfarm -p agriculture_db < BDD/data.sql   # optionnel
```

### ÉTAPE 4 : Déployer le code

```bash
sudo mkdir -p /var/www
cd /var/www
sudo git clone https://github.com/t3mq/Dashfarm.git 
sudo chown -R $USER:$USER dashfarm
cd dashfarm
```

#### Backend

```bash
cd server
npm ci --omit=dev
```

Créer `/var/www/dashfarm/server/.env`

```dotenv
DB_HOST=localhost
DB_PORT=3306
DB_USER=dashfarm
DB_PASSWORD=mot_de_passe_solide
DB_NAME=agriculture_db
PORT=3000
JWT_SECRET=<génère avec : openssl rand -hex 32>
JWT_EXPIRES_IN=7d
```

#### Frontend

```bash
cd ../client
npm ci
npm run build
```

Le build se trouve dans `client/dist`&#x20;

### ÉTAPE 5 : Lancer l'API avec PM2

```bash
cd /var/www/dashfarm/server
pm2 start src/index.js --name dashfarm-api
pm2 save
pm2 startup    # suit les instructions affichées pour activer au boot
```

Commandes utiles :&#x20;

```bash
pm2 list                # statut des processus
pm2 logs dashfarm-api   # logs en temps réel
pm2 restart dashfarm-api
pm2 stop dashfarm-api
pm2 monit               # monitoring interactif
```

### ÉTAPE 6 : Configurer Nginx

Créer `/etc/nginx/sites-availables/dashfarm`

```nginx
server {
    listen 80;
    server_name ton-domaine.fr www.ton-domaine.fr;

    # Frontend statique
    root /var/www/dashfarm/client/dist;
    index index.html;

    # SPA fallback (toutes les routes React renvoient index.html)
    location / {
        try_files $uri $uri/ /index.html;
    }
    # Proxy API
    location /api/ {
        proxy_pass http://localhost:3000/api/;
        proxy_http_version 1.1;
        proxy_set_header Host              $host;
        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;
    }
}
```

Activer le site :&#x20;

```bash
sudo ln -s /etc/nginx/sites-available/dashfarm /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
```

### ÉTAPE 7 : TLS avec Let's Encrypt

```bash
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d ton-domaine.fr -d www.ton-domaine.fr
```

Certbot modifie automatiquement la config Nginx pour ajouter HTTPS et la redirection HTTP -> HTTPS. Le renouvellement est automatique via un timer.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.devonyx.fr/getting-started/deploiement-and-infrastructure.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
