Simple example of how to create a python script to redirect on a maintenance page
Replace site_name by the website domain name
Create a simple html page
Go under the directory:
cd /var/www/
Create the folder site_name_maintenance_page
mkdir site_name_maintenance_page
and then:
mkdir html
the path should be: /var/www/site_name_maintenance_page/html/
And create the file index.html
nano index.html
with the following lines
<html><head><title>site_name</title></head><body><h1>The site is under maintenance ! </h1></body></html>
Note: it is a minimalist example there are a lots of maintenance page free templates available on the web.
Edit nginx sites-enabled file
If the django website is running with Nginx the file
vi /etc/nginx/sites-enabled/site_name
should look like
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;}}
to redirect the site to the maintenance page. Remove all the lines of the above file
:1,$d
and replace it by
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;}}
Then restart Nginx
sudo service nginx restart
Create a python script
Now create a simple python script (called for example 'site_name_status.py') to make it automatic:
python site_name_status.py on
to display the django-based website
python site_name_status.py off
to display the maintenance page
import sysimport osarg_list = sys.argv#print(arg_list)file = '/etc/nginx/sites-available/site_name'#----------------------------------------------------------------------------------------#if arg_list[1]=='off':open(file, 'w').close() # clean the filef = 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 filef = 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#----------------------------------------------------------------------------------------#
Restart Nginx
sudo service nginx restart
References
| Links | 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 |
