On Red Hat Enterprise Linux (RHEL) and its derivatives like CentOS, the traditional way to manage services at boot is through systemd
or the older System V init system. Since your kernel version (2.6.9) suggests an older RHEL version, we'll cover both methods.
For systems running systemd (RHEL 7+), use these commands:
# Enable Apache to start at boot
sudo systemctl enable httpd
# Verify the setting
sudo systemctl is-enabled httpd
For older RHEL versions using SysV init:
# Add Apache to startup sequence
sudo chkconfig --add httpd
sudo chkconfig httpd on
# Verify the configuration
sudo chkconfig --list httpd
If you installed from source and need to create an init script:
#!/bin/sh
#
# httpd Startup script for Apache
#
# chkconfig: 345 85 15
# description: Apache is a web server
# processname: httpd
case "$1" in
start)
/usr/local/apache2/bin/apachectl start
;;
stop)
/usr/local/apache2/bin/apachectl stop
;;
restart)
/usr/local/apache2/bin/apachectl restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
Save this as /etc/init.d/httpd
, then make it executable:
sudo chmod +x /etc/init.d/httpd
sudo chkconfig --add httpd
sudo chkconfig httpd on
If Apache fails to start at boot:
- Check SELinux context:
ls -Z /etc/init.d/httpd
- Verify file permissions
- Examine system logs:
journalctl -xe
or/var/log/messages
For quick solutions, you can add to root's crontab:
@reboot /usr/local/apache2/bin/apachectl start
When dealing with Red Hat Enterprise Linux (or CentOS) systems using the 2.6.9 kernel (like your environment), the traditional System V init system handles service startup. Since you've installed Apache from source, it won't automatically create the necessary init scripts that package managers would normally provide.
Here's how to create a proper init script for your source-installed Apache:
#!/bin/sh
#
# Apache HTTP Server init script for source installations
# chkconfig: 345 85 15
# description: Apache HTTP Server
httpd=/usr/local/apache2/bin/httpd
apachectl=/usr/local/apache2/bin/apachectl
case "$1" in
start)
$apachectl -k start
;;
stop)
$apachectl -k stop
;;
restart)
$apachectl -k restart
;;
status)
$apachectl status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0
Save this script as /etc/init.d/httpd
and make it executable:
sudo chmod +x /etc/init.d/httpd
Then add it to the default runlevels:
sudo chkconfig --add httpd
sudo chkconfig httpd on
If you're using a newer RHEL version with systemd, create this service file:
[Unit]
Description=Apache HTTP Server (Source Install)
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl -k start
ExecStop=/usr/local/apache2/bin/apachectl -k stop
ExecReload=/usr/local/apache2/bin/apachectl -k graceful
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Save it as /etc/systemd/system/httpd.service
and enable it:
sudo systemctl daemon-reload
sudo systemctl enable httpd
After setting this up, reboot your system and verify Apache is running:
ps aux | grep httpd
sudo netstat -tulpn | grep httpd
If Apache fails to start at boot:
- Check SELinux context:
sudo restorecon -Rv /usr/local/apache2/
- Verify file permissions:
sudo chown -R root:root /usr/local/apache2/
- Examine error logs:
tail -n 50 /usr/local/apache2/logs/error_log