使用shell脚本安装apache
安装httpd步骤:
创建一个系统用户
下载依赖包
下载软件Apr、Apr-util、httpd软件包、并解压
编译apr、Apr-util、httpd
设置环境变量、头文件、man文档
设置开机自启
#!/bin/bash
# 关闭防火墙和SElinux
systemctl disable --now firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
echo -e "\033[1;32m 防火墙和SElinux已关闭 \033[0m"
# 定义变量
httpd_version=2.4.54
install_dir=/usr/local/apache
# 创建用户
id apache &> /dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin apache
echo "创建apache用户"
else
echo "用户apache已存在"
fi
# 下载依赖包
yum -yq install boost-devel --allowerasing openssl-devel pcre-devel expat-devel libtool make gcc gcc-c++ wget
# 下载软件包
if [ ! -d apr-1.7.0 ] && [ ! -d httpd-${httpd_version} ];then
cd /usr/src
# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
# wget https://downloads.apache.org/httpd/httpd-${httpd_version}.tar.gz
fi
# 下载并解压Apr包
cd /usr/src
if [ ! -d /usr/src/apr-1.7.0 ];then
tar xf apr-1.7.0.tar.gz
sed -i '/$RM "$cfgfile"/d' apr-1.7.0/configure
echo "Apr包已解压"
else
echo "apr-1.7.0目录已存在"
fi
# 下载并 解压apr-util包
if [ ! -d /usr/src/apr-util-1.6.1 ];then
tar xf apr-util-1.6.1.tar.gz
echo "apr-util包已解压"
else
echo "apr-util-1.6.1目录已存在"
fi
# 下载并解压httpd包
if [ ! -d /usr/src/httpd-${httpd_version} ];then
tar xf httpd-${httpd_version}.tar.gz
echo "httpd包已解压"
else
echo "httpd-${httpd_version}目录已存在"
fi
# 编译Apr
cd /usr/src/apr-1.7.0
if [ ! -d /usr/local/apr ];then
./configure --prefix=/usr/local/apr && \
make && make install
else
echo "apr目录已存在"
fi
# 编译apr-util
cd ../apr-util-1.6.1
if [ ! -d /usr/local/apr-util ];then
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make && make install
else
echo "apr-util目录已存在"
fi
# 编译httpd
cd ../httpd-${httpd_version}
if [ ! -d $install_dir ];then
./configure --prefix=$install_dir \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork && \
make && make install
else
echo "apache目录已存在"
fi
if [ ! -f etc/profile.d/apache.sh ] && [ ! -h /usr/include/apache];then
# 设置环境变量
echo "export PATH=$install_dir/bin:\$PATH" > /etc/profile.d/apache.sh
# 设置头文件
ln -s $install_dir/include /usr/include/apache
# 设置man文档
echo "MANDATORY_MANPATH $install_dir/man" >> /etc/man_db.conf
fi
# 配置service文件
if [ ! -f /usr/lib/systemd/system/httpd.service ];then
cat > /usr/lib/systemd/system/httpd.service << EOF
[Unit]
Description=httpd server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=$install_dir/bin/apachectl start
ExecStop=$install_dir/bin/apachectl stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
fi
# 刷新程序
systemctl daemon-reload
# 启动、并开机自启
systemctl enable --now httpd
echo -e "\e[1;32m apache已开机自启动 \e[0m"