Redémarrer automatiquement Apache/MySQL avec monit

Sur un serveur web LAMP il peut arriver que la processus Apapche 2 ou MariaDB/MySQL se termine de façon anormale ou soit terminé par OOM Killer.

Pour surveiller ces processus il existe monit qui est très simple d’utilisation. Voici comment l’installer sur un Ubuntu Server 22.04 :

sudo apt install monit

Aller dans le répertoire de configuration de monit :

cd /etc/monit/conf.d/

Créer un fichier pour Apache, apache.conf contenant :

check process apache with pidfile /run/apache2/apache2.pid
    start program = "/usr/sbin/service apache2 start"
    stop program = "/usr/sbin/service apache2 stop"
    if failed host 127.0.0.1 port 80 protocol http then restart
    if 5 restarts within 5 cycles then timeout

Créer un second fichier pour MariaDB/MySQL, mysql.conf :

check process mysqld with pidfile /run/mysqld/mysqld.pid
    start program = "/usr/sbin/service mysql start"
    stop program = "/usr/sbin/service mysql stop"
    if failed host 127.0.0.1 port 3306 protocol mysql then restart
    if 5 restarts within 5 cycles then timeout

Puis redémarrer monit et vérifier que celui-ci fonctionne bien :

sudo service monit restart
sudo service monit status
● monit.service - LSB: service and resource monitoring daemon
     Loaded: loaded (/etc/init.d/monit; generated)
     Active: active (running) since Wed 2023-06-07 08:14:53 CEST; 4s ago
       Docs: man:systemd-sysv-generator(8)
    Process: 58545 ExecStart=/etc/init.d/monit start (code=exited, status=0/SUCCESS)
      Tasks: 1 (limit: 9518)
     Memory: 1.7M
        CPU: 31ms
     CGroup: /system.slice/monit.service
             └─58551 /usr/bin/monit -c /etc/monit/monitrc

Jun 07 08:14:53 ip-172-31-26-128 systemd[1]: Starting LSB: service and resource monitoring daemon...
Jun 07 08:14:53 ip-172-31-26-128 monit[58545]:  * Starting daemon monitor monit
Jun 07 08:14:53 ip-172-31-26-128 monit[58545]:    ...done.
Jun 07 08:14:53 ip-172-31-26-128 systemd[1]: Started LSB: service and resource monitoring daemon.

Et voilà, c’est en place. monit vérifie toutes les 2 minutes que les processus fonctionnent et les redémarre si besoin.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *