一键编译安装Httpd-2.4.53

###################################################################
# File Name: install_httpd.sh
# Author: Easy
# Mail: 718911514@qq.com
#=============================================================
#!/bin/bash

APR_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/apr/
APR_FILE=apr-1.7.0
TAR=.tar.bz2
APR_UTIL_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/apr/
APR_UTIL_FILE=apr-util-1.6.1
HTTPD_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/
HTTPD_FILE=httpd-2.4.53
INSTALL_DIR=/data/httpd-2.4.53
CPUS=$(lscpu | awk '/^CPU\(s\)/{print $2}')
MPM=event

install_httpd() {
    if [ "$(awk -F'"' '/^ID=/{print $2}' /etc/os-release)" == "centos" ]; then
        yum -y install gcc make expat-devel pcre-devel openssl-devel wget bzip2
    else
        sudo apt update
        sudo apt -y install gcc libapr1-dev libaprutil1-dev libpcre3 libpcre3-dev libssl-dev wget make
    fi

    cd /usr/local/src
    wget ${APR_URL}${APR_FILE}${TAR} --no-check-certificate
    wget ${APR_UTIL_URL}${APR_UTIL_FILE}${TAR} --no-check-certificate
    wget ${HTTPD_URL}${HTTPD_FILE}${TAR} --no-check-certificate

    tar xf ${APR_FILE}${TAR}
    tar xf ${APR_UTIL_FILE}${TAR}
    tar xf ${HTTPD_FILE}${TAR}

    mv ${APR_FILE} ${HTTPD_FILE}/srclib/apr
    mv ${APR_UTIL_FILE} ${HTTPD_FILE}/srclib/apr-util

    cd ${HTTPD_FILE}
    ./configure --prefix=${INSTALL_DIR} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=${MPM}
    make -j ${CPUS} && make install

    useradd -s /sbin/nologin -r apache
    sed -i 's/daemon/apache/' ${INSTALL_DIR}/conf/httpd.conf

    echo "PATH=${INSTALL_DIR}/bin:\$PATH" > /etc/profile.d/${HTTPD_FILE}.sh
    . /etc/profile.d/${HTTPD_FILE}.sh

    cat > /lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart=${INSTALL_DIR}/bin/apachectl start
ExecReload=${INSTALL_DIR}/bin/apachectl graceful
ExecStop=${INSTALL_DIR}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable --now httpd
}

install_httpd
1、变量定义
APR_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/apr/
APR_FILE=apr-1.7.0
TAR=.tar.bz2
APR_UTIL_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/apr/
APR_UTIL_FILE=apr-util-1.6.1
HTTPD_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/
HTTPD_FILE=httpd-2.4.53
INSTALL_DIR=/data/httpd-2.4.53
CPUS=$(lscpu | awk '/^CPU\(s\)/{print $2}')
MPM=event

这些变量定义了脚本中使用的一些常量:

  • APR_URL: APR(Apache Portable Runtime)的下载 URL。
  • APR_FILE: APR 文件名。
  • TAR: 压缩文件的扩展名。
  • APR_UTIL_URL: APR-util 的下载 URL。
  • APR_UTIL_FILE: APR-util 文件名。
  • HTTPD_URL: httpd 的下载 URL。
  • HTTPD_FILE: httpd 文件名。
  • INSTALL_DIR: 安装目录。
  • CPUS: CPU 核心数,用于并行编译。
  • MPM: 多进程模型(event 模式)。
2. install_httpd 函数

这个函数是脚本的核心,包含了安装 Apache HTTP Server 的所有步骤。

install_httpd() {
2.1 检查操作系统并安装依赖包
if [ "$(awk -F'"' '/^ID=/{print $2}' /etc/os-release)" == "centos" ]; then
    yum -y install gcc make expat-devel pcre-devel openssl-devel wget bzip2
else
    sudo apt update
    sudo apt -y install gcc libapr1-dev libaprutil1-dev libpcre3 libpcre3-dev libssl-dev wget make
fi

根据操作系统类型(CentOS 或 Ubuntu)安装所需的依赖包:

  • CentOS: 使用 yum 安装依赖包。
  • Ubuntu: 使用 apt 安装依赖包。
2.2 下载和解压源代码包
cd /usr/local/src
wget ${APR_URL}${APR_FILE}${TAR} --no-check-certificate
wget ${APR_UTIL_URL}${APR_UTIL_FILE}${TAR} --no-check-certificate
wget ${HTTPD_URL}${HTTPD_FILE}${TAR} --no-check-certificate

tar xf ${APR_FILE}${TAR}
tar xf ${APR_UTIL_FILE}${TAR}
tar xf ${HTTPD_FILE}${TAR}

mv ${APR_FILE} ${HTTPD_FILE}/srclib/apr
mv ${APR_UTIL_FILE} ${HTTPD_FILE}/srclib/apr-util

下载 APR、APR-util 和 httpd 源代码包,并将其解压。解压后,将 APR 和 APR-util 移动到 httpd 源码目录的 srclib 目录下。

2.3 编译和安装 httpd
cd ${HTTPD_FILE}
./configure --prefix=${INSTALL_DIR} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=${MPM}
make -j ${CPUS} && make install

配置、编译并安装 httpd。配置选项包括:

  • --prefix: 指定安装目录。
  • --enable-so: 启用动态共享对象(DSO)。
  • --enable-ssl: 启用 SSL 支持。
  • --enable-cgi: 启用 CGI 支持。
  • --enable-rewrite: 启用 URL 重写。
  • --with-zlib: 使用 zlib 库。
  • --with-pcre: 使用 PCRE 库。
  • --with-included-apr: 使用包含的 APR。
  • --enable-modules: 启用大多数模块。
  • --enable-mpms-shared: 启用所有的多进程模型(MPM)。
  • --with-mpm: 指定使用的 MPM(event 模式)。
2.4 创建 apache 用户并配置环境变量
useradd -s /sbin/nologin -r apache
sed -i 's/daemon/apache/' ${INSTALL_DIR}/conf/httpd.conf

echo "PATH=${INSTALL_DIR}/bin:\$PATH" > /etc/profile.d/${HTTPD_FILE}.sh
. /etc/profile.d/${HTTPD_FILE}.sh

创建一个 apache 用户,并将 httpd 配置文件中的运行用户改为 apache。然后配置 httpd 的环境变量,使其二进制文件可以在系统路径中使用。

2.5 创建 systemd 服务文件
cat > /lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart=${INSTALL_DIR}/bin/apachectl start
ExecReload=${INSTALL_DIR}/bin/apachectl graceful
ExecStop=${INSTALL_DIR}/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now httpd

创建一个 systemd 服务文件,使 httpd 可以作为系统服务来管理。配置完成后,重新加载 systemd 配置,并启用和启动 httpd 服务。

3. 调用 install_httpd 函数
install_httpd

最后,调用 install_httpd 函数,开始执行所有的安装步骤。

这个脚本自动化了 Apache HTTP Server 的安装过程,适用于 CentOS 和 Ubuntu 操作系统。脚本首先检查操作系统并安装必要的依赖包,然后下载和解压 APR、APR-util 和 httpd 源代码包,接着编译和安装 httpd,最后配置 systemd 服务文件并启动 httpd 服务。通过这个脚本,可以快速方便地部署 Apache HTTP Server。

  • 15
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向日葵般灿烂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值