# Mise en place d'un load-balancing & fail-over avec NGINX et Apache

# Mise en place des deux serveurs Web Apache

Sur les deux machines, on installe Apache.

```bash
apt install apache2 -y
```

On crée ensuite un virtual-host sur chaque machine.

```bash
nano /etc/apache2/sites-available/web.home.khroners.fr.conf
```

Son contenu (identique aux deux serveurs) :

```bash
<VirtualHost *:80>
        ServerName web.home.khroners.fr
        ServerAlias www.web.home.khroners.fr
        ServerAdmin alexisbonnet@khroners.fr
        DocumentRoot /var/www/web.home.khroners.fr
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```

On crée ensuite la page web.

```bash
rm /var/www/index.html
mkdir /var/www/web.home.khroners.fr
nano /var/www/web.home.khroners.fr/index.html
```

Le contenu du fichier (ici pour Web2):

```HTML
<!doctype html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <title>Web2</title>
  <meta name="description" content="My Page Description">
</head>
<body>
Bienvenue sur le site web2
</body>
</html>
```

On active ensuite le site et on redémarre Apache.

```bash
a2ensite web.home.khroners.fr
systemctl restart apache2
```

</body></html>

# Mise en place de NGINX

Tout d'abord, on l'installe.

```bash
apt install nginx -y
```

On modifie le fichier de configuration pour y inclure :

```bash
nano /etc/nginx/nginx.conf
```

```Nginx
http {
   upstream backend {
      server 192.168.199.50 weight=5 max_fails=3 fail_timeout=10s;
      server 192.168.199.51 weight=1 max_fails=3 fail_timeout=10s;
   }
  
   server {
      listen 80;
      server_name web.home.khroners.fr;

      location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_pass http://backend$request_uri;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header Host web.home.khroners.fr;
      }
   }

```

On redémarre le service.

```bash
systemctl restart nginx
```

# Tests

[![image-1616493707029.png](https://docs.khroners.fr/uploads/images/gallery/2021-03/scaled-1680-/H8bFhql9PYkDUGOh-image-1616493707029.png)](https://docs.khroners.fr/uploads/images/gallery/2021-03/H8bFhql9PYkDUGOh-image-1616493707029.png)

Si on rafraichit plusieurs fois, on aura Web2.

[![image-1616493724400.png](https://docs.khroners.fr/uploads/images/gallery/2021-03/scaled-1680-/c2peAazxQ7J6W2yt-image-1616493724400.png)](https://docs.khroners.fr/uploads/images/gallery/2021-03/c2peAazxQ7J6W2yt-image-1616493724400.png)

Si l'on coupe sur un des deux serveurs, ici Web1 le service Apache :

```bash
systemctl stop apache2
```

On aura uniquement Web2.

[![image-1616493784993.png](https://docs.khroners.fr/uploads/images/gallery/2021-03/scaled-1680-/wp55Yi6uIU5VVEkc-image-1616493784993.png)](https://docs.khroners.fr/uploads/images/gallery/2021-03/wp55Yi6uIU5VVEkc-image-1616493784993.png)

Et en réactivant et en stoppant le service Apache de l'autre:

On aura uniquement Web1.

[![image-1616493823574.png](https://docs.khroners.fr/uploads/images/gallery/2021-03/scaled-1680-/0mJYqqbjWVWJc5vD-image-1616493823574.png)](https://docs.khroners.fr/uploads/images/gallery/2021-03/0mJYqqbjWVWJc5vD-image-1616493823574.png)