源码安装Apache脚本

Apache脚本

1. 提供包

[root@node1 ~]# ls apache/files/
apr-1.7.0.tar.gz       httpd-2.4.54.tar.gz
apr-util-1.6.1.tar.gz
[root@node1 ~]# 

2. 编写安装脚本

[root@node1 apache]# vim install.sh 
[root@node1 apache]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "Execute the script as an administrator."
    exit
fi

#定义变量
install_dir=/usr/local/apache

#创建用户
id apache &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin apache
else
    echo "用户已存在"
fi

#安装依赖包
yum -y groups mark install "Development Tools"
yum -y install openssl-devel pcre-devel expat-devel libtool make gcc gcc-c++ wget

#解压软件包
rm -rf /usr/src/*
tar xf files/apr-1.7.0.tar.gz -C /usr/src
tar xf files/apr-util-1.6.1.tar.gz -C /usr/src
tar xf files/httpd-2.4.54.tar.gz -C /usr/src/

#编译安装apr
cd /usr/src/apr-1.7.0
if [ ! -d /usr/local/apr ];then
    sed -i '/$RM "$cfgfile"/d' configure
    ./configure --prefix=/usr/local/apr && \
    make && make install
else
    ls /usr/local
    echo "apr 编译安装完成"
    sleep 2
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
    ls /usr/local/
    echo "apr-util 编译安装完成"
    sleep 2
fi

#编译安装httpd
cd ../httpd-2.4.54/
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
    ls $install_dir
    sleep 2
    echo "httpd 安装完成"
fi

#查看是否有环境变量,man文档,头文件并且设置
ls $install_dir

echo "export PATH=$install_dir/bin:\$PATH" > /etc/profile.d/apache.sh
ls /etc/profile.d/apache.sh

ln -s $install_dir/include /usr/include/apache &> /dev/null
ls /usr/include/apache

grep "$install_dir/man" /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t$install_dir/man" /etc/man_db.conf
fi

#编写service file
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd server daemon
After=network.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

#加载文件
systemctl daemon-reload
systemctl enable --now httpd
ss -antl
[root@node1 apache]# 

3. 优化脚本

[root@node1 apache]# vim install.sh 
[root@node1 apache]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "Execute the script as an administrator."
    exit
fi

#定义变量
install_dir=/usr/local/apache

#创建用户
id apache &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin apache
else
    echo "用户已存在"
fi

#安装依赖包
echo -e "\033[32mInstalling dependency packages \033[\0m\033[5;32m......\033[0m"
yum -y groups mark install "Development Tools"
yum -y install openssl-devel pcre-devel expat-devel libtool make gcc gcc-c++ wget &> /dev/null

#解压软件包
rm -rf /usr/src/*
tar xf files/apr-1.7.0.tar.gz -C /usr/src
tar xf files/apr-util-1.6.1.tar.gz -C /usr/src
tar xf files/httpd-2.4.54.tar.gz -C /usr/src/

#编译安装apr
cd /usr/src/apr-1.7.0
if [ ! -d /usr/local/apr ];then
    sed -i '/$RM "$cfgfile"/d' configure
    ./configure --prefix=/usr/local/apr && \
    make && make install
else
    ls /usr/local
    echo "apr 编译安装完成"
    sleep 2
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
    ls /usr/local/
    echo "apr-util 编译安装完成"
    sleep 2
fi

#编译安装httpd
cd ../httpd-2.4.54/
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
    ls $install_dir
    sleep 2
    echo "httpd 安装完成"
fi

#查看是否有环境变量,man文档,头文件并且设置
ls $install_dir

echo "export PATH=$install_dir/bin:\$PATH" > /etc/profile.d/apache.sh
ls /etc/profile.d/apache.sh

ln -s $install_dir/include /usr/include/apache &> /dev/null

grep "$install_dir/man" /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
    sed -i "/^MANDATORY.*share\/man/a MANDATORY_MANPATH\t\t\t$install_dir/man" /etc/man_db.conf
fi

#编写service file
cat > /usr/lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd server daemon
After=network.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

#加载文件
systemctl daemon-reload

#设置开机自启
systemctl enable --now httpd
ss -antl
sleep 2
echo -e "\033[32mApache installation Complete \033[\0m"
[root@node1 apache]# 
[root@node1 ~]# tar zcf apache.tar.gz apache
[root@node1 ~]# ls
anaconda-ks.cfg  apache  apache.tar.gz
[root@node1 ~]# file apache.tar.gz 
apache.tar.gz: gzip compressed data, last modified: Wed Aug  3 09:11:40 2022, from Unix, original size 11407360
[root@node1 ~]#             

4. 配置虚拟主机,关闭防火墙和提供网站

[root@node1 apache]# cp /usr/local/apache/conf/extra/httpd-vhosts.conf files/
[root@node1 apache]# ls files/
apr-1.7.0.tar.gz       httpd-2.4.54.tar.gz
apr-util-1.6.1.tar.gz  httpd-vhosts.conf
[root@node1 apache]# 

[root@SYL7 apache]# ls
config.sh  files  install.sh
[root@SYL7 apache]# ls files/
apr-1.7.0.tar.gz       htmlxunakuhei.zip    httpd-vhosts.conf
apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz  www
[root@SYL7 apache]# cat config.sh 
#!/bin/bash

#定义变量
install_dir=$(grep '^install_dir' install.sh |awk -F'=' '{print $2}')
echo "$install_dir"

#关闭防火墙和selinux
systemctl disable --now firewalld
setenforce 0
sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/config

#设置包含虚拟主机
grep 'Include conf/extra/vhosts.conf' $install_dir/conf/httpd.conf &> /dev/null
if [ $? -ne 0 ];then
    echo "Include conf/extra/vhosts.conf" >> $install_dir/conf/httpd.conf
fi

#配置虚拟主机
cat > $install_dir/conf/extra/vhosts.conf <<EOF
<VirtualHost *:80>
    DocumentRoot "$install_dir/htdocs/game.example.com"
    ServerName game.example.com
    ErrorLog "logs/game.example.com-error_log"
    CustomLog "logs/game.example.com-access_log" common
</VirtualHost>
EOF

#创建目录
mkdir -p $install_dir/htdocs/game.example.com
cp -r files/www/* $install_dir/htdocs/game.example.com
systemctl restart httpd
[root@SYL7 apache]#

5. 打包

[root@SYL7 ~]# tar zcf apache.tar.gz apache
[root@SYL7 ~]# ls
anaconda-ks.cfg  apache  apache.tar.gz
[root@SYL7 ~]# 

6. 访问

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值