issue100:site_web_from_scratch
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue100:site_web_from_scratch [2015/08/30 15:21] – créée auntiee | issue100:site_web_from_scratch [2015/09/15 11:14] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Titre : Website with Infrastructure from Scratch | + | **Titre : Website with Infrastructure from Scratch |
The goal of this series, mainly targeted to beginners, is to guide you step-by-step in building a website, with the entire Linux infrastructure – from scratch. Basically, deploy a Linux distribution (Ubuntu) in the cloud, secure the OS, install the Apache web server and secure Apache. | The goal of this series, mainly targeted to beginners, is to guide you step-by-step in building a website, with the entire Linux infrastructure – from scratch. Basically, deploy a Linux distribution (Ubuntu) in the cloud, secure the OS, install the Apache web server and secure Apache. | ||
Ligne 7: | Ligne 7: | ||
I strongly believe there is no “One size fits all solution” – building from scratch will totally fulfill your needs, while a ready-to-use solution probably won't. On the other hand, a “ready-to-use solution”, | I strongly believe there is no “One size fits all solution” – building from scratch will totally fulfill your needs, while a ready-to-use solution probably won't. On the other hand, a “ready-to-use solution”, | ||
- | But my main reason for building from scratch is learning! When I built my wife's website from scratch, I learned a lot – mainly around security, OS setup and Apache – in other words, I gathered invaluable Linux experience. | + | But my main reason for building from scratch is learning! When I built my wife's website from scratch, I learned a lot – mainly around security, OS setup and Apache – in other words, I gathered invaluable Linux experience.** |
- | Enough talking – let's start! | + | Titre : Site Web avec infrastructure à partir de 0 |
+ | |||
+ | Le but de cette série, surtout destinée aux débutants, est de vous guider pas à pas dans la construction d'un site Web, avec toute l' | ||
+ | |||
+ | Vous pourriez vous demander pourquoi nous ferions tout ça alors qu'il est si facile et possible de prendre une solution hôte peu chère et complète comme Wordpress ? | ||
+ | |||
+ | Je crois fortement qu'il n'y a pas de « solution universelle », la construction à partir de zéro répondra entièrement à vos besoins, alors que la solution toute faite ne le fera probablement pas. À l' | ||
+ | |||
+ | Mais la raison principale d'une construction intégrale est d' | ||
+ | |||
+ | **Enough talking – let's start! | ||
Here, at a high level, is what we will achieve: | Here, at a high level, is what we will achieve: | ||
Ligne 25: | Ligne 35: | ||
Let's spin a new VM, and we'll choose Ubuntu 14.04: | Let's spin a new VM, and we'll choose Ubuntu 14.04: | ||
• Go to https:// | • Go to https:// | ||
- | • Choose “Create a new droplet” (“droplet” is just a VM). | + | • Choose “Create a new droplet” (“droplet” is just a VM).** |
+ | |||
+ | Assez parlé - commençons ! | ||
+ | |||
+ | Voici, au niveau global, ce que nous allons réaliser : | ||
+ | • Premièrement, | ||
+ | • Puis, nous allons sécuriser cette VM Linux, | ||
+ | • Ensuite, nous nous occupérons de l' | ||
+ | • Enfin, je vous montrerai comment enregistrer un domaine et relier ce domaine à Apache, hébergé dans la VM Linux. | ||
+ | |||
+ | Déployer Linux dans le Nuage | ||
+ | |||
+ | Il y a beaucoup de solutions disponibles pour déployer une VM dans le Nuage. Juste un exemple : Amazon Web Services (service Web d' | ||
+ | |||
+ | Dans mon document, je vais utiliser Digital Ocean. Je n' | ||
+ | |||
+ | Faisons tourner une nouvelle VM et nous choisirons Ubuntu 14.04 : | ||
+ | • allez sur https:// | ||
+ | • choisissez « Create a new droplet » (créer une nouvelle droplet - droplet égale | ||
- | Key in a VM name and choose the VM size. The smallest size runs really well. Yes, it doesn' | + | **Key in a VM name and choose the VM size. The smallest size runs really well. Yes, it doesn' |
Then choose the Linux flavor – we'll go for Ubuntu 14.04 64-bit - and finally use the “Create Droplet” button at the bottom of the page. | Then choose the Linux flavor – we'll go for Ubuntu 14.04 64-bit - and finally use the “Create Droplet” button at the bottom of the page. | ||
Ligne 44: | Ligne 72: | ||
• Second, command-line is faster. | • Second, command-line is faster. | ||
- | As convention, all commands starting with # should run as root while all starting with $ will start as regular user. | + | As convention, all commands starting with # should run as root while all starting with $ will start as regular user.** |
- | Basic Security | + | Saisissez un nom de VM et choisissez la taille de la VM. Le plus petit format fonctionne vraiment bien. Oui, il n'y a pas beaucoup de mémoire, mais souvenez-vous que nous allons faire tourner Linux ; aussi, c'est largement suffisant pour une paire de sites. |
+ | |||
+ | Ensuite, choisissez la saveur de Linux - pour nous, Ubuntu 14.04 64-bits - et, enfin, utilisez le bouton « Create Droplet » (Créer la droplet) en bas de la page. | ||
+ | |||
+ | Quelques secondes plus tard, la VM sera créée et un mot de passe administrateur (root) avec l' | ||
+ | |||
+ | IMPORTANT | ||
+ | |||
+ | Digital Ocean facture à l' | ||
+ | |||
+ | Si vous voulez arrêter tout coût supplémentaire, | ||
+ | |||
+ | Veuillez noter que nous allons continuer la préparation du serveur en utilisant uniquement la ligne de commande. | ||
+ | • Premièrement, | ||
+ | • Deuxièmement, | ||
+ | |||
+ | Par convention, toutes les commandes commençant par # devront être lancées par root alors que celles avec $ le seront par un utilisateur normal. | ||
+ | |||
+ | **Basic Security | ||
Now that the VM is up & running, it is available via SSH from anywhere – no security is provided by default except the root password – we need to address this rapidly. | Now that the VM is up & running, it is available via SSH from anywhere – no security is provided by default except the root password – we need to address this rapidly. | ||
Ligne 68: | Ligne 114: | ||
| | ||
- | The -m flag for useradd is to create the home folder, and -s means we want bash as shell. | + | The -m flag for useradd is to create the home folder, and -s means we want bash as shell.** |
- | Now we must ensure that we can log in with the new user created. Open a new terminal session and let's try it out: | + | Sécurité minimum |
+ | |||
+ | Maintenant que la VM est configurée et fonctionne, elle est accessible depuis n' | ||
+ | |||
+ | Laissez-moi faire un parallèle avec l' | ||
+ | |||
+ | Etape 0 - Créer un utilisateur | ||
+ | |||
+ | La première fois que vous vous connectez à votre VM Linux toute neuve, créez un nouvel utilisateur nommé « tux ». Ensuite, nous attribuons le mot de passe « linux » à ce nouvel utilisateur (bien entendu, il faut choisir un meilleur mot de passe). | ||
+ | |||
+ | Connectez-vous à la VM - nous nous connectons comme root cette première fois. Ouvrez un terminal : | ||
+ | |||
+ | ssh root@104.236.124.121 | ||
+ | |||
+ | Puis ajoutez un nouvel utilisateur : | ||
+ | |||
+ | useradd -m -s /bin/bash tux | ||
+ | |||
+ | passwd tux | ||
+ | |||
+ | Attendez l' | ||
+ | |||
+ | Le commutateur -m pour useradd crée un répertoire home et le -s indique que nous voulons bash comme shell. | ||
+ | |||
+ | **Now we must ensure that we can log in with the new user created. Open a new terminal session and let's try it out: | ||
ssh tux@104.236.124.121 | ssh tux@104.236.124.121 | ||
Ligne 98: | Ligne 168: | ||
Try it out – ssh root@104.236.124.121 - error message should be “Permission Denied”. | Try it out – ssh root@104.236.124.121 - error message should be “Permission Denied”. | ||
- | Level 1 of security completed! | + | Level 1 of security completed!** |
- | 2 – RSA key | + | Maintenant, nous devons vérifier que nous pouvons nous connecter avec le nouvel utilisateur. Ouvrez un nouveau terminal et essayez : |
+ | |||
+ | ssh tux@104.236.124.121 | ||
+ | |||
+ | Saisissez votre mot de passe et vous devriez être connecté. Bien vérifier que cette étape fonctionne avant de poursuivre. | ||
+ | |||
+ | 1 - « pas d' | ||
+ | |||
+ | N' | ||
+ | |||
+ | vi / | ||
+ | |||
+ | Et ajoutez ces deux lignes au début du fichier : | ||
+ | |||
+ | AllowUsers | ||
+ | |||
+ | DenyUsers | ||
+ | |||
+ | J' | ||
+ | |||
+ | Pour que ça prenne effet, nous devons redémarrer le démon ssh : | ||
+ | |||
+ | service ssh restart | ||
+ | |||
+ | Maintenant, l' | ||
+ | |||
+ | Essayez ssh root@104.236.124.121 ; le message d' | ||
+ | |||
+ | Le niveau 1 sécuritaire est terminé ! | ||
+ | |||
+ | **2 – RSA key | ||
An RSA key is a pretty secure way to login to an SSH server. Generating an RSA key will create 2 components – a private key and a public key. In plain English, a private key is a small file. A public key is also a small file. In order to access the system with the RSA keys (so we'll do what is called “RSA authentication”), | An RSA key is a pretty secure way to login to an SSH server. Generating an RSA key will create 2 components – a private key and a public key. In plain English, a private key is a small file. A public key is also a small file. In order to access the system with the RSA keys (so we'll do what is called “RSA authentication”), | ||
Ligne 118: | Ligne 218: | ||
You can accept the default selection or change the name. Let's change the name on purpose to id_rsa_iceberg (but keep the same folder). | You can accept the default selection or change the name. Let's change the name on purpose to id_rsa_iceberg (but keep the same folder). | ||
- | You can choose a password or leave it blank. | + | You can choose a password or leave it blank.** |
+ | |||
+ | 2 - Clé RSA | ||
+ | |||
+ | Une clé RSA est une façon bien sécurisée de se connecter à un serveur SSH. La génération de la clé RSA créera deux composantes : une clé privée et une clé publique. En français ordinaire, une clé privée, c'est un petit fichier. Une clé publique est un autre petit fichier. Pour accéder au système avec les clés RSA (ce que nous appellerons « l’authentification RSA »), SSH vérifiera que les deux fichiers se correspondent et, si c'est le cas, vous pourrez vous connecter. | ||
+ | |||
+ | Laissez-moi faire un nouveau parallèle avec un billet de banque : imaginez que nous coupions aléatoirement le billet en deux. Seules ces deux parties vont ensemble. C'est pareil pour les clés RSA. | ||
+ | |||
+ | La clé publique est stockée dans le serveur SSH (iceberg dans notre exemple), alors que tux conservera la clé privée. Quand tux essaie de se connecter à iceberg, tux doit montrer la clé privée correspondant à la clé publique. Et la taille de la clé sera si grande que la probabilité que quelqu' | ||
+ | |||
+ | En résumé, sans la bonne clé privée, il n'y a pas d' | ||
+ | |||
+ | IMPORTANT - Nous allons générer des clés pour tux et pas pour root (souvenez-vous : « pas d' | ||
+ | |||
+ | Pour la génération de la paire de clés privée/ | ||
+ | |||
+ | ssh-keygen -t rsa -b 10240 | ||
+ | |||
+ | Vous pouvez accepter la sélection par défaut ou changer le nom. Changeons-le pour id_rsa_iceberg (mais gardons le même répertoire). | ||
+ | |||
+ | Vous pouvez choisir un mot de passe, ou le laisser en blanc. | ||
- | Some explanation here – the password is used to protect the private key itself. In the unlikely scenario somebody steals the private key, that attacker could then be able to connect to your server. But if he doesn' | + | **Some explanation here – the password is used to protect the private key itself. In the unlikely scenario somebody steals the private key, that attacker could then be able to connect to your server. But if he doesn' |
I know that a few of you will not agree, but think about it – what is really the chance somebody will steal your key? Virtually none, right? This is my point – it may therefore not need a password for our purposes. | I know that a few of you will not agree, but think about it – what is really the chance somebody will steal your key? Virtually none, right? This is my point – it may therefore not need a password for our purposes. | ||
Ligne 139: | Ligne 259: | ||
Now, before we continue, let's talk a little bit about the private key password security. | Now, before we continue, let's talk a little bit about the private key password security. | ||
- | As a general rule, modern encryption systems are pretty robust. However corners are sometimes cut and the entire system will be easier to defeat. | + | As a general rule, modern encryption systems are pretty robust. However corners are sometimes cut and the entire system will be easier to defeat.** |
- | If you are familiar with the Enigma encryption system used by Germans during World War 2 – that was a really robust encryption system. For whatever (good?) reason, the creators decided that a character could not be encrypted as itself. Well, this feature inserted a weakness because it reduced the number of combinations the cypher could produce. It's obviously not the only reason why the code was broken – my point is that a small decision (by sloppiness or not) lowered the encryption strength of a well thought system. | + | Un peu d' |
+ | |||
+ | Je sais que certains d' | ||
+ | |||
+ | Mais faisons-le selon les règles de l'art : choisissons le mot de passe 12345 (à nouveau, si vous prévoyez de tout faire comme il faut, ne choisissez pas un mauvais mot de passe - ceci n'est qu'un tutoriel). | ||
+ | |||
+ | Nous devrions avoir 2 fichiers dans le répertoire / | ||
+ | • id_rsa_iceberg pour la clé privée. | ||
+ | • id_rsa_iceberg.pub pour la clé publique. | ||
+ | |||
+ | Maintenant, faites une copie de la clé publique comme suit : le nom est important car ssh cherchera spécifiquement les clés autorisées (vous pouvez changer ce comportement par défaut en ajustant / | ||
+ | |||
+ | cp id_rsa_iceberg.pub authorized_keys | ||
+ | |||
+ | Sécurité spéciale du mot de passe de la clé privée. | ||
+ | |||
+ | Avant de poursuivre, parlons un peu de la sécurité du mot de passe de la clé privée. | ||
+ | |||
+ | En règle générale, les systèmes modernes de chiffrage sont plutôt robustes. Cependant, parfois des libertés sont prises et on pourrait plus facilement faire tomber les défenses de tout le système. | ||
+ | |||
+ | **If you are familiar with the Enigma encryption system used by Germans during World War 2 – that was a really robust encryption system. For whatever (good?) reason, the creators decided that a character could not be encrypted as itself. Well, this feature inserted a weakness because it reduced the number of combinations the cypher could produce. It's obviously not the only reason why the code was broken – my point is that a small decision (by sloppiness or not) lowered the encryption strength of a well thought system. | ||
Same with SSH - private keys can be encrypted with a password (remember – 12345 in our example), however the encryption used is not as strong as you may think. Thanks to Open Source, somebody noticed this weakness and it is easily possible to seriously beef up the private key encryption. If you have any interest in doing this – just for fun actually - I recommend reading this excellent blog: http:// | Same with SSH - private keys can be encrypted with a password (remember – 12345 in our example), however the encryption used is not as strong as you may think. Thanks to Open Source, somebody noticed this weakness and it is easily possible to seriously beef up the private key encryption. If you have any interest in doing this – just for fun actually - I recommend reading this excellent blog: http:// | ||
Ligne 166: | Ligne 306: | ||
openssl pkcs8 -topk8 -v2 des3 -in / | openssl pkcs8 -topk8 -v2 des3 -in / | ||
- | [NOTE: the above is all one line] | + | [NOTE: the above is all one line]** |
- | Now the private key will be protected with the password included in file2.txt, and properly encrypted! | + | Si vous connaissez le système de chiffrage Enigma utilisé par les Allemands pendant la Seconde Guerre mondiale, c' |
+ | |||
+ | C'est pareil pour SSH : les clés privées peuvent être protégées par mot de passe (rappel : 12345 dans notre exemple), cependant, le chiffrage n'est pas aussi fort que vous pourriez le penser. Grâce à l'Open Source, quelqu' | ||
+ | |||
+ | En résumé : | ||
+ | • Faites une copie de votre clé privée, | ||
+ | • créez 2 fichiers - file1.txt avec le mot de passe d' | ||
+ | • modifiez le chiffrage. | ||
+ | |||
+ | Comme tux (su - tux, si vous êtes root) : | ||
+ | |||
+ | cd / | ||
+ | |||
+ | mv id_rsa_iceberg id_rsa_iceberg.bak | ||
+ | |||
+ | cat >> file1.txt | ||
+ | |||
+ | Tapez 12345 puis appuyez sur Ctrl+d | ||
+ | |||
+ | cat >> file2.txt | ||
+ | |||
+ | Tapez 12345 puis appuyez sur Ctrl+d | ||
+ | |||
+ | openssl pkcs8 -topk8 -v2 des3 -in / | ||
+ | |||
+ | [NOTE : Ci-dessus, il ne s'agit que d'une seule ligne !) | ||
+ | |||
+ | **Now the private key will be protected with the password included in file2.txt, and properly encrypted! | ||
In order to connect to iceberg, we need to copy the private key id_rsa_iceberg to our computer. There are a few ways of doing this (scp for example – or secure copy), however what about a simple copy / paste? | In order to connect to iceberg, we need to copy the private key id_rsa_iceberg to our computer. There are a few ways of doing this (scp for example – or secure copy), however what about a simple copy / paste? | ||
Ligne 194: | Ligne 361: | ||
chmod 400 id_rsa_iceberg | chmod 400 id_rsa_iceberg | ||
- | (this is read-only and expected by ssh). | + | (this is read-only and expected by ssh).** |
- | We should now try to connect to iceberg using our ssh key. | + | À partir de maintenant, la clé privée sera protégée par le mot de passe inclus dans file2.txt, et chiffrée comme il faut ! |
+ | |||
+ | Pour pouvoir se connecter à iceberg, nous avons besoin d'une copie de la clé privée id_rsa_iceberg sur notre ordinateur. On peut le faire de différentes façons (scp par exemple, ou une copie sécurisée) ; cependant, pourquoi pas un simple copier/ | ||
+ | |||
+ | Faisons-le : | ||
+ | |||
+ | cat id_rsa_iceberg | ||
+ | |||
+ | • Sélectionnez tout ce qui commence à -----BEGIN RSA PRIVATE KEY----- jusqu' | ||
+ | |||
+ | Sur votre bureau : | ||
+ | |||
+ | cd | ||
+ | |||
+ | (pour revenir à votre dossier home) | ||
+ | |||
+ | cd .ssh | ||
+ | |||
+ | vi id_rsa_iceberg | ||
+ | |||
+ | Là, collez, enregistrez et fermez. | ||
+ | |||
+ | • Changez les permissions du fichier : | ||
+ | |||
+ | chmod 400 id_rsa_iceberg | ||
+ | |||
+ | (il est en lecture seule et ssh s'y attend). | ||
+ | |||
+ | **We should now try to connect to iceberg using our ssh key. | ||
From your desktop: | From your desktop: | ||
Ligne 220: | Ligne 415: | ||
You should get Permission denied (publickey). It means that password authentication is not allowed. | You should get Permission denied (publickey). It means that password authentication is not allowed. | ||
- | Now we are fully secure! | + | Now we are fully secure!** |
- | To summarize: | + | Nous devons maintenant essayer de nous connecter à iceberg en utilisant notre clé ssh. |
+ | |||
+ | Sur le bureau : | ||
+ | |||
+ | ssh -i ~/ | ||
+ | |||
+ | Vous devriez avoir une demande de mot de passe pour la clé privée (c'est 12345) et vous devriez ensuite être connecté à iceberg. | ||
+ | |||
+ | Réglage final de la sécurité : nous allons maintenant interdire toutes les connexions par mot de passe à iceberg. | ||
+ | |||
+ | Revenez au fichier / | ||
+ | |||
+ | vi / | ||
+ | |||
+ | • Changez PermitRootLogin yes en PermitRootLogin no | ||
+ | • Changez # | ||
+ | • Changez LoginGraceTime de 120 à 20 (ceci autorise 20 connexions simultanées – c'est largement assez). | ||
+ | |||
+ | Enregistrez, | ||
+ | |||
+ | Essayez ssh tux@104.236.124.121 | ||
+ | |||
+ | Vous devriez recevoir Permission denied (publickey) - permission refusée (clé publique). Cela signifie que l' | ||
+ | |||
+ | Maintenant, nous sommes complètement sécurisés. | ||
+ | |||
+ | **To summarize: | ||
• root cannot connect. | • root cannot connect. | ||
• only RSA keys are allowed for authentication. | • only RSA keys are allowed for authentication. | ||
Ligne 236: | Ligne 457: | ||
With the snapshot, the VM can be re-created to the same state as when the snapshot was taken. | With the snapshot, the VM can be re-created to the same state as when the snapshot was taken. | ||
- | If you are using any other Cloud provider, make sure to check their documentation so you do not pay while the VM is not up & running. | + | If you are using any other Cloud provider, make sure to check their documentation so you do not pay while the VM is not up & running.** |
+ | |||
+ | En résumé : | ||
+ | • root ne peut pas se connecter. | ||
+ | • seules les clés RSA sont autorisées pour l' | ||
+ | |||
+ | La prochaine fois, nous sécuriserons le serveur en utilisant les règles du pare-feu (le serveur est grand ouvert, mais quand même assez sécurisé pour aujourd' | ||
+ | |||
+ | Nous installerons apache (le serveur Web) et nous le sécuriserons. | ||
+ | |||
+ | Dernière note importante | ||
+ | |||
+ | Si vous utilisez Digital Ocean, le simple arrêt de la VM n' | ||
+ | |||
+ | Avec l' | ||
+ | |||
+ | Si vous utilisez un autre fournisseur de Nuage, vérifiez leur documentation pour vous assurer de ne pas payer quand la VM n'est pas en cours d' |
issue100/site_web_from_scratch.1440940888.txt.gz · Dernière modification : 2015/08/30 15:21 de auntiee