开发者

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

How do I set the httpd and mysqld services to start automatically upon booting an 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.


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


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'.


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


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


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


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.


The best way on Amazon Linux 2 is to use the following bash script on creation. This will install the updates, start Apache2, make it listed as a service so that it automatically restarts upon reboot, and the creation of an index.html and health.html sample files. Configuring a health page is important for application loadbalancers and for autoscaling groups.

#!/bin/bash

yum update -y

yum install httpd -y httpd-tools mod_ssl

service httpd start

chkconfig httpd on

systemctl start httpd

systemctl enable httpd

echo "Hello, World, from your Webserver on Amazon Linux" > /var/www/html/index.html

echo "Healthy" > /var/www/html/health.html
 

Cheers!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜