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

ApacheWeb ServicesAmazon Ec2

Apache Problem Overview


How do I set the [tag:httpd] and [tag:mysqld] services to start automatically upon booting an [tag:amazon-ec2] instance?

Currently I have to start them manually by connecting to the instance via ssh and running sudo service httpd start and sudo service mysqld start.

Apache Solutions


Solution 1 - Apache

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 - Apache

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 - Apache

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 - Apache

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

Solution 5 - Apache

I faced the similar problem, here is the solution i am suggesting, you need to create a file under /etc/init.d directory, e.g with name tomcat, and change the JAVA_HOME and CATALINA_HOME parameters as per your system installation. Once you do setup this file then run the below command:

sudo chkconfig <file-name> on

where is the file you have created in /etc/init.d it is tomcat in my case.

[ec2-user@ip-<myip> init.d]$ cat tomcat
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/opt/apache-tomcat-7.0.96
export $JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/opt/apache-tomcat-7.0.96

case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
esac
exit 0
chmod 755 tomcat
chkconfig --add tomcat
chkconfig --level 234 tomcat on
chkconfig --list tomcat
service tomcat start

Solution 6 - Apache

ReactJS on Amazon Linux2 process: Installing ReactJS on EC2 and running the app at boot:

  1. Once you connect to EC2 instance install NodeJS. Follow this tutorial: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html
  2. Install httpd server using this tutorial: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html
  3. I used Git Clone to clone the ReactJS app on to /home/ec2-user.
  4. Install Yarn using the command “npm install yarn -g”
  5. Execute the following commands in the cloned project: “Yarn” and then “Yarn build”
  6. Now Copy the build folder using : cp -a /build/. /var/www/html/
  7. Now go to the /var/www/html/ here create a .htaccess file using vi and include the following content: “Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.html [QSA,L]” Save the file with :wq
  8. Now in /etc/httpd/conf/httpd.conf search for Directory with “/var/www/html” attribute and change “AllowOverride None” to “AllowOverride All”. Now open the browser and enter http://ec2-ip or http://ec2-url you will see the default page
  9. Enter the command “systemctl enable httpd” and then “systemctl start httpd” on AmazonLinux2. Now you can access the app on boot rather than running the app again and again. You are complete.

Solution 7 - Apache

Either use any of the preexisting LAMP AMI, it will have both of them running as service already.

One example is BitNami, you will find several other when you fire an ec2 instance.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser655334View Question on Stackoverflow
Solution 1 - ApacheQuasarDJView Answer on Stackoverflow
Solution 2 - ApacheyoshinorinView Answer on Stackoverflow
Solution 3 - ApacheRavi RagguView Answer on Stackoverflow
Solution 4 - ApacheMayank DudakiyaView Answer on Stackoverflow
Solution 5 - ApacheJagdish0886View Answer on Stackoverflow
Solution 6 - ApacheAdithya MaringantyView Answer on Stackoverflow
Solution 7 - ApacheZimbabaoView Answer on Stackoverflow