Starting Apache Web Server at every Reboot (for Ubuntu / Debian Linux)

If you compiled and installed Apache on a Debian/Ubuntu Linux machine and want the Apache service run every time you reboot your machine this short tutorial is for you. I have tried to make it very simple so I am pretty sure you'll be able to follow all steps even if you don't know Unix/Linux shell scripting.

In order to run Apache at boot time you have to write a very simple start up script. Follow the steps below. Please note that you have to run the commands as root for both Ubuntu and Debian.

  • In Ubuntu, do the following (you'll probably be prompted for password).

    sudo nano /etc/init.d/apache2

    In Debian do the following (as root).

    nano /etc/init.d/apache2

    The above command will open up a text editor with an empty page (assuming that /etc/init.d/apache2 did not previously exist or was empty).
  • Now, enter the code below in your text editor. Lines starting with a # symbol are comments (except the first line).

    #!/bin/sh
    case "$1" in
    start)
    echo "Starting Apache ..."
    # Change the location to your specific location
    /usr/local/apache2/bin/apachectl start
    ;;
    stop)
    echo "Stopping Apache ..."
    # Change the location to your specific location
    /usr/local/apache2/bin/apachectl stop
    ;;
    graceful)
    echo "Restarting Apache gracefully..."
    # Change the location to your specific location
    /usr/local/apache2/bin/apachectl graceful
    ;;
    restart)
    echo "Restarting Apache ..."
    # Change the location to your specific location
    /usr/local/apache2/bin/apachectl restart
    ;;
    *)
    echo "Usage: '$0' {start|stop|restart|graceful}" > &2
    exit 64
    ;;
    esac
    exit 0

  • Now, press "Ctrl–o" to save the file and "Ctrl–x" to exit from the editor.
  • You have to change the file permissions by executing the command below:

    Ubuntu: sudo chmod u+x /etc/init.d/apache2

    Debian: chmod u+x /etc/init.d/apache2

  • To start Apache, run command below:

    Ubuntu: sudo /etc/init.d/apache2 start

    Debian: /etc/init.d/apache2 start

  • To stop Apache, run command below:

    Ubuntu: sudo /etc/init.d/apache2 stop

    Debian: /etc/init.d/apache2 stop

  • To restart Apache, run command below:

    Ubuntu: sudo /etc/init.d/apache2 restart

    Debian: /etc/init.d/apache2 restart

  • To restart Apache gracefully, run command below:

    Ubuntu: sudo /etc/init.d/apache2 graceful

    Debian: /etc/init.d/apache2 graceful

  • In order to add the script to the default runlevel you do the following.

    Ubuntu: sudo update–rc.d apache2 defaults

    Debian: update–rc.d apache2 defaults

  • In case you want to remove it from the run level you do the following.

    Ubuntu: sudo update–rc.d –f apache2 remove

    Debian: update–rc.d –f apache2 remove

That's it you're done. Now, Apache will start automatically at boot time.

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.

Comments (write a comment):

Excellent! Thank you for a lucid and completely accurate explanation of your method to start applications at boot. I have adapted it to start both my Apache 2.4 and MySQL servers, and it works faultlessly, unlike most of the other methods I've seen out there. It's much appreciated!

Regards,
JMW Posted by: J M on Jun 12, 2013

Thank you so much for your article, anyway when i to copy the code, and run it, it print out some error, this line :
echo "Usage: '$0' {start|stop|restart|graceful}" > &2

but it worked when i remove &2.

:) Posted by: Mochamad G on Jul 07, 2013

right off the bat, what if the apache2 script is already there in /etc/init.d/? I have installed apache2 using ubuntu in an EC2 instance. Not sure what the existing script does. Posted by: Ken S on Aug 21, 2014

@ken: you may need to modify that script to suit your needs.

Plz make sure you make a copy of the script first. Posted by: shahryar on Sep 02, 2014

for that "& unexpected on line 24" error, when copying from this webpage spaces appear between the quote, arrow, and ampersand. If you delete those spaces the script works perfectly. Posted by: beowulfey on Dec 19, 2014

leave a comment on tutorial leave a comment