How do I get Apache to startup at bootime on Linux?

Solution 1:

You want to add its init script to the appropriate run level. The init script is typically /etc/init.d/apache2 where you could manually run /etc/init.d/apache2 start to start it.

On Gentoo you would write:

rc-update add apache2 default

On Ubuntu/Debian this works:

sudo update-rc.d apache2 defaults

On Red Hat Linux/Fedora/CentOS a little googling shows this:

chkconfig --add httpd

It varies a little bit from distribution to distribution , but the idea is usually the same. Basically, all these commands make a symbolic link from /etc/init.d/ to the appropriate run-level folder in /etc/.

Solution 2:

Here is what finally worked for me. This assumes you are the root user.

  1. vi /etc/init.d/apache2 (edit it as shown below)
  2. chmod 755 /etc/init.d/apache2
  3. chkconfig --add apache2
  4. chkconfig --list apache2 (to verify that it worked)

Contents of /etc/init.d/apache2:

#!/bin/bash
#
# apache2        Startup script for the Apache HTTP Server
#
# chkconfig: 3 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#              HTML files and CGI.

/usr/local/apache2/bin/apachectl $@

You can get the runlevel by running /sbin/runlevel, which in my case was 3. And of course you need to call your version of apachectl, which in my case was /usr/local/apache2/bin/apachectl

Thanks to the following:

  • The answer from Evan Teran +1 for the help
  • The answer from Russell Heilling + 1 for the help
  • man chkconfig
  • This link from sysdigg which contained the information on the runlevel.

Solution 3:

Check if you have the httpd init script in the /etc/rc.d directory. If yes, then you can just run the following command which enables the httpd service to start at boot time.

chkconfig --level 345 httpd on

If you don't have the init script, then just append the /etc/rc.local file with apachectl -k start (the command to start Apache).


Solution 4:

As you have installed by source there will not be an init script installed in the /etc/init.d/ directory. The apachectl binary is designed to be compatible with standard init script options so you may well be able to simply symlink to it rather than creating a wrapper script (e.g ln -s /usr/local/sbin/apachectl /etc/init.d/apache)

You can then follow the procedures outlined in the other posts for adding links to invoke the init script at the correct runlevels.

Citation: Apache Documentation http://httpd.apache.org/docs/2.2/invoking.html

Starting at Boot-Time

If you want your server to continue running after a system reboot, you should add a call to apachectl to your system startup files (typically rc.local or a file in an rc.N directory). This will start Apache as root. Before doing this ensure that your server is properly configured for security and access restrictions.

The apachectl script is designed to act like a standard SysV init script; it can take the arguments start, restart, and stop and translate them into the appropriate signals to httpd. So you can often simply link apachectl into the appropriate init directory. But be sure to check the exact requirements of your system.