如何在启动 Amazon EC2 实例时自动启动 Web 服务?

发布于 2024-10-22 10:20:34 字数 452 浏览 2 评论 0原文

如何设置 服务在启动 实例?

目前,我必须通过 ssh 连接到实例并运行 sudo service httpd start 和 sudo service mysqld start 来手动启动它们。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

忆梦 2024-10-29 10:20:34

您无需从新的 AMI 开始,只需在 Amazon Linux EC2 实例上发出以下命令...

sudo chkconfig mysqld on
sudo chkconfig httpd on

您可以在 & 之前检查设置。使用以下命令启用这些服务在启动时启动后...

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

仅使用...查看所有服务

sudo chkconfig --list

注意如果您在 root 路径中遇到 chkconfig 问题,您可以尝试像这样指定完整路径...

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

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
淡淡離愁欲言轉身 2024-10-29 10:20:34

Amazon Linux 1 和 Amazon Linux 2 之间有所不同。

Amazon Linux 1

在 AmazonLinux1 中,使用 chkconfig 命令。

$ sudo chkconfig mysqld on
$ sudo chkconfig httpd on

Amazon Linux2

在 AmazonLinux2 中,引入了 systemd。因此,chkconfig 是旧命令。您应该使用systemctl。它是systemd的控制命令。

$ sudo systemctl enable mysqld
$ sudo systemctl enable httpd

您可以通过 is-enabled 命令确认它是否启用。

$ sudo systemctl is-enabled mysqld
enabled

chkconfig 命令请求将转发到 systemctl

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

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'.
¢好甜 2024-10-29 10:20:34

如果您使用 Amazon Linux 2 AMI,您需要执行以下步骤:

  1. 在 AMI2 中,他们使用 systemctl 来管理服务,检查它是否安装在您的计算机上
    2.systemctl list-units --type=service 通过此命令检查 tomcat.service 是否列出
  2. sudo systemctl enable tomcat.service 使 tomcat 在启动时启动
  3. >systemctl is-enabled tomcat.service 检查tomcat是否在Linux系统启动时启动。

之后,您可以重新启动Linux系统,tomcat将启动。

有关 systemctl 的更多信息 点击此处

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

唔猫 2024-10-29 10:20:34

我的一位客户想要完成这项任务,我已经通过使用以下方式成功完成了。

以下命令在实例启动时自动启动服务。

自动启动apache/httpd

1) systemctl enable httpd

自动启动redis服务

2) systemctl enable redis

我已将SELINUX设置为禁用

3) /etc/sysconfig/selinux

对于mysql服务

sudo chkconfig mysqld on
sudo chkconfig httpd on

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
原野 2024-10-29 10:20:34

我遇到了类似的问题,这是我建议的解决方案,
您需要在/etc/init.d 目录下创建一个文件,例如名称为tomcat,并根据您的系统安装更改JAVA_HOME 和CATALINA_HOME 参数。
一旦你设置了这个文件,然后运行以下命令:

sudo chkconfig <file-name> on

你在 /etc/init.d 中创建的文件在哪里,在我的例子中是 tomcat。

[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

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
闻呓 2024-10-29 10:20:34

Amazon Linux2 上的 ReactJS 流程:
在 EC2 上安装 ReactJS 并在启动时运行应用程序:

  1. 连接到 EC2 实例后,安装 NodeJS。请按照本教程操作:
    https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html
  2. 使用本教程安装 httpd 服务器:https://docs.aws.amazon.com/AmazonRDS/ latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html
  3. 我使用 Git Clone 将 ReactJS 应用程序克隆到 /home/ec2-user。
  4. 使用命令“npm install yarn -g”安装 Yarn
  5. 在克隆的项目中执行以下命令:“Yarn”,然后“Yarn build”
  6. 现在使用以下命令复制构建文件夹:cp -a /build/。 /var/www/html/
  7. 现在转到 /var/www/html/ ,使用 vi 创建一个 .htaccess 文件并包含以下内容:“Options -MultiViews
    重写引擎开启
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [QSA,L]”
    使用 :wq 保存文件
  8. 现在在 /etc/httpd/conf/httpd.conf 中搜索具有“/var/www/html”属性的目录,并将“AllowOverride None”更改为“AllowOverride All”。现在打开浏览器并输入 http://ec2-ip 或 http://ec2-url 您将看到默认页面
  9. 在 AmazonLinux2 上输入命令“systemctl enable httpd”,然后输入“systemctl start httpd”。现在,您可以在启动时访问该应用程序,而无需一次又一次地运行该应用程序。
    你完整了。

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.
行雁书 2024-10-29 10:20:34

Amazon Linux 2 上的最佳方法是在创建时使用以下 bash 脚本。这将安装更新,启动 Apache2,将其列为服务,以便在重新启动时自动重新启动,并创建 index.html 和 health.html 示例文件。配置运行状况页面对于应用程序负载均衡器和自动缩放组非常重要。

#!/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
 

干杯!

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!

南风几经秋 2024-10-29 10:20:34

要么使用任何预先存在的 LAMP AMI,它们都将作为服务运行。

一个例子是 BitNami,当您启动 ec2 实例时,您会发现其他几个。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文