Forum

How To Restart Apac...
 
Notifications
Clear all

[Solved] How To Restart Apache Automatically After Crash?

 
(@mrjames)
Active Member

Hi Team,

 

I have VPS server on digital ocean, sometimes apache server crash without any reason. I believe my server space is only 30GB, and it is giving me the problem, can you help me. I do not want to upgrade the server right now.

I am looking for a solution to restart my apache server automatically when it crash.

 

Thank you.

Quote
Topic starter Posted : 13/02/2023 12:15 pm
(@administrator)
Member Admin

To automatically restart Apache after a crash, you can use a process manager like Systemd or supervisord. Here's how to do it with Systemd:

  1. Create a new Systemd service file: sudo nano /etc/systemd/system/apache.service

  2. Add the following lines to the service file:

    [Unit]
    Description=Apache Web Server
    After=network.target
    
    [Service]
    ExecStart=/usr/sbin/apache2ctl start
    ExecReload=/usr/sbin/apache2ctl graceful
    ExecStop=/usr/sbin/apache2ctl stop
    PIDFile=/var/run/apache2/apache2.pid
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
    

  1. Save the file and exit the editor.

  2. Reload the Systemd configuration: sudo systemctl daemon-reload

  3. Start the new service: sudo systemctl start apache

  4. Check the status of the service: sudo systemctl status apache

Now, whenever Apache crashes, Systemd will automatically restart it.

ReplyQuote
Posted : 13/02/2023 12:24 pm
(@mrjames)
Active Member

Thanks for your Quick reply, do I restart the apache server via Cron job?

ReplyQuote
Topic starter Posted : 13/02/2023 12:27 pm
(@administrator)
Member Admin

@mrjames 

 

It's not recommended to use Cron to automatically restart Apache after a crash, as Cron is designed for scheduled tasks and may not be reliable in detecting when Apache has crashed. It's better to use a dedicated process manager like Systemd or supervisord to ensure that Apache is restarted promptly and reliably.

However, if you still want to use Cron for this purpose, you can create a Cron job that checks if Apache is running, and if not, restarts it. Here's how:

  1. Open the Cron configuration file: sudo crontab -e

  2. Add the following line to the file:

    * * * * * ps aux | grep apache2 | grep -v grep || /usr/sbin/service apache2 start
    

        •  

This will run the command "ps aux | grep apache2 | grep -v grep" every minute, which checks if Apache is running. If the command returns no output (i.e., Apache is not running), the second part of the command "/usr/sbin/service apache2 start" will start Apache.

  1. Save the file and exit the editor.

Note that this approach may not be as reliable as using a dedicated process manager, as there may be a delay between the time Apache crashes and the Cron job detects it and restarts it. Additionally, this approach may not work if there are multiple instances of Apache running, or if the Apache process name is different (e.g., "httpd" instead of "apache2").

ReplyQuote
Posted : 13/02/2023 12:33 pm
Mr James reacted
Share:
×