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

Active 11 avril 2020    /    Viewed 923    /    Comments 0    /    Edit


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

Liens Site
How to erase the file contents of text file in Python? stackoverflow
Reading and Writing to text files in Python stackoverflow
Python - Command Line Arguments stackoverflow


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!