Monday

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

Slashdot

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.

Before we proceed please read the disclaimer below.

DISCLAIMER: I have made every attempt to present accurate information, but I cannot guarantee that inaccuracies will not occur. I (the author) will not be held responsible for any claim, loss, damage or inconvenience caused as a result of any information found on this website (blog).


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 ave 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.

Labels: , , , , , , , ,

4 Comments:

At February 18, 2007 at 1:46:00 AM PST , Blogger Mathew Newton said...

Doesn't the dafault Apache install on Ubuntu already have an init script (quite a detailed one at that) and start at boot automatically?

Mathew

 
At February 18, 2007 at 9:34:00 AM PST , Blogger Shahryar Ghazi said...

I wrote this tutorial for ppl who've compiled and installed Apache on Ubuntu. It is not intended for ppl who use Ubuntu's default package manager to install Apache.

First line of my tutorial,
"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. "

 
At July 24, 2008 at 6:58:00 PM PDT , Anonymous Jeff said...

just added this to my new deployment - well written and works like a champ. I used emacs however ;-) (any text editor will do - don't have to use nano).

 
At August 14, 2008 at 2:45:00 PM PDT , Anonymous Erik said...

Hi

Just wanted to say thanks, because your how-to helped me a lot. I have installed xampp and by default it does not make the autostart of apache at booting up.
With your info it worked from the first time!

Erik

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home