Comment créer une simple page de maintenance pour un site en django (déployé avec Nginx, Gunicorn) sur Ubuntu (digitalocean) ?

Published: 11 avril 2020

Tags: Django; Nginx; Gunicorn; Digitalocean;

DMCA.com Protection Status

Exemple de comment créer une simple page de maintenance pour un site en django (déployé avec Nginx, Gunicorn) sur Ubuntu (digitalocean):

Remplacer site_name par le nom de domaine du site

Créer une simple page

Allez sous le répertoire:

cd /var/www/

Créez le dossier site_name_maintenance_page

mkdir site_name_maintenance_page

puis:

mkdir html

le chemin est alors: /var/www/site_name_maintenance_page/html/

Et créez le fichier index.html

nano index.html

avec les lignes suivantes

<html>
    <head>
        <title>site_name</title>
    </head>
    <body>
        <h1>The site is under maintenance ! </h1>
    </body>
</html>

Remarque: il s'agit d'un exemple minimaliste, il existe de nombreux modèles gratuits de pages de maintenance disponibles sur le Web.

Modifier le fichier nginx sites-enabled

Si le site django fonctionne avec Nginx, le fichier

vi /etc/nginx/sites-enabled/site_name

devrait ressembler a

server {
    listen 80;
    listen [::]:80;
    server_name site_name.com www.site_name.com;
    return 301 https://site_name.com$request_uri;
}

server {
    listen 443 ssl;
    server_name site_name.com www.site_name.com;

    ssl_certificate /root/ssl_certificates/site_name/site_name_com.chained.crt;
    ssl_certificate_key /root/ssl_certificates/site_name/site_name_com.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers '***********************';

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static {
        alias /home/username/webapps/site_name_static;
    }

        location /media  {
           alias /home/username/webapps/site_name_media;
        }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

pour rediriger le site vers la page de maintenance. Supprimez toutes les lignes du fichier ci-dessus

:1,$d

et remplacez-le par

server {
    listen 80;
    listen [::]:80;
    server_name site_name.com www.site_name.com;
    return 301 https://site_name.com$request_uri;
}

server {
    listen 443 ssl;
    server_name site_name.com www.site_name.com;

    ssl_certificate /root/ssl_certificates/site_name/site_name_com.crt;
    ssl_certificate_key /root/ssl_certificates/site_name/site_name.com.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers '*******************';

    root /var/www/site_name_maintenance_page/html;
    index index.html index.htm index.nginx-debian.html;

    location / {
            try_files $uri $uri/ =404;
    }

}

Redémarrez ensuite Nginx

sudo service nginx restart

Créer a python script

Créez maintenant un script python simple (appelé par exemple 'site_name_status.py') pour rendre l'opération quasi automatique:

python site_name_status.py on

pour afficher: django-based website

python site_name_status.py off

pour afficher la page de maintenance

import sys
import os

arg_list = sys.argv
#print(arg_list)

file = '/etc/nginx/sites-available/site_name'

#----------------------------------------------------------------------------------------#

if arg_list[1]=='off':

    open(file, 'w').close() # clean the file

    f = open(file,"w")

    str = '''server {
    listen 80;
    listen [::]:80;
    server_name site_name.com www.site_name.com;
    return 301 https://site_name.com$request_uri;
}

server {
    listen 443 ssl;
    server_name site_name.com www.site_name.com;

    ssl_certificate /root/ssl_certificates/site_name/site_name_com.chained.crt;
    ssl_certificate_key /root/ssl_certificates/site_name/site_name_com.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers '******************';

    root /var/www/site_name_maintenance_page/html;
    index index.html index.htm index.nginx-debian.html;

    location / {
            try_files $uri $uri/ =404;
    }

}
'''

    f.write(str)

    f.close

#----------------------------------------------------------------------------------------#

if arg_list[1]=='on':

    open(file, 'w').close() # clean the file

    f = open(file,"w")

    str = '''server {
    listen 80;
    listen [::]:80;
    server_name site_name.com www.site_name.com;
    return 301 https://site_name.com$request_uri;
}

server {
    listen 443 ssl;
    server_name site_name.com www.site_name.com;

    ssl_certificate /root/ssl_certificates/site_name/site_name_com.chained.crt;
    ssl_certificate_key /root/ssl_certificates/site_name/site_name_com.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers '***********************';

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static {
        alias /home/username/webapps/site_name_static;
    }

        location /media  {
           alias /home/username/webapps/site_name_media;
        }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}   
'''

    f.write(str)

    f.close

#----------------------------------------------------------------------------------------#

Redémarrer Nginx

sudo service nginx restart

Références