bts sio slam → immoform

Configurer et déployer une application .NET et Symfony sur Linux

1. Configuration

Backend .NET

ASP.NET Core empile plusieurs sources ; la dernière lue gagne :

flowchart LR
    A[appsettings.json] --> B["appsettings.{Environment}.json"] --> C["User Secrets<br/>(Development)"] --> D[Variables d'environnement] --> E[Ligne de commande]

L'environnement vient de ASPNETCORE_ENVIRONMENT (Development via launchSettings.json en local, Production sur le serveur).

ProjetCléRôle
Immoform.APIConnectionStrings:DefaultConnectionChaîne SQL Server lue par Program.cs
Immoform.APIConnectionStrings:ConnectionStringChaîne lue par AddInfrastructureServices
Immoform.ClientApiBaseURL de l'API (ex. http://localhost:5074/)

Chaîne SQL Server type : Server=<serveur>;Database=<base>;User Id=<utilisateur>;Password=<mot_de_passe>;TrustServerCertificate=True

User Secrets : les deux projets web ont un <UserSecretsId>. On stocke la chaîne de connexion hors du dépôt (~/.microsoft/usersecrets/<id>/secrets.json, lu seulement en Development) :

cd Immoform_Backend/Immoform.API
dotnet user-secrets set "ConnectionStrings:DefaultConnection" "Server=localhost;Database=Immoform;..."
dotnet user-secrets list

Frontend Symfony

Fichier (le suivant écrase le précédent)Commité ?Usage
.env✅Valeurs par défaut, non secrètes
.env.local❌Surcharges de ta machine
.env.{APP_ENV}✅Valeurs d'un environnement
.env.{APP_ENV}.local (ex. .env.prod.local)❌Surcharges locales d'un environnement
VariableRôleExemple (fictif)
APP_ENVEnvironnementdev / prod
APP_SECRETSignature des jetons (CSRF, cookies…)chaîne aléatoire
DATABASE_URLBase MySQL des comptesmysql://<user>:<mdp>@127.0.0.1:3306/immoform?serverVersion=8.0
CLIENT_API_URLURL de l'API, injectée via $clientApiUrlhttp://localhost:5074
MESSENGER_TRANSPORT_DSN, MAILER_DSN, DEFAULT_URI, APP_SHARE_DIRTechniques (Messenger / Mailer installés mais inutilisés)

php bin/console debug:dotenv affiche les variables chargées.


2. Lancer le projet en local

Pré-requis : SDK .NET 10 + dotnet-ef, SQL Server, PHP 8.4, Composer, Symfony CLI, Node.js + npm, MySQL.

flowchart LR
    A[1. SQL Server + MySQL] --> B[2. Immoform.API :5074] --> C[3. Immoform.Client :5265]
    B --> D[3. Symfony]
# 1. API
cd Immoform_Backend
dotnet user-secrets set "ConnectionStrings:DefaultConnection" "<chaîne SQL Server>" --project Immoform.API
dotnet ef database update --project Immoform.Infrastructure --startup-project Immoform.API
dotnet run --project Immoform.API            # http://localhost:5074  (test : /scalar)

# 2. Back-office (autre terminal) — renseigner ApiBase dans appsettings.Development.json
dotnet run --project Immoform.Client         # http://localhost:5265

# 3. Portail (autre terminal) — créer .env.local avec DATABASE_URL et CLIENT_API_URL
cd Immoform_Frontend
composer install && npm install
php bin/console doctrine:database:create --if-not-exists
php bin/console doctrine:migrations:migrate
npm run watch                                # laisser tourner
symfony serve                                # autre terminal

3. Déploiement du backend

flowchart LR
    DEV[Poste développeur] -- "rsync + ssh" --> SRV
    subgraph SRV["Serveur Linux"]
        D["/var/www/ImmoformBackend<br/>api/ + client/"]
        S1["systemd : ImmoformApi"] --> D
        S2["systemd : ImmoformClient"] --> D
    end
    S1 --> DB[(SQL Server)]

Immoform_Backend/deploy-API.sh (valeurs masquées) :

#!/bin/bash
USER="<utilisateur>"; SERVER="<ip_serveur>"; DEST_DIR="/var/www/ImmoformBackend"

# 1. Publication
dotnet publish Immoform.API/Immoform.API.csproj -c Release -o ./publish/api
dotnet publish Immoform.Client/Immoform.Client.csproj -c Release -o ./publish/client
# 2. Arrêt des services
ssh $USER@$SERVER "sudo systemctl stop ImmoformApi"
ssh $USER@$SERVER "sudo systemctl stop ImmoformClient"
# 3. Synchronisation
rsync -avz --delete -e ssh ./publish/ $USER@$SERVER:$DEST_DIR
# 4. Migrations
export ASPNETCORE_ENVIRONMENT=Production
dotnet ef database update --project "Immoform.Infrastructure/Immoform.Infrastructure.csproj" \
  --startup-project "Immoform.API/Immoform.API.csproj" --context AppDbContext
# 5. Redémarrage
ssh $USER@$SERVER "sudo systemctl start ImmoformApi"
ssh $USER@$SERVER "sudo systemctl start ImmoformClient"
#Explication
1dotnet publish -c Release compile en mode optimisé et rassemble tout le nécessaire (DLL, dépendances, appsettings*.json, wwwroot).
2On ne remplace pas des DLL en cours d'utilisation.
3rsync ne copie que les différences (-a attributs, -v verbeux, -z compression) ; --delete supprime sur le serveur ce qui n'existe plus localement.
4Production fait lire appsettings.Production.json. ⚠️ La commande s'exécute sur le poste du développeur : il doit pouvoir joindre la base de production.
5Redémarre avec la nouvelle version.

Le service systemd

Non versionné (sur le serveur, typiquement /etc/systemd/system/ImmoformApi.service). Forme habituelle :

[Unit]
Description=Immoform API

[Service]
WorkingDirectory=/var/www/ImmoformBackend/api
ExecStart=/usr/bin/dotnet /var/www/ImmoformBackend/api/Immoform.API.dll
Restart=always
RestartSec=10
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=ASPNETCORE_URLS=http://localhost:5074

[Install]
WantedBy=multi-user.target
sudo systemctl status ImmoformApi     # état
sudo journalctl -u ImmoformApi -f     # logs en direct
sudo systemctl daemon-reload          # après modification du .service
sudo systemctl enable ImmoformApi     # démarrage au boot

4. Déploiement du frontend

Immoform_Frontend/deploy.sh (valeurs masquées) :

#!/bin/sh
rsync -e 'ssh' -S -av ./ <utilisateur>@<ip_serveur>:/var/www/Immoform \
      --include="public/.htaccess" --include=".env" --include=".env.prod.local" \
      --exclude-from=".gitignore" --exclude=".*"

ssh <utilisateur>@<ip_serveur> -o "StrictHostKeyChecking=no" <<'eof'
cd /var/www/Immoform
mv .env.prod.local .env.local
composer install
symfony console --no-interaction doctrine:database:create --if-not-exists
symfony console --no-interaction doctrine:migrations:migrate
npm run build
eof
ÉlémentExplication
--exclude-from=".gitignore"N'envoie pas vendor/, var/, node_modules/…
--exclude=".*" + --include=...Exclut les fichiers cachés sauf ceux nécessaires. Les --include doivent précéder les --exclude (rsync applique la première règle qui correspond).
<<'eof' ... eofHeredoc : envoie le bloc au shell distant ; les guillemets empêchent l'interprétation des variables en local.
mv .env.prod.local .env.localLa config de production prend la place de .env.local, prioritaire sur .env.
--no-interactionAucune question (indispensable dans un script).
npm run buildAssets minifiés et versionnés.

Serveur web. public/.htaccess (symfony/apache-pack) indique Apache. Le DocumentRoot doit pointer sur public/, sinon .env et le code source seraient accessibles depuis le web :

<VirtualHost *:80>
    ServerName immoform.example
    DocumentRoot /var/www/Immoform/public
    <Directory /var/www/Immoform/public>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Après un déploiement, si une modification n'apparaît pas : php bin/console cache:clear --env=prod (Symfony met en cache configuration, routes et templates ; composer install le fait normalement via ses auto-scripts).