如何让 Apache 在 Linux 上启动时启动?

gri*_*eve 38 linux startup apache-2.2

我已经在我的 Linux 机器上从源代码安装了 Apache 2。apachectl -k start工作正常,但如何让 Apache 在启动时启动?

这是在 Red Hat Linux 发行版上:

Linux <主机名> 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686 i686 i386 GNU/Linux

小智 51

您想将其 init 脚本添加到适当的运行级别。init 脚本通常/etc/init.d/apache2是您可以手动运行/etc/init.d/apache2 start以启动它的地方。

在 Gentoo 上你会写:

rc-update add apache2 default
Run Code Online (Sandbox Code Playgroud)

在 Ubuntu/Debian 上,这有效:

sudo update-rc.d apache2 defaults
Run Code Online (Sandbox Code Playgroud)

在 Red Hat Linux/Fedora/CentOS 上,通过谷歌搜索显示:

chkconfig --add httpd
Run Code Online (Sandbox Code Playgroud)

它从分布到分布略有不同,但想法通常是相同的。基本上,所有这些命令从一个符号链接/etc/init.d/到相应的运行级别的文件夹/etc/


gri*_*eve 26

这是最终对我有用的方法。这假设您是 root 用户。

  1. vi /etc/init.d/apache2(如下图编辑)
  2. chmod 755 /etc/init.d/apache2
  3. chkconfig --add apache2
  4. chkconfig --list apache2(验证它是否有效)

/etc/init.d/apache2 的内容:

#!/bin/bash
#
# apache2 Apache HTTP 服务器的启动脚本
#
# chkconfig: 3 85 15
# 描述:Apache 是一个万维网服务器。它用于服务\
# HTML 文件和 CGI​​。

/usr/local/apache2/bin/apachectl $@

您可以通过运行 /sbin/runlevel 获取运行级别,在我的情况下为 3。当然您需要调用您的 apachectl 版本,在我的情况下为 /usr/local/apache2/bin/apachectl

感谢以下人员:


Vik*_*iky 8

检查目录中是否有httpdinit 脚本/etc/rc.d。如果是,那么您只需运行以下命令即可在启动时启动httpd服务。

chkconfig --level 345 httpd on
Run Code Online (Sandbox Code Playgroud)

如果您没有 init 脚本,则只需将/etc/rc.local文件附加apachectl -k start(启动 Apache 的命令)。