# 🌐 Publier un serveur web local avec localhost.run ## 🎯 Objectif Rendre accessible sur Internet un site web ou une application web hĂ©bergĂ©e localement (par exemple avec `python -m http.server` ou Streamlit), grĂące Ă  un **tunnel SSH** via [localhost.run](https://localhost.run). --- ## đŸ§© 1. DĂ©marrer un petit serveur web local Placez-vous dans le dossier contenant votre fichier `index.html`, puis lancez : ```bash python3 -m http.server 8501 ``` - Le serveur Ă©coute sur le port **8501** - VĂ©rifiez sur : [http://localhost:8501](http://localhost:8501) --- ## 🔑 2. CrĂ©er une clĂ© SSH (une seule fois par machine) Une **clĂ© SSH** permet de s’authentifier de façon sĂ©curisĂ©e auprĂšs de `localhost.run` sans mot de passe. ### ➀ Étape 1 : GĂ©nĂ©ration de la clĂ© ```bash ssh-keygen -t ed25519 -C "localhost.run" ``` - Appuyez sur **EntrĂ©e** Ă  chaque question pour accepter les valeurs par dĂ©faut. - Les fichiers sont créés dans : - `~/.ssh/id_ed25519` → clĂ© **privĂ©e** - `~/.ssh/id_ed25519.pub` → clĂ© **publique** ### ➀ Étape 2 : DĂ©marrer l’agent SSH et ajouter la clĂ© ```bash eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 ``` ### ➀ Étape 3 : Copier la clĂ© publique ```bash cat ~/.ssh/id_ed25519.pub ``` Copiez la ligne affichĂ©e (commence par `ssh-ed25519 ...`). --- ## 🌍 3. CrĂ©er un compte sur localhost.run 1. Ouvrez [https://admin.localhost.run](https://admin.localhost.run) 2. Connectez-vous (Google, GitHub ou email) 3. Collez votre **clĂ© publique SSH** dans la zone prĂ©vue 4. Validez > 🔐 Votre clĂ© publique identifie votre machine. > Ne partagez **jamais** la clĂ© privĂ©e (`id_ed25519`). --- ## 🚀 4. CrĂ©er le tunnel sĂ©curisĂ© Dans un **nouveau terminal** (laissez votre serveur HTTP ouvert) : ```bash ssh -o ExitOnForwardFailure=yes -R 80:localhost:8501 localhost.run ``` Une URL s’affiche, par exemple : ``` https://9a12bc34d5ef6.lhr.life ``` 👉 Ce lien permet d’accĂ©der Ă  votre site depuis Internet. --- ## ⚙ 5. Comprendre la commande | ÉlĂ©ment | Signification | |----------|----------------| | `ssh` | dĂ©marre une connexion sĂ©curisĂ©e | | `-R` | redirection de port distante | | `80` | port distant (HTTP public) | | `localhost:8501` | adresse et port du serveur local | | `localhost.run` | service de tunnel public | > 💡 Avec le **compte gratuit**, l’adresse gĂ©nĂ©rĂ©e est alĂ©atoire et temporaire (ex. `https://abc123.lhr.life`). > La rĂ©servation d’un sous-domaine personnalisĂ© (`mon-site.lhr.rocks`) nĂ©cessite un **abonnement payant**. --- ## 🔁 6. Maintenir la connexion Tant que le terminal reste ouvert, le site est accessible. Pour un tunnel plus stable : ```bash autossh -M 0 -o "ServerAliveInterval=60" -R 80:localhost:8501 localhost.run ``` --- ## 🔒 7. SĂ©curitĂ© - Ne partagez **jamais** votre clĂ© privĂ©e. - Servez uniquement des fichiers publics. - Le tunnel est **chiffrĂ©** (SSH + HTTPS). - Fermez le terminal pour couper l’accĂšs public. --- ## 🧠 8. Exemples pratiques | Application | Commande locale | Tunnel | |--------------|-----------------|---------| | Site statique | `python3 -m http.server 8000` | `ssh -R 80:localhost:8000 localhost.run` | | Streamlit | `streamlit run app.py --server.port 8501` | `ssh -R 80:localhost:8501 localhost.run` | | Flask | `flask run -p 5000` | `ssh -R 80:localhost:5000 localhost.run` | --- ## ✅ RĂ©capitulatif | Étape | Commande | RĂ©sultat | |:--|:--|:--| | 1ïžâƒŁ CrĂ©er la clĂ© SSH | `ssh-keygen -t ed25519 -C "localhost.run"` | Authentification sĂ©curisĂ©e | | 2ïžâƒŁ DĂ©marrer le serveur | `python3 -m http.server 8501` | Serveur local actif | | 3ïžâƒŁ CrĂ©er le tunnel | `ssh -R 80:localhost:8501 localhost.run` | URL publique gĂ©nĂ©rĂ©e | | 4ïžâƒŁ Tester | Ouvrir le lien HTTPS fourni | AccĂšs depuis Internet | --- ## đŸ§Ÿ RĂ©fĂ©rences - Site officiel : [https://localhost.run](https://localhost.run) - Documentation : [https://localhost.run/docs](https://localhost.run/docs) - Gestion de compte : [https://admin.localhost.run](https://admin.localhost.run) --- ✍ *Fiche rĂ©alisĂ©e pour les Ă©lĂšves de BTS CIEL / NSI — Prof. F. MĂ©chain*