How to auto start web services when starting an Amazon EC2 instance?

Solution 1:

Rather than starting over with a new AMI, you could just issue the following commands on an Amazon Linux EC2 instance...

sudo chkconfig mysqld on
sudo chkconfig httpd on

You can check the settings before & after enabling these services to start on boot using the following commands...

sudo chkconfig --list mysqld
sudo chkconfig --list httpd

See all services using just...

sudo chkconfig --list

NOTE: If you are having any trouble with chkconfig being in root's path, you can try specifying the full path like this...

sudo /sbin/chkconfig mysqld on
sudo /sbin/chkconfig httpd on

Solution 2:

It is different between Amazon Linux 1 and Amazon Linux 2.

Amazon Linux 1

In AmazonLinux1, use chkconfig command.

$ sudo chkconfig mysqld on
$ sudo chkconfig httpd on

Amazon Linux2

In AmazonLinux2, systemd was introduced. So, chkconfig is legacy command. You should use systemctl. It is a control command for systemd.

$ sudo systemctl enable mysqld
$ sudo systemctl enable httpd

You can confirm it is enabled or not using by is-enabled command.

$ sudo systemctl is-enabled mysqld
enabled

chkconfig command request will be forwarded to systemctl.

$ chkconfig mysqld on
Note: Forwarding request to 'systemctl enable mysqld.service'.

Solution 3:

If you using Amazon Linux 2 AMI you need to follow these steps:

  1. In AMI2 they are using systemctl for managing services check if it is installed on your machine 2.systemctl list-units --type=service by this command check if tomcat.service is listed
  2. sudo systemctl enable tomcat.service To eanable tomcat start on boot up
  3. systemctl is-enabled tomcat.service To check if tomcat enabled to start on boot up linux system

After that you can reboot your linux system and tomcat will be started.

For more about systemctl Click Here

Solution 4:

One of my client wants to do this task and I have successfully done by using following way.

Following commands starts the services automatic when instance started.

Auto start apache/httpd

1) systemctl enable httpd

Auto start redis service

2) systemctl enable redis

I have set SELINUX set to disabled in

3) /etc/sysconfig/selinux

For mysql services

sudo chkconfig mysqld on
sudo chkconfig httpd on