Auto Start Services after rebooting in Linux machines

CentOS or RHEL 6.x

The following example I have taken as Apache HTTP server, but procedure remain same for any other services you wish to start at boot in Red Hat Enterprise Linux (RHEL) or CentOS 6 version.
You can keep any script file name, and here I’ve kept httpd

  • Become as root user on your Linux server
  • Create or copy your script under /etc/init.d/
[root@tanvir init.d]# ls -ltr httpd
-rwxr-xr-x. 1 root root 3371 Jan 6 08:56 httpd
[root@tanvir init.d]#

We will use chkconfig utility which is available default on Linux or CentOS.

  • Add script to start on boot using chkconfig with --add parameter
[root@tanvir init.d]# chkconfig --add httpd
[root@tanvir init.d]# chkconfig httpd on
  • Confirm script is added successfully with --list
[root@tanvir init.d]# chkconfig --list httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

That’s all! httpd script will be called to start services on Linux boot.
In case you need to disable auto-start service then you can use the following commands

chkconfig httpd off
chkconfig --del httpd

RHEL or CentOS 7.x

Procedure to configure services on boot in RHEL 7 is slightly different than RHEL 6. It uses systemd to manage the services.
Most of the software like Apache, PHP, MySQL, Nginx scripts are added in services when you install it.
Let’s take an example of PHP-FPM.
First thing first, let’s see the status of php-fpm

[root@tanvir ~]# systemctl status php-fpm
php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
Active: inactive (dead) 

As you can see the status is disabled which means it’s not configured to start on boot.
Let’s enable php-fpm to start on boot by using systemctl

[root@tanvir ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

Now, let’s see the status

[root@tanvir ~]# systemctl status php-fpmphp
php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)    Active: inactive (dead) 

php-fpm is all set to start on boot. Let’s test it by rebooting the server.
If you ever need to disable starting services on boot, then you can use below command

systemctl disable php-fpm

Ubuntu

Configuring auto-start services in Ubuntu is slightly different. Let’s say script name is nginx

  • Login to Ubuntu server with root
  • Copy the script in /etc/init.d/ folder
  • Execute the below command
update-rc.d nginx defaults
  • Reboot the server to ensure services are started.