Apache may sometime crash or stop for some other reasons. I had this issue with one of the server where Let’s Encrypt post hook does start Apache for whatever reasons. So i thought it would be better to have some bash script which runs every minute and check Apache status and start it if its dead/inactive.
Login to your server and create apache.sh file in /opt/httpd (you can choose other dir for storing the script). Create httpd dir 1st(mkdir httpd).
This script is tested on Ubuntu 16.04 server.
#!/bin/sh STARTAPACHE="systemctl start apache2" LOGFILE="/opt/httpd/autostart-apache2.log" #check for the word inactive in the result of status if systemctl status apache2 | grep -q inactive then echo "starting apache at $(date)" >> $LOGFILE $STARTAPACHE >> $LOGFILE else echo "apache is running at $(date)" fi
If you want to log the else statement too, add >> $LOGFILE at the end of the echo line. I personally don’t need it as it will spam the logs.
To run this script every minute via cron:
* * * * * sh /opt/httpd/apache2.sh